## Calculating time interval in months

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

## Re: Calculating time interval in months

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

## Re: Calculating time interval in months

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

## Re: Calculating time interval in months

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.

Okay, thankyou

## Re: Calculating time interval in months

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.

## Re: Calculating time interval in months

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.

## Re: Calculating time interval in months

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

--------------------------

## Re: Calculating time interval in months

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

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