## Calculate age in months from yyyymm dates

Solved
Super Contributor
Posts: 358

# 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: 10,217

## 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
How to convert datasets to data steps
How to post code

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

## 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
How to convert datasets to data steps
How to post code
Super Contributor
Posts: 358

## Re: Calculate age in months from yyyymm dates

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

## Re: Calculate age in months from yyyymm dates

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

## Re: Calculate age in months from yyyymm dates

Use INTCK function

Super User
Posts: 6,762

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

Super Contributor
Posts: 358

## 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: 13,523

## 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.));

Super Contributor
Posts: 358

## 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: 13,523

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

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.