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

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!!

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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;

View solution in original post

4 REPLIES 4
Linlin
Lapis Lazuli | Level 10

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;

SASstudent2013
Calcite | Level 5

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.

Linlin
Lapis Lazuli | Level 10

Hi,

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

TashaChapman
Fluorite | Level 6

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

So instead, try this:

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

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 15550 views
  • 0 likes
  • 3 in conversation