BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
filippo_kow
Obsidian | Level 7

 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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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().

View solution in original post

4 REPLIES 4
quickbluefish
Barite | Level 11

x=((x-int(x))>=0.5)*0.5+int(x);
Tom
Super User Tom
Super User

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().

mkeintz
PROC Star

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);
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
filippo_kow
Obsidian | Level 7

 Hi guys,

 

Thanks a lot for you answers, it helped me a lot 🙂

 

 Best,

 Filip

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 649 views
  • 2 likes
  • 4 in conversation