BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kingCobra
Obsidian | Level 7

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 &param. IN (A,B,C) %THEN %DO;

        .........................

   %END;

   %ELSE %IF &param. IN (E,F,G) %THEN %DO;

       ....................

   %END;

%mend;

%xxxxxx(param=A);

%xxxxxx(param=G);

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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 &param. 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.

 

http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#n0vc5y2nyi55dvn1a...

 

http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#p0pbehl7wj5sl4n1o...

 

 

 

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

kingCobra
Obsidian | Level 7

RW9,

 

I made this simplified to facilitate understanding. The main issue is if &param has certain values, I want to execute certain steps. I have several of such %IF %THEN %ELSE block of codes.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Astounding
PROC Star

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 &param. 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.

 

http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#n0vc5y2nyi55dvn1a...

 

http://support.sas.com/documentation/cdl/en/mcrolref/69726/HTML/default/viewer.htm#p0pbehl7wj5sl4n1o...

 

 

 

Tom
Super User Tom
Super User

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,&param)) %THEN %DO;
        .........................
%END;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 6505 views
  • 2 likes
  • 4 in conversation