turn on suggestions

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

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Calculate difference between dates in months with ...

Topic Options

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

12-14-2017 07:50 AM

Hi! If I want to calculate the difference between months I use this syntax;

```
*months;
DATA want;
SET have;
months=intck("month", start, end);
RUN;
```

But you only get the months without decimals. Is there an easy way to get the months with for example 2 decimals?

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to hovliza

12-14-2017 08:04 AM

Fractions of what? 31-day months, 30-day months, 28- or 29-day months?

I'd rather use days. A day is a day is a day.

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

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to hovliza

12-14-2017 08:06 AM

I guess that depends on how you define the word "easy".

The problem is that decimal months are not really defined; in fact, because some months have 30 days and some months have 31 days and some months have 28 or 29 days, I don't think you will find a unique answer, and I don't think there is anything programmed into SAS that will provide decimal months.

Nevertheless, you can create your own definition of decimal months and write data step code to make this happen.

--

Paige Miller

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to PaigeMiller

12-14-2017 08:12 AM

Okay, thanks for the suggestions. I'd hoped for a procedure or something. But for now I'm going to use this syntax;

```
DATA want;
SET have;
months=(end-start)/(365.25/12);
RUN;
```

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to hovliza

12-14-2017 09:33 AM

That's both assuming an average for year and for month. How accurate do you need it to be? May not be a suitable calculation for some.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

12-14-2017 10:30 AM

And ignores leap years.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to hovliza

12-14-2017 10:40 AM

hovliza wrote:

Hi! If I want to calculate the difference between months I use this syntax;

`*months; DATA want; SET have; months=intck("month", start, end); RUN;`

But you only get the months without decimals. Is there an easy way to get the months with for example 2 decimals?

Please describe exactly how you plan on using that fractional month part of the value.

Anything I have worked on that needed a different interval worked much better by dropping to an actual different interval, either weeks, biweekly period, days or even 5-day periods. That way at least handles 0.1month = one of .28, .29, .30 or .31.

Note that the intck and intnx functions supports multiples of the interval such as DAY3 or WEEK2 so you can easily specify a consistent period.