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;
%Symexist is the function I think you are looking for.
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;
well, %symexist seemed tp giving the argument that it was character not numeric though it returns (1 or 0). I will show log.
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.
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.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.