My code is similar to below. I have a lot of different datasets and %if &dsn in (CMD DEM DV2) doesn't seem to work. Is there a different command that does the same thing?
%macro Counts(dsn, varlist);
data &dsn.1;
set an.&dsn_an;
%if &dsn= CMD|&dsn= DEM|&dsn= DV2 %then %do;
where protseg in ('A' 'B');
%end;
array vars{*} &varlist.;
num=0; den=0;
do i=1 to dim(vars);
if vars{i} in ("a" "d" "g" "j") then den = sum(den, 1); /*denominator*/
if vars{i} in ("j") then num = sum(num, 1); /*numerator*/
end;
run;
%mend;
If you want the macro processor to treat IN as an operator you need to tell it that with the MINOPERATOR option.
But if you are going to use IN as an operator inside a macro definition then it is best to set that option on %MACRO statement instead of depending on the whims of the user that is calling the macro.
%macro Counts(dsn, varlist) / minoperator mindelimter=' ';
data &dsn.1;
set an.&dsn_an;
%if &dsn in CMD DEM DV2 %then %do;
where protseg in ('A' 'B');
%end;
array vars{*} &varlist.;
num=0; den=0;
do i=1 to dim(vars);
/*denominator*/
if vars{i} in ("a" "d" "g" "j") then den = sum(den, 1);
/*numerator*/
if vars{i} in ("j") then num = sum(num, 1);
end;
run;
%mend Counts;
PS Make it easier to read the code by placing comments about what the code is going to do BEFORE the code instead of afterwords.
See the MINOPERATOR option and the MINDELIMITER option.
If you want the macro processor to treat IN as an operator you need to tell it that with the MINOPERATOR option.
But if you are going to use IN as an operator inside a macro definition then it is best to set that option on %MACRO statement instead of depending on the whims of the user that is calling the macro.
%macro Counts(dsn, varlist) / minoperator mindelimter=' ';
data &dsn.1;
set an.&dsn_an;
%if &dsn in CMD DEM DV2 %then %do;
where protseg in ('A' 'B');
%end;
array vars{*} &varlist.;
num=0; den=0;
do i=1 to dim(vars);
/*denominator*/
if vars{i} in ("a" "d" "g" "j") then den = sum(den, 1);
/*numerator*/
if vars{i} in ("j") then num = sum(num, 1);
end;
run;
%mend Counts;
PS Make it easier to read the code by placing comments about what the code is going to do BEFORE the code instead of afterwords.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.