Rounding and Truncating Numbers - SAS Programming

Suppose you want to convert pounds to kilograms and inches to centimeters, and you want the results rounded to the nearest kilogram and the nearest tenth of a centimeter (millimeter) respectively.You can use the ROUND function to accomplish this task.

Example

DATA NEW;
SET OLD? *Assume QUO has variables WEIGHT and HEIGHT;
WEIGHT=ROUND (WEIGHT/2,2 , 1);
HEIGHT=ROUND (2.54*HEIGHT , .1);
RUN;

The ROUND function takes two arguments:the first is the variable to be rounded, the second is the rounding unit. By the way,the second argument of the ROUND function does not have to be a power of 10.For example, to create a new variable X, which is Y rounded to the nearest 20, you could write:

X=ROUND (Y, 2 0 ) ;

You can have the same variable on both sides of the assignment statement. Thus,to round Y to the nearest 10, you would write:

Y=ROUND (Y, 10);

To truncate a number (drop off the fractional part),you use the INT function.To truncate WEIGHT and HEIGHT instead of rounding, you would rewrite the program as follows:

Example

DATA NEW;
SET OLD; *Assume OLD has variables WEIGHT and HEIGHT;
WEIG8T-INT (WEIGHT/2,2);
HEIGHT*I8T (2.54*HEIGHT);
RUN;

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics