DATA Step, Macro, Functions and more

Can "let id2=1233 " be replaced with "let id2 in ("1233","1234","1245")

Accepted Solution Solved
Reply
Super Contributor
Posts: 336
Accepted Solution

Can "let id2=1233 " be replaced with "let id2 in ("1233","1234","1245")

%LET id1= a2222;
%let id2=b1222;


%macro wins(N) ;
DATA lib1.&id1. ;
set
  %do I= 1122 %To &N;
%if %sysfunc(exist(lib2. a&i)) 
%then %str( lib2. b_c_201&i ) ;
%end;
;
where	id2 ="&id2"

	
;


%mend wins; 
%wins(3322 );

 I have used this quite well but my current task is that id2 has three values instead of one. My question is, should I remove this macro, and type each value instead, such as: where id2 in ("1233","1234","1245")


Accepted Solutions
Solution
‎05-31-2017 03:05 PM
PROC Star
Posts: 7,356

Re: Can "let id2=1233 " be replaced with "let id2 in ("1233","1234&quo

You could use:

%let id2='James','Jane','John';
data want;
  set sashelp.class (where=(name in (&id2.)));
run;

Art, CEO, AnalystFinder.com

View solution in original post


All Replies
Solution
‎05-31-2017 03:05 PM
PROC Star
Posts: 7,356

Re: Can "let id2=1233 " be replaced with "let id2 in ("1233","1234&quo

You could use:

%let id2='James','Jane','John';
data want;
  set sashelp.class (where=(name in (&id2.)));
run;

Art, CEO, AnalystFinder.com

Trusted Advisor
Posts: 1,607

Re: Can "let id2=1233 " be replaced with "let id2 in ("1233","1234&amp

[ Edited ]

To expand on the correct answer by @art297, don't mix the data which can be variable (the values that &id2 takes on) with the data step code which is constant.

 

If the only thing that is varying is

 

"1233","1234","1245"

 

so that's all you need to include in the macro variable. The part which reads

 

where id2 in

 

is constant so it should not be put into a macro variable.

Super User
Posts: 5,072

Re: Can "let id2=1233 " be replaced with "let id2 in ("1233","1234&quo

You are already working in a macro, so you can use a %DO loop.

 

As long as the values inside the IN operator are consecutive, you can use this construct to get your WHERE statement:

 

where id2 in (   %do i=&id2 %to &id2 + 2;    "&i"  %end;  ) ;

 

The pieces in red become your WHERE statement.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 117 views
  • 2 likes
  • 4 in conversation