sasmacrosdo-loops

How can I conditionally process within %DO macro loop? (SAS)


I am writing a program to loop through a list of variables, run proc surveyfreq and proc surveylogistic, extract and format ODS outputs, then append these all into a final table. I am using a macro loop (truncated example below) to individually process through my variable list, run my procs and pull what I need from ODS output (the data step formatting of ODS outputs are not included, it's not necessary to my question). Everything is force-appended to a final dataset once I've merged and trimmed the surveyfreq and surveylogisitic outputs to what I need for my final table.

Here is my issue: I need to fork the program in order to run different regression models for different variables. Some by-variables I need to run with the 'First' reference group, while other's need a '2' reference group. I have tried using an %IF/%THEN/%DO statement like below, but it does not work.

Is there some way to get the %IF/%THEN statements to work with the &BYVAR macro variable that scans along my variable list? How would you solve this problem of forking the program? Appreciate any suggestions.

%let ds=dataset;

%let outcome=nonadhere;

%let wgt=rxsewt;

%let repwgt=RXSE1--RXSE100;

%let covars=age_gp65;

%let &byvars= race sex adi income diabetes heart_any;
%macro loop;
  %global byvar i;
  %do i=1 %to %sysfunc(countw(&byvars));
    %let byvar =%scan(&byvars,&i);'

proc surveyfreq data=&ds method=brr(fay=0.3);       
        title 'Weighted / Unweighted n';    
    weight &wgt;    
    repweight &repwgt;  
    table &byvar*&outcome / row chisq ;
    ods output  CrossTabs = _ctab;
run;
title;

BUNCH OF DATA STEP PROCESSING HERE

%if &byvar in(race, sex, adi, income) %then %do;

PROC SURVEYLOGISTIC data=&ds method=brr(fay=0.30);
    weight &wgt;    
    repweight &repwgt;
    CLASS &byvar(Ref=first) &covars &outcome;
    MODEL &outcome (event='1')= &byvar &covars;
    ODS OUTPUT ParameterEstimates =_pe OddsRatios =_or;
RUN;

BUNCH OF DATA STEP PROCESSING HERE

%else %if &byvar in(diabetes, heart_any) %then %do;

PROC SURVEYLOGISTIC data=&ds method=brr(fay=0.30);
    weight &wgt;    
    repweight &repwgt;
    CLASS &byvar(Ref='2') &covars &outcome;
    MODEL &outcome (event='1')= &byvar &covars;
    ODS OUTPUT ParameterEstimates =_pe OddsRatios =_or;
RUN;

BUNCH OF DATA STEP PROCESSING HERE

%end;

/**Append to final dataset**/
proc append
    base=final
    data=output_clean force;
run;

  %end;
%mend;

%loop;

I've described everything in the 'details' text box.


Solution

  • You are very close. You need to enable the in operator for the macro with the minoperator and mindelimiter= options.

    %macro loop / minoperator mindelimiter=',';
    ...
    
    %if &byvar in(race, sex, adi, income) %then %do;
    ...
    
    %else %if &byvar in(diabetes, heart_any) %then %do;
    ...
    
    %mend;