BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
mariko5797
Pyrite | Level 9

 

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

See the MINOPERATOR option and the MINDELIMITER option.

--
Paige Miller
Tom
Super User Tom
Super User

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 399 views
  • 1 like
  • 3 in conversation