Solved
Contributor
Posts: 27

# Calculating age-related variable

Hello

I am trying to read raw data into SAS and need some help create age-related variables using information such as DOD, DOB, and age. According to the coding definition I was provided with for the variable "age":

Begins with:
0 or 1 = Years
2 = Months
3 = Days
4 = Hours
5 = Minutes
9 = Unknown (999)

Does anyone know how to create person's age at death based on the definition above? Ultimately, I'd like to subset people who died before they reach one year of age. I've attached some sample data. All three variables are numeric.

Thank you

Accepted Solutions
Solution
‎10-21-2016 01:19 PM
Super User
Posts: 6,785

## Re: Calculating age-related variable

[ Edited ]

So assuming we should just ignore the DOD and DOB, and just use AGE, why is there a problem?  Do you need to come up with a fractional age for hours or minutes in the AGE field?  Something like this should be straightforward:

if age < 200 then age_at_death = age;

else if age < 300 then age_at_death = (age-200)/12;

else if age < 900 then age_at_death=0;

This assumes that the data was entered somewhat intelligently.

21 = 21 years

201 = 1 month

All Replies
Super User
Posts: 13,583

## Re: Calculating age-related variable

Many users here don't want to download Excel files because of virus potential, others have such things blocked by security software. Also if you give us Excel we have to create a SAS data set and due to the non-existent constraints on Excel data cells the result we end up with may not have variables of the same type (numeric or character) and even values.

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... has instructions on making data step code from a SAS data set that can be posted here as text that we can use to create data to test code against.

Your data "explanation" is not very clear (and I'm not opening the xlsx).

The first step would be to read the date information into SAS date valued variables. If you have day, month and year values then the function MDY creates a SAS date value, or possibly the data can be read using an informat but we would have to see examples of what you have.

Once you have SAS dates for DOB and DOD it is very easy to get intervals, for example DOD-DOB would be number of days between birth and death. You may have to decide on a rule involving leap years though.

Any record with "unknown" is likely not going to allow an age calculation.

Contributor
Posts: 27

[ Edited ]

Super User
Posts: 6,785

## Re: Calculating age-related variable

So if you have a DOB like 1211945, how do you know what date that is?  It could be December 1, 1945, or it could be January 21, 1945.

Contributor
Posts: 27

## Re: Calculating age-related variable

Sorry- it should be MMDDYYYY

Super User
Posts: 13,583

## Re: Calculating age-related variable

Still a problem. There is not indication that the month or the day is forced to be two digits.

Post a zip file would have had the same problem. That is why the instructions I posted start with a SAS DATA SET which would show us exactly what your current SAS data set looks like.

Solution
‎10-21-2016 01:19 PM
Super User
Posts: 6,785

## Re: Calculating age-related variable

[ Edited ]

So assuming we should just ignore the DOD and DOB, and just use AGE, why is there a problem?  Do you need to come up with a fractional age for hours or minutes in the AGE field?  Something like this should be straightforward:

if age < 200 then age_at_death = age;

else if age < 300 then age_at_death = (age-200)/12;

else if age < 900 then age_at_death=0;

This assumes that the data was entered somewhat intelligently.

21 = 21 years

201 = 1 month

Contributor
Posts: 27

[ Edited ]

Super User
Posts: 6,785

## Re: Calculating age-related variable

If you want a suggestion, you'll have to show the complete set of data points:  what went in, what came out ... for observations that come up with the wrong calculations.

Contributor
Posts: 27