- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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);
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;