BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
master_kim
Fluorite | Level 6

Hello everyone.

I have a numeric variable of date of birth expressed in format of yyyymmdd (i.e. 19880731). I was wondering how I could calculate the age in, for example, 2023-01-01. I was thinking of converting the date of birth into some continuous number and get difference and divide by 365.25. However, I was not sure how I could convert the date of birth into a continuous value. I would really appreciate if anyone could give me some suggestion.

 

Thank you very much!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If you have a plain number like 19880731 that mimics a date then the best thing is to create a new variable that is an actual SAS date value. Then you have multiple functions, such as YRDIF to work with the values. Then you do not need to kludge things like "divide by 365.25".

 

Something like

data example;
   dateofbirth= 19880731;
   dob = input(put(dateofbirth,8. -L),yymmdd10.);
   format dob date9.;/* or pick your favorite date format there are lots*/
   agetoday = yrdif(dob,today());
run;

If you want a "legal age" such as years without the decimal portion then use the Floor function on the agetoday or your variable name.

Note that AGE of any sort requires two dates, date of birth and the date the age is to be determined for.

 

The above code creates a text value that the Input function can use with the Put function, then uses the informat yymmdd10 to read the result into a SAS data set. OR do that what when reading the data into SAS.

 

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

View solution in original post

5 REPLIES 5
ballardw
Super User

If you have a plain number like 19880731 that mimics a date then the best thing is to create a new variable that is an actual SAS date value. Then you have multiple functions, such as YRDIF to work with the values. Then you do not need to kludge things like "divide by 365.25".

 

Something like

data example;
   dateofbirth= 19880731;
   dob = input(put(dateofbirth,8. -L),yymmdd10.);
   format dob date9.;/* or pick your favorite date format there are lots*/
   agetoday = yrdif(dob,today());
run;

If you want a "legal age" such as years without the decimal portion then use the Floor function on the agetoday or your variable name.

Note that AGE of any sort requires two dates, date of birth and the date the age is to be determined for.

 

The above code creates a text value that the Input function can use with the Put function, then uses the informat yymmdd10 to read the result into a SAS data set. OR do that what when reading the data into SAS.

 

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.

PaigeMiller
Diamond | Level 26

Convert 19880731 to an actual SAS date value (19880731 might look like a date value to humans but it is not a date value to SAS). Then use the INTCK function to compute the age at a given date.

 

data have;
    date_value=19880731;
run;

data want;
    set have;
    sas_date = input(put(date_value,8.),yymmdd8.); /* Convert to actual SAS date value */
    years_of_age = intck('year',sas_date,mdy(1,1,2023)); /* Compute years of age on 01JAN2023 */
run;
--
Paige Miller
gspritom
Fluorite | Level 6

Hi, I am a fairly new SAS user and I have a dataset where the birth year and birth month is recorded in seperate columns and the date of survey is recorded in MMDDYYYY format. I need to extract age at survey for each participants.

 

First I tried the yrdif but failed.

Then I tried this solution but still did not work.

 

Could you suggest what would be the best way forward?

 

Thanks in advance.

PaigeMiller
Diamond | Level 26

something like this should work, it creates an actual SAS date from your data

 

data want;
    set have;
    birthday= mdy(birthmonth,1,birthyear);

 

 

and then yrdif should work. This assumes that birthmonth are integers with obvious meaning, and birthyear is a 4-digit integer. INTCK should also work.

--
Paige Miller
Tom
Super User Tom
Super User

There is no SAS FORMAT named MMDDYYYY so it is not clear what type of variable you have the survey date.

 

Run PROC CONTENTS on your dataset and check the TYPE of the variable and what FORMAT, if any, it has attached to it.

 

If it is numeric with a format like MMDDYYN8. or DATE9. or any other format that works with date values then you already have a date value.

 

If it is numeric but has some other format attached, but prints as digits strings that a human would recognize as a date in MMDDYYYY style then you will have to first use PUT() function to convert it to a string and then use INPUT() function to convert it back into a number, only one that is an actual date value.

 

If it is character you will need to use the INPUT() function to convert it into a date value.

 

You can use MDY() with an arbitrary day of the month, like the 1st or the 15th, to convert your month and year of birth into a date value.

 

Once you have two date values then the yrdif calculations will be possible.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 5 replies
  • 670 views
  • 4 likes
  • 5 in conversation