Solved
Contributor
Posts: 23

# Create new age variable then create agegroups

Hello,

I need to create an age variable based on date of birth ('dob') and another date ('dov'). Examples of how the numbers in dob and dov are displayed are: 5503, 6445, 16229, 16404.

How do I create a new variable 'age' based on the variables 'dob' and 'dov'? I'm not sure what format the dates or age has to be in, but age needs to be readable so that 12 = 12 years old, etc. Once I have the age variable in place, I will need to perform 2-way ANOVA to test for interaction to see if agegroup is an effect modifier.

I am not having luck creating the age variable, but did create agegroups (assuming I have age in place):

data test;

set homewrk2.test;

/*create age groups*/

if age <= 12 then agegrp=1;

else if 12 <= age < 20 then agegrp=2;

else if age >=20 then agegrp=3;

run;

Any and all help and advice would be appreciated, in creating the new variable age and then creating agegroups!!

Accepted Solutions
Solution
‎02-24-2013 09:57 PM
Super Contributor
Posts: 1,636

## Re: Create new age variable then create agegroups

Hi try:

data test;

set homewrk2.test;

age=int((dov-dob)/365.25);

/*create age groups*/

if age <= 12 then agegrp=1;

else if 12 <= age < 20 then agegrp=2;

else if age >=20 then agegrp=3;

run;

All Replies
Solution
‎02-24-2013 09:57 PM
Super Contributor
Posts: 1,636

## Re: Create new age variable then create agegroups

Hi try:

data test;

set homewrk2.test;

age=int((dov-dob)/365.25);

/*create age groups*/

if age <= 12 then agegrp=1;

else if 12 <= age < 20 then agegrp=2;

else if age >=20 then agegrp=3;

run;

Contributor
Posts: 23

## Re: Create new age variable then create agegroups

Thank you. This seems to work! The only issue is that it is telling me that 'age' is uninitialized once I create the agegroups; log is below. Do you know if this note about it being uninitialized is an issue?

115  data test;

116  set homewrk2.test;

117  /*create new variable for age*/

118  age=int((dov-dob)/365.25);

119  run;

NOTE: There were 665 observations read from the data set HOMEWRK2.BONE.

NOTE: The data set WORK.BONE has 665 observations and 10 variables.

NOTE: DATA statement used (Total process time):

real time           0.92 seconds

cpu time            0.00 seconds

120  data test;

121  set homewrk2.test;

122  /*create age groups so that group 1 = children under 12 years old, group 2 = children 12 or

122! older but younger than 20, group 3 = 20 or older*/

123  if age < 12 then agegrp=1;

124  else if 12 <= age < 20 then agegrp=2;

125  else if age >=20 then agegrp=3;

126  run;

NOTE: Variable age is uninitialized.

Super Contributor
Posts: 1,636

## Re: Create new age variable then create agegroups

Hi,

Did you use dataset HOMEWRK2.BONE. or HOMEWRK2.test? It would be helpful if you post your code.

Occasional Contributor
Posts: 13

## Re: Create new age variable then create agegroups

When it says in your log that "Age is uninitialized" what that means is that the variable "Age" does not exist in the dataset.  I don't know why the LOG always shows this note in blue, when it seems to me that it should be in big bold red letters, but so be it.  It is a problem because any calculations that you attempt on based on "Age" will result to missing (because there is no age...).

In the example you posted above, it appears that the problem is because you're writing over the same dataset twice. (Although, as Linlin points out, it's a little hard to tell because your LOG notes reference a different dataset than what the code references...)  So ignoring the LOG notes that reference HOMEWRK2.BONE, what I see is:

data test;                           <--- you create a dataset WORK.TEST

set homewrk2.test;             <--- It is based off of HOMEWRK2.TEST

age=int((dov-dob)/365.25);    <--- You create a new variable in WORK.TEST called "AGE"

run;

data test;                         <--- You again create a dataset WORK.TEST, essentially writing over everything you just did.

set homewrk2.test;           <--- This is also based off of HOMEWRK2.TEST, ignoring everything you just did.

if age < 12 then agegrp=1; <--- SAS freaks out because HOMEWRK2.TEST does not have a variable called "AGE"

else if 12 <= age < 20 then agegrp=2;

else if age >=20 then agegrp=3;

run;                                                     <--- The end

data test;

set homewrk2.test;

age=int((dov-dob)/365.25);

if age < 12 then agegrp=1;

else if 12 <= age < 20 then agegrp=2;

else if age >=20 then agegrp=3;

run;                                                     <--- The end

🔒 This topic is solved and locked.