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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.