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 age in months from yyyymm dates

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

06-30-2017 07:58 AM

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

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

Posted in reply to Cruise

06-30-2017 08:16 AM - edited 06-30-2017 08:20 AM

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

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

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

Posted in reply to Cruise

06-30-2017 08:16 AM - edited 06-30-2017 08:20 AM

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

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 KurtBremser

06-30-2017 08:19 AM

It gave me minus values, such -23, -12 et.c

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

Posted in reply to KurtBremser

06-30-2017 08:20 AM

Got it. worked out. Switched the position of two dates. Thanks

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

Posted in reply to Cruise

06-30-2017 08:16 AM

Use INTCK function

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

Posted in reply to Cruise

06-30-2017 08:40 AM

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.

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

Posted in reply to Astounding

06-30-2017 10:39 AM

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

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

Posted in reply to Cruise

06-30-2017 10:51 AM

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

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

Posted in reply to ballardw

06-30-2017 01:26 PM

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

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

Posted in reply to Cruise

06-30-2017 01:34 PM

SUNY_Maggie wrote:

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