Hi,
Is there a way to use IN operator with %IF similar to what we do in data step IF THEN ELSE?
So for eample I need to use
%macro xxxxx(param= );
%IF ¶m. IN (A,B,C) %THEN %DO;
.........................
%END;
%ELSE %IF ¶m. IN (E,F,G) %THEN %DO;
....................
%END;
%mend;
%xxxxxx(param=A);
%xxxxxx(param=G);
Yes, but it looks a little different. At a minimum, parentheses should not be used.
Read up on two options: minoperator and mindelimiter.
If you set the MINDELIMITER option to |, you would use:
%if ¶m. in A|B|C %then %do;
You are allowed to use a comma as the value of MINDELIMITER, but that's not a requirement. Do not leave extra spaces on either side of your delimiter.
Try:
http://support.sas.com/kb/35/591.html
However there doesn't seem to be much reason to do it this way. Your macro is just a complicated way of avoiding doing Base SAS. Post some real example code and will describe further.
RW9,
I made this simplified to facilitate understanding. The main issue is if ¶m has certain values, I want to execute certain steps. I have several of such %IF %THEN %ELSE block of codes.
Thats why I say it depends on the other code I can't see. There are two simple options:
1) Have two macros, and call one or the other based on the data:
%macro param_unit (param=,unit=); ... %mend; %macro param (param=); ... %mend; data want; set have; if param in ("A","B","C") then call execute(cats('%param_unit (param=',param,",unit=',unit,');')); else call execute(cats('%param (param=',param,');')); run;
Or you could od it in the datastep within the macro, or you could do it in datastep with no macro. Probably several other options.
Edit; Just to add, the point of macro is to create generic reusable code, hardcoding data requirements in it reduces both these.
Yes, but it looks a little different. At a minimum, parentheses should not be used.
Read up on two options: minoperator and mindelimiter.
If you set the MINDELIMITER option to |, you would use:
%if ¶m. in A|B|C %then %do;
You are allowed to use a comma as the value of MINDELIMITER, but that's not a requirement. Do not leave extra spaces on either side of your delimiter.
Look at the MINDELIMITER system option or / MINDELIMITER= option on the %MACRO statement in the on-line documentation. You should find examples there.
I have used INDEXW() function to do this and find it works well and avoids worrying about MINDELIMITER.
%IF %sysfunc(indexw(A B C,¶m)) %THEN %DO;
.........................
%END;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.