Hi guys,
I have a quite trivial problem. I have a number which can have one decimal point and I want to "trunc it down" by 0.5 step. It means: if I have 2.2 I want 2. If I have 2.4 I want 2. If I have 2.6 I want 2.5, like shown in the following code:
data numbers;
input have $3. want $5.;
datalines;
2.2 2
2.6 2.5
2.9 2.5
3.1 3
;
run;
Can anyone help me please? 🙂
Thanks in advance!
Filip
Very simple.
data numbers;
input have want ;
result = int(2*have)/2 ;
diff = result-want;
datalines;
2.2 2
2.6 2.5
2.9 2.5
3.1 3
;
proc print;
run;
If you really have character variables instead of numbers then first convert the character string into a number using INPUT(). If you want a character result then convert the number to a string using PUT().
x=((x-int(x))>=0.5)*0.5+int(x);
Very simple.
data numbers;
input have want ;
result = int(2*have)/2 ;
diff = result-want;
datalines;
2.2 2
2.6 2.5
2.9 2.5
3.1 3
;
proc print;
run;
If you really have character variables instead of numbers then first convert the character string into a number using INPUT(). If you want a character result then convert the number to a string using PUT().
I think your objective can be resolved via the second argument of the ROUND function:
want=round(have,0.5);
If you are starting out with character variable (say with length 5), then
want=round(input(have,5.),0.5);
Hi guys,
Thanks a lot for you answers, it helped me a lot 🙂
Best,
Filip
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.