BookmarkSubscribeRSS Feed
Macro
Obsidian | Level 7

%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?

4 REPLIES 4
Tom
Super User Tom
Super User

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;

Scott_Mitchell
Quartz | Level 8

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;

Ksharp
Super User

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 845 views
  • 0 likes
  • 5 in conversation