DATA Step, Macro, Functions and more

Using IN operator with %IF %THEN %ELSE

Accepted Solution Solved
Reply
Contributor
Posts: 25
Accepted Solution

Using IN operator with %IF %THEN %ELSE

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);

 

 


Accepted Solutions
Solution
‎10-19-2017 04:08 PM
Super User
Posts: 5,518

Re: Using IN operator with %IF %THEN %ELSE

Posted in reply to kingCobra

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


All Replies
Super User
Super User
Posts: 7,997

Re: Using IN operator with %IF %THEN %ELSE

Posted in reply to kingCobra

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.

 

Contributor
Posts: 25

Re: Using IN operator with %IF %THEN %ELSE

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.

Super User
Super User
Posts: 7,997

Re: Using IN operator with %IF %THEN %ELSE

[ Edited ]
Posted in reply to kingCobra

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.

Solution
‎10-19-2017 04:08 PM
Super User
Posts: 5,518

Re: Using IN operator with %IF %THEN %ELSE

Posted in reply to kingCobra

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

 

 

 

Super User
Super User
Posts: 7,080

Re: Using IN operator with %IF %THEN %ELSE

Posted in reply to kingCobra

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;

 

☑ This topic is solved.

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

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