BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Cruise
Ammonite | Level 13

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

9 REPLIES 9
Kurt_Bremser
Super User

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.

Cruise
Ammonite | Level 13
It gave me minus values, such -23, -12 et.c
Cruise
Ammonite | Level 13
Got it. worked out. Switched the position of two dates. Thanks
Astounding
PROC Star

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.

Cruise
Ammonite | Level 13

@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.));
ballardw
Super User

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

Cruise
Ammonite | Level 13

@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.));
ballardw
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 9 replies
  • 7940 views
  • 2 likes
  • 5 in conversation