I calculate the interval between two dates in days. And divided this by 30.5 to get the results in month. I did it this way because I wanted to know the exact number of days behind this calculation. I realise if the number of days are less than or equal to 15, this is outputted as 0. Any Idea what I can do here to show that the value is not really 0 but a half month? Can I set this to 0.5 months even if the value were to be 5 days or should this remain 0?
Or can I make the months have decimal places so that 0.16 months will relate to 5days approximately. Does this make sense?
I used the inck("day", firstdate, seconddate) to do my calculation.
Thanks for any help
Since SAS dates are counts of days, your calculation is equivalent to
seconddate - firstdate
If you do
result = (seconddate - firstdate) / 30.5; format result 5.2;
you'll get your correct fractions.
If it "makes sense" is always defined by the task for which you use the resulting number. There is no universal definite answer for this.
If your concern is half month values perhaps you should look at the INTCK function with the SEMIMONTH interval.
Intck and Intnx can also work with custom multiples.
You might look at your interval result with using
intck("day6", firstdate, seconddate);
Which returns the number of boundaries between dates in terms of 6 day chunks (or 3 or 21 or ...)
Without knowing exactly how your resulting "month" or other interval is to be used it is kind of hard be sure exactly what to suggest.
Are you ok with end-of-January to end-of-February being 0.92 months, rather than 1 month?
1747 data _null_; 1748 months=('28feb2023'd-'31jan2023'd)/30.5; 1749 put months=5.2; 1750 run; months=0.92
If so, then @Kurt_Bremser's solution is all you need.
Are you asking how to round a fraction to intervals of one half?
Just use the ROUND() function.
1244 data test; 1245 do x=0.1 to 0.9 by 0.1, 1 to 3 by 0.25 ; 1246 y=round(x,0.5); 1247 put x= y=; 1248 end; 1249 run; x=0.1 y=0 x=0.2 y=0 x=0.3 y=0.5 x=0.4 y=0.5 x=0.5 y=0.5 x=0.6 y=0.5 x=0.7 y=0.5 x=0.8 y=1 x=0.9 y=1 x=1 y=1 x=1.25 y=1.5 x=1.5 y=1.5 x=1.75 y=2 x=2 y=2 x=2.25 y=2.5 x=2.5 y=2.5 x=2.75 y=3 x=3 y=3
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.