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.
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.
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.
Use INTCK function
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.
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.));
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.));
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.));
@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).
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.