To round a number to the nearest integer in SAS, you can use the SAS round() function. By default, round() rounds to the nearest integer.
data data_with_rounding; set data; round_to_integer = round(num); run;
When working with numbers in SAS, the ability to round and keep a certain number of decimal places is valuable.
To round to the nearest integer in a SAS data step, you can use the sas round() function.
By default, the SAS round() function rounds to the nearest integer. If you wanted to change this behavior, you could pass another argument defining how many decimal places you wanted to keep.
Let’s say we have the following code which creates a SAS dataset with some numbers.
data data; input num; datalines; 84.3187 19.23498 5.61295 -0.45324 -6.5123 -100.2382 ; run;
To round these numbers to the nearest integer, we can use the following SAS code.
data data_rounded_to_integer; set data; round_to_integer = round(num); run;
This results in the following dataset.
num round_to_integer 1 84.3187 84 2 19.23498 19 3 5.61295 6 4 -0.45324 0 5 -6.5123 -7 6 -100.2382 -100
Other SAS Rounding Methods to Round Numbers
Rounding numbers to the nearest integer can be useful, but if you want to make sure that you are going in the right direction (up or down), sometimes it is better to use a function different from round().
If you want to round a number down, you can use the SAS floor() function.
data data_with_floor; set data; floor = floor(num); run;
If you want to round a number up, you can use the SAS ceil() function.
data data_with_ceiling; set data; ceil= ceil(num); run;
With the SAS ceil() and floor() functions, you ensure that you are going to the integer you want, and not just letting round() determine the direction you are rounding your data.
Hopefully this article has been useful for you to learn how to round numbers to integers in SAS.