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

I have two datasets that look like the following:

mariko5797_0-1628180256340.png

I want to perform ANOVA only if there are more than 3 subjects in each BMI category for each time period (STPER, ENDPER). In my head it would be something along the lines of

%if have.STPER > 3 (where BMI = 'Not Obese' and STAT = 'N') and have.STPER > 3 (where BMI = 'Not Obese' and STAT = 'N') and have.STPER > 3 (where BMI = 'Not Obese' and STAT = 'N') %then %do;
 proc glm data = have2;
  class BMI;
  model log(CONCEN) = BMI;
  means BMI / hovtest = levene welch;
ods output welch = welch diff = pairs; run;
/*repeat for ENDPER*/

I have very little experience with macros, so I'm not sure if what I am asking is possible.

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

You don't need full-blown macro code here.  Instead you can use the "select ... into :macrovar" facility of PROC SQL to populate a the macrovar &BMI_LIST with a comma separated list of quoted BMI levels that satisfy your requirements:

 

proc sql noprint;
  select quote(strip(bmi)) into :bmi_list separated by ','
  from have
  where stat='N' and stper>3;
quit;
%put &=bmi_list;

Then run your PROC GLM code, with the additional where statement:

proc glm data = have2;
  where bmi in (&bmi_list);
  class BMI;
  model log(CONCEN) = BMI;
  means BMI / hovtest = levene welch;
  ods output welch = welch diff = pairs;
 run;

Of course, you'd have to run this twice, once for STPER and once for ENDPER.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

You don't need full-blown macro code here.  Instead you can use the "select ... into :macrovar" facility of PROC SQL to populate a the macrovar &BMI_LIST with a comma separated list of quoted BMI levels that satisfy your requirements:

 

proc sql noprint;
  select quote(strip(bmi)) into :bmi_list separated by ','
  from have
  where stat='N' and stper>3;
quit;
%put &=bmi_list;

Then run your PROC GLM code, with the additional where statement:

proc glm data = have2;
  where bmi in (&bmi_list);
  class BMI;
  model log(CONCEN) = BMI;
  means BMI / hovtest = levene welch;
  ods output welch = welch diff = pairs;
 run;

Of course, you'd have to run this twice, once for STPER and once for ENDPER.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

Since your Have set has multiple statistics perhaps you only meant the rows where the N statistic matters?

 

Combine the Have and Have2 so that there is a NEW variable that has the N. Then use a Where in the model to select appropriate records.

Not tested code for obvious reasons:

 

Proc sort data=have2;
   by bmi;
run;
Proc sort data=have (where=(stat='N')) out=bmin;
   by bmi;
run;

data want;
   merge have2  
              bmin (rename=(stper=stper_n))
   ;
   by bmi;
run; 

Proc glm data=want;
   where stper_n > 3;
   class BMI;
<rest of glm code goes here>

You might save some minor coding by creating a summary set with only the count and use that instead of Have with all the statistics.

 

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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