Condintional macro based upon if macro variable exists, another condition is run.

Reply
Contributor
Posts: 30

Condintional macro based upon if macro variable exists, another condition is run.

I have a macro where I want the first part of the %if statement and the last part to run conditional on a second part to prevent errors.

this is my code, what I am attempting to do in the wrong coding is in orange type, and also underlined farther in the code:

%macro shetre;

%if &numstrat>3 and if &SS4 exists do &SS4>0 %then %do;

proc surveyselect data=&dsname(where=(STRATA=4))

         method=srs n=&SS4

          out=SampleStrata4;

strata Strata; 

   run;

   %end;

 

   %mend shetre;

   %shetre;

%macro sepstrat;

proc surveyselect data=&dsname(where=(STRATA=1))

         method=srs n=&SS1

          out=SampleStrata1; 

strata Strata;

   run;

proc surveyselect data=&dsname(where=(STRATA=2))

         method=srs n=&SS2

          out=SampleStrata2;

strata Strata;

   run;

%if &numstrat>2 %then %do;

proc surveyselect data=&dsname(where=(STRATA=3))

         method=srs n=&SS3

          out=SampleStrata3;

strata Strata; 

   run;

   %end;

%if &numstrat>3 and %if &SS4 exists %then %do %if &SS4>0 %then %do;

proc surveyselect data=&dsname(where=(STRATA=4))

         method=srs n=&SS4

          out=SampleStrata4;

strata Strata; 

   run;

   %end;

   %end;

%if &numstrat>4 and &SS5>0 %then %do;

proc surveyselect data=&dsname(where=(STRATA=5))

         method=srs n=&SS5

          out=SampleStrata5;

strata Strata; 

   run;

   %end;

%if &numstrat>5 and &SS6>0 %then %do;

proc surveyselect data=&dsname(where=(STRATA=6))

         method=srs n=&SS6

          out=SampleStrata6;

strata Strata; 

   run;

   %end;

%if &numstrat>6 and &SS7>0 %then %do;

proc surveyselect data=&dsname(where=(STRATA=7))

         method=srs n=&SS7

          out=SampleStrata7;

strata Strata; 

   run;

   %end;

   %mend;

   %sepstrat;

Super Contributor
Posts: 340

Re: Condintional macro based upon if macro variable exists, another condition is run.

%Symexist is the function I think you are looking for.

SAS(R) 9.2 Macro Language: Reference

Super User
Super User
Posts: 7,992

Re: Condintional macro based upon if macro variable exists, another condition is run.

Hi,

Something like:

%if &numstrat. > 3 and %symexist(ss6) %then %do;  /* so if exist then use ss6 */

%end;

%else %do;

     %let ss6=0;

     /* Do otherwise */

%end;

Contributor
Posts: 30

Re: Condintional macro based upon if macro variable exists, another condition is run.

well, %symexist seemed tp giving the argument that it was character not numeric though it returns (1 or 0).  I will show log.

Contributor
Posts: 30

Re: Condintional macro based upon if macro variable exists, another condition is run.

1877  %macro shapla;

1878  %if &numstrat>4 and and %symexist(SS5)=1 %then %do;

1879  proc surveyselect data=&dsname(where=(STRATA=5))

1880           method=srs n=&SS5

1881            out=SampleStrata5;

1882  strata Strata;

1883     run;

1884     %end;

1885  %mend;

1886  %shapla;

MLOGIC(SHAPLA):  Beginning execution.

SYMBOLGEN:  Macro variable NUMSTRAT resolves to        4

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand

       is required. The condition was: &numstrat>4 and and %symexist(SS5)=1

ERROR: The macro SHAPLA will stop executing.

MLOGIC(SHAPLA):  Ending execution.

Super User
Super User
Posts: 7,992

Re: Condintional macro based upon if macro variable exists, another condition is run.

Well, to start with you have the word and twice in that if statement.  Second you don't need the =1 as the result is either true or false.

Contributor
Posts: 30

Re: Condintional macro based upon if macro variable exists, another condition is run.

if anyone is curious this is what I figured out...

%macro sepstrat;

proc surveyselect data=&dsname(where=(STRATA=1))

         method=srs n=&SS1

          out=SampleStrata1; 

strata Strata;

   run;

proc surveyselect data=&dsname(where=(STRATA=2))

         method=srs n=&SS2

          out=SampleStrata2;

strata Strata;

   run;

%if &numstrat>2 %then %do;

proc surveyselect data=&dsname(where=(STRATA=3))

         method=srs n=&SS3

          out=SampleStrata3;

strata Strata; 

   run;

   %end;

%if &numstrat>3 and %symexist(SS4) %then %do;

%if &SS4>0 %then %do;

proc surveyselect data=&dsname(where=(STRATA=4))

         method=srs n=&SS4

          out=SampleStrata4;

strata Strata; 

   run;

   %end;

   %end;

%if &numstrat>4 and %symexist(SS5) %then %do;

%if &SS5>0 %then %do;

proc surveyselect data=&dsname(where=(STRATA=5))

         method=srs n=&SS5

          out=SampleStrata5;

strata Strata; 

   run;

   %end;

   %end;

%if &numstrat>5 and %symexist(SS6) %then %do;

%if &SS6>0 %then %do;

proc surveyselect data=&dsname(where=(STRATA=6))

         method=srs n=&SS6

          out=SampleStrata6;

strata Strata; 

   run;

runs great...thanks everyone.

   %end;

   %end;

%if &numstrat>6 and %symexist(SS7) %then %do;

%if &SS7>0 %then %do;

proc surveyselect data=&dsname(where=(STRATA=7))

         method=srs n=&SS7

          out=SampleStrata7;

strata Strata; 

   run;

   %end;

   %end;

   %mend;

options mlogic mprint symbolgen;

%sepstrat;

Ask a Question
Discussion stats
  • 6 replies
  • 246 views
  • 0 likes
  • 3 in conversation