DATA Step, Macro, Functions and more

compare a macro variable with macro variable list

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

compare a macro variable with macro variable list

%let var1=12MAY2017;

 

proc sql ;
select H_date into:var2 seperated by ',' from Holiday_list ;
quit;

 

var2 will contain values from 01JAN2017 to 31DEC2017 (holiday list for 2017)

how to see if var1 contains values in var2 ??

 

 


Accepted Solutions
Solution
‎05-15-2017 08:34 AM
PROC Star
Posts: 63

Re: compare a macro variable with macro variable list

Shubham,

So far you got a lot of good hints, but not a complete solution.

 

It is quite simple, though:

%sysfunc(indexw(&var2,&var1)) will give you a positive number if &VAR1 is present as a word in &VAR2, otherwise you will get 0.

 

So you can use it in a macro like

%if %sysfunc(indexw(&var2,&var1)) %then...

Regards,

Søren

View solution in original post


All Replies
Occasional Contributor
Posts: 10

Re: compare a macro variable with macro variable list

Use %do%while loop witth %scan function. 

It's very easy.

 

New Contributor
Posts: 3

Re: compare a macro variable with macro variable list

I want to use this as 

if &va1 contains value in &var2 then do;

-----------------------

end;

else do;

------------------------------------

end;

 

can u tell me how can i compare in an if condition ?

 

Trusted Advisor
Posts: 1,400

Re: compare a macro variable with macro variable list

[ Edited ]

Try next code:

 

data want;

  set have;

        retain holydays "&var2";  /* the list */

        if  index(holydays,"&var1") then do;

         ---- holyday action ----

       end;

      else do;

      ------

      end;

run;

 

I have edited the code to repair some typos.

New Contributor
Posts: 3

Re: compare a macro variable with macro variable list

thank you sir, it worked !

 

Super User
Posts: 17,909

Re: compare a macro variable with macro variable list

Using SYSFUNC will allow you to use the standard SAS FINDW or INDEXW functions. 

Super Contributor
Posts: 336

Re: compare a macro variable with macro variable list

Flag will be 0 if not in list, integer otherwise:

 

%Let Flag=%Index("&var2",&var1.);

.. and you have a typo in "separated".

 

Super User
Posts: 6,963

Re: compare a macro variable with macro variable list

Why use a macro variable list at all?

 

%let var1=12MAY2017;
 
%let flag = 0;

data _null_;
set Holiday_list;
if var2 = "&var1"d then call symput('flag','1');
run;

Assuming that var2 in Holiday_list is a SAS date variable (numeric with date format assigned)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎05-15-2017 08:34 AM
PROC Star
Posts: 63

Re: compare a macro variable with macro variable list

Shubham,

So far you got a lot of good hints, but not a complete solution.

 

It is quite simple, though:

%sysfunc(indexw(&var2,&var1)) will give you a positive number if &VAR1 is present as a word in &VAR2, otherwise you will get 0.

 

So you can use it in a macro like

%if %sysfunc(indexw(&var2,&var1)) %then...

Regards,

Søren

Trusted Advisor
Posts: 1,630

Re: compare a macro variable with macro variable list

You can use the %inm macro to replicate the functionality of the SAS command IN as a macro

 

The macro is explained here, it's pretty simple

https://groups.google.com/forum/#!topic/comp.soft-sys.sas/fWcSDgg11tE

Super User
Posts: 9,687

Re: compare a macro variable with macro variable list

You want put it in Data Step or A Macro ?


%let var1=12MAY2017;
 
data holiday_list;
 do h_date='01jan2017'd to '01jun2017'd ; 
  output;
 end;
run;
proc sql noprint;
select put(H_date,date9.) into:var2 separated by ',' from Holiday_list ;
quit;

data x;
 set holiday_list;
 if findw(symget('var2'),"&var1") then put 'found'; 
  else put 'not found';
run;


☑ This topic is solved.

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

Discussion stats
  • 10 replies
  • 190 views
  • 0 likes
  • 9 in conversation