BookmarkSubscribeRSS Feed
hovliza
Obsidian | Level 7

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?

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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
hovliza
Obsidian | Level 7

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;

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

ballardw
Super User

And ignores leap years.

ballardw
Super User

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 6 replies
  • 16625 views
  • 2 likes
  • 5 in conversation