BookmarkSubscribeRSS Feed
Anita_n
Pyrite | Level 9

Dear all,

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

8 REPLIES 8
Kurt_Bremser
Super User

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.

Anita_n
Pyrite | Level 9

@Kurt_Bremser Thanks for your reply, I just wanted to know if it makes sense to output months as decimal values

Anita_n
Pyrite | Level 9
Okay, thankyou
ballardw
Super User

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.

Reeza
Super User
As long as you're ok with some fuzziness, for example 0.16 days for February is 4.48 days which will round to 4 days.
mkeintz
Jade | Level 19

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.

--------------------------
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

--------------------------
Tom
Super User Tom
Super User

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

 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 275 views
  • 1 like
  • 6 in conversation