DATA Step, Macro, Functions and more

How to test if a list in macro var2 is in a list in macro var1?

Reply
Contributor
Posts: 50

How to test if a list in macro var2 is in a list in macro var1?

%let varlist1 =a b c d e f;

%let varlist2 = c d e;

How to use macro or function to test if var in varlist2 are in varlist1?

Say, I can use %scan() function and do loop to test, but I don't know if there is a macor counterpart of 'in' of sas base?

e.g, "%scan(&varlist2,1)"  in &varlist1, but in SAS base, &varlist1 should be quoted for each element of &varlist2, how can I make it quoted so that I can use SAS base in operator?

Or alternatively is there other way to achieve this without sas base 'in' operator?

Super User
Posts: 19,770

Re: How to test if a list in macro var2 is in a list in macro var1?

Does order matter?

Super User
Super User
Posts: 7,039

Re: How to test if a list in macro var2 is in a list in macro var1?

Do you want to test each value in VARLIST2 are in VARLIST1?

%let found=0;

%do i=1 %to %sysfunc(countw(&varlist2)) ;

   %if %sysfunc(indexw(&varlist1,%scan(&varlist1,&i))) %then %let found=1;

%end;

Super Contributor
Posts: 297

Re: How to test if a list in macro var2 is in a list in macro var1?

If you have 9.2 onwards you can using the IN operator.

I have an older version so the following has not been tested.

%LET VARLIST1 = A B C D E F;
%LET VARLIST2 = C D E;

%MACRO TEST;
%DO I = 1 %TO %LENGTH(%SYSFUNC(COMPRESS(&VARLIST2.)));
%IF %SCAN(&VARLIST2,&I.) IN &VARLIST1. %THEN %DO
  %LET EXIST&I. = Y;
%END;
%END;
%MEND;

%TEST;

Super User
Posts: 10,018

Re: How to test if a list in macro var2 is in a list in macro var1?

Does the order matter ?

%LET VARLIST1 = A B C D E F;
%LET VARLIST2 = C D E;

data _null_;
 if find(symget('VARLIST1'),symget('VARLIST2')) then putlog "Found" ;
  else putlog "Not Found";
  stop;
run;

Xia Keshan

Ask a Question
Discussion stats
  • 4 replies
  • 212 views
  • 0 likes
  • 5 in conversation