Solved
Contributor
Posts: 23

# 2-way ANOVA question

Hello,

I need to perform a two-way ANOVA by age group and sex with no interaction term, and then with an interaction term.

When I do this, it's not recognizing the 'agegrp' variable, even though I created it earlier! Included below is the code (also includes histogram and qqplot generation) and then a portion of the log. Do you know why it is not recognizing the 'agegrp' variable in the end for the 2-way ANOVA?

Any and all help is much appreciated!!

data test;

set homewrk2.test;

/*create new variable for age*/

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

run;

data test;

set homewrk2.test;

/*create age groups so that group 1 = children under 12 years old, group 2 = children 12 or older but younger than 20, group 3 = 20 or older*/

if age < 12 then agegrp=1;

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

else if age >=20 then agegrp=3;

run;

title 'Distribution of periosteal circumference';

ods graphics off;

ods select Lognormal.ParameterEstimates Lognormal.GoodnessOfFit MyPlot;

/*Create a histogram for the variable periosteal circumference to test for normality*/

proc univariate data=homewrk2.test;

var peri;

histogram / lognormal(w=3 theta=est)

vaxis = axis1

name  = 'MyPlot';

inset n mean (5.3) std='Std Dev' (5.3) skewness (5.3) /

pos = ne  header = 'Summary Statistics';

axis1 label=(a=90 r=0);

run;

/*Create a qqplot for the variable periosteal circumference to test for normality*/

title ’Normal Quantile-Quantile Plot for Periosteal Circumference’;

proc capability data=homewrk2.test noprint;

spec lsl=9.5 llsl=2 clsl=blue

usl=10.5 lusl=20 cusl=blue;

qqplot peri / normal(mu=est sigma=est color=red l=2)

square

nospeclegend;

run;

/*Two way ANOVA by age group and sex with no interaction term. Save the predicted values into a data set called out1*/

proc glm data=homewrk2.test;

class sex agegrp;

model peri = sex agegrp;

run;

LOG:

216  proc glm data=homewrk2.test;

217  class sex age;

NOTE: The previous statement has been deleted.

218  model peri = sex age;

NOTE: The previous statement has been deleted.

219  run;

Accepted Solutions
Solution
‎02-24-2013 10:56 PM
Super User
Posts: 23,778

## Re: 2-way ANOVA question

You're not referencing your datasets properly.

*Dataset TEST was created in work library;

data test;

set homewrk2.test;

/*create new variable for age*/

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

run;

*Replace dataset TEST in work library using the test dataset from library homewrk2 instead of work;

data test;

set homewrk2.test;

/*create age groups so that group 1 = children under 12 years old, group 2 = children 12 or older but younger than 20, group 3 = 20 or older*/

if age < 12 then agegrp=1;

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

else if age >=20 then agegrp=3;

run;

You should open your datasets or print them to make sure it's doing what you want.

You can correct this by using doing one datastep instead. Also, make sure in further code you refer to test rather than homework.test because they're two different datasets.

data test;

set homewrk2.test;

/*create age groups so that group 1 = children under 12 years old, group 2 = children 12 or older but younger than 20, group 3 = 20 or older*/

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;

All Replies
Solution
‎02-24-2013 10:56 PM
Super User
Posts: 23,778

## Re: 2-way ANOVA question

You're not referencing your datasets properly.

*Dataset TEST was created in work library;

data test;

set homewrk2.test;

/*create new variable for age*/

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

run;

*Replace dataset TEST in work library using the test dataset from library homewrk2 instead of work;

data test;

set homewrk2.test;

/*create age groups so that group 1 = children under 12 years old, group 2 = children 12 or older but younger than 20, group 3 = 20 or older*/

if age < 12 then agegrp=1;

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

else if age >=20 then agegrp=3;

run;

You should open your datasets or print them to make sure it's doing what you want.

You can correct this by using doing one datastep instead. Also, make sure in further code you refer to test rather than homework.test because they're two different datasets.

data test;

set homewrk2.test;

/*create age groups so that group 1 = children under 12 years old, group 2 = children 12 or older but younger than 20, group 3 = 20 or older*/

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;

Contributor
Posts: 23

## Re: 2-way ANOVA question

Thank you. This works. I am also opening the dataset along the way to be sure that everything is working correctly.

🔒 This topic is solved and locked.