DATA Step, Macro, Functions and more

Calculate age in months from yyyymm dates

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 125
Accepted Solution

Calculate age in months from yyyymm dates

I have two date variables both in yyyymm format. It looks like 201601 and character. I'd like to calculate age in months. Below code didn't work, of course.

 

age=(discharge_date-patient_birth_date)/30;

Thanks for help.

 


Accepted Solutions
Solution
‎06-30-2017 08:20 AM
Super User
Posts: 6,931

Re: Calculate age in months from yyyymm dates

[ Edited ]

First you need to expand the incomplete dates to real dates, then you can convert those to SAS date values, with which you can use the SAS date functions:

age = intck('month',input(patient_birth_date !! '01',yymmdd8.),input(discharge_date !! '01',yymmdd8.));

 

Edit: changed order of variables to get a positive month count.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎06-30-2017 08:20 AM
Super User
Posts: 6,931

Re: Calculate age in months from yyyymm dates

[ Edited ]

First you need to expand the incomplete dates to real dates, then you can convert those to SAS date values, with which you can use the SAS date functions:

age = intck('month',input(patient_birth_date !! '01',yymmdd8.),input(discharge_date !! '01',yymmdd8.));

 

Edit: changed order of variables to get a positive month count.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 125

Re: Calculate age in months from yyyymm dates

It gave me minus values, such -23, -12 et.c
Frequent Contributor
Posts: 125

Re: Calculate age in months from yyyymm dates

Got it. worked out. Switched the position of two dates. Thanks
PROC Star
Posts: 551

Re: Calculate age in months from yyyymm dates

Use INTCK function

Super User
Posts: 5,080

Re: Calculate age in months from yyyymm dates

Be very careful about using the INTCK function.  You need to understand what it counts, which barely resembles your original formula (difference in days / 30).

 

INTCK measure the number of boundaries crossed.  When dealing with months, it measures the number of "1st of the month" dates within the interval.  

 

January 2, 2017 to January 30, 2017 ==> INTCK returns 0, since there are no "1st of the month" dates within the interval.

 

January 31, 2017 to February 2, 2017 ==> INTCK returns 1, since there is one "1st of the month" date within the interval

 

Along the way, you did figure out how to convert your character values to numeric dates, but you might want to change the formula and get rid of INTCK.  At least you should be aware of what you are getting so you can make that choice.

Frequent Contributor
Posts: 125

Re: Calculate age in months from yyyymm dates

@Astounding

 

Thanks.

I'm dealing with newborn data where age is often less than a month. Ideally would calculate it in days but i have no access to confidential data with complete yyyymmdd yet.

Curious to se ethe difference with intck vs wihout intck. Simply removing intck didn't work out. Could you edit in below code for the version without intck? 

 

age = ('month',(input(patient_birth_date !! '01',yymmdd8.)),input( discharge_date !! '01',yymmdd8.));
Super User
Posts: 10,483

Re: Calculate age in months from yyyymm dates

Subtracting with dates will give the number of days difference. If you are imputing a day of the month as shown you might consider using 15 instead of 01 to minimize the mean bias potential. That is a recommendation from CDC on using there standard growth curve data when calculating age from year/month data.

 

days = (input( discharge_date !! '151',yymmdd8.)) - (input(patient_birth_date !! '15',yymmdd8.));

Frequent Contributor
Posts: 125

Re: Calculate age in months from yyyymm dates

@ballardw

I appreciate it. Directly relevant to my context as well. One more question, may I? 

Do you know how to calculate age at current date? below didn't work out. 

 

age_now = (( today,yymmdd8.)) - (input(patient_birth_date !! '15',yymmdd8.));
Super User
Posts: 10,483

Re: Calculate age in months from yyyymm dates


SUNY_Maggie wrote:

@ballardw

I appreciate it. Directly relevant to my context as well. One more question, may I? 

Do you know how to calculate age at current date? below didn't work out. 

 

age_now = (( today,yymmdd8.)) - (input(patient_birth_date !! '15',yymmdd8.));

Instead of (( today,yymmdd8.)) which tells sas to do something with a variable named Today you want the function

 

today() instead of (( today,yymmdd8.))

 

You may want to investigate the SAS online help for Date functions for more goodies involved with tearing dates apart(to get day of the month or week, month of the year, the year) or building from other variables (build a date from variables containing numeric month, day of month, and year).

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 154 views
  • 2 likes
  • 5 in conversation