Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- Calculating time interval in months

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 08-09-2023 04:06 AM
(1194 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Okay, thankyou

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

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

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

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

Are you ready for the spotlight? We're accepting content ideas for **SAS Innovate 2025** to be held May 6-9 in Orlando, FL. The call is **open **until September 16. Read more here about **why** you should contribute and **what is in it** for you!

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.