BookmarkSubscribeRSS Feed
zscott1
Calcite | Level 5

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;

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

zscott1
Calcite | Level 5

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

zscott1
Calcite | Level 5

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

zscott1
Calcite | Level 5

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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1657 views
  • 0 likes
  • 3 in conversation