DATA Step, Macro, Functions and more

MACRO with %IF (...) IN (...)

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

MACRO with %IF (...) IN (...)

Hello, my macro doesn't work the way I want to. So basically this below works perfectly:

 

%MACRO mymacro() ;
%IF %substr(&n50prc0_aaaamm.,5,2) = 06
%THEN %DO;
[...]

 

However I would need something like this because I want every trimester to work

 

%MACRO mymacro() ;
%IF %substr(&n50prc0_aaaamm.,5,2) in (03,06,09,12)
%THEN %DO;
[...]

 

I tried many different things including different options like MINDELIMITER but nothing seems to work for me. Thank you for your help 


Accepted Solutions
Solution
‎11-08-2017 05:00 PM
Super User
Posts: 6,648

Re: MACRO with %IF (...) IN (...)

Macro language does support an IN operator.  As @collinelliot indicated, you need two options properly set, plus the matching syntax in your code.  Tedious, but it is possible.  (For example, get rid of the parentheses around the list of values.)

 

An easy workaround for this case might (depending on the range of values) be:

 

%let two_digits = %substr(&n50prc0_aaaamm.,5,2);

%if &two_digits = &two_digits / 3 * 3 %then %do;

 

This statement would apply integer arithmetic (truncating remainders).

View solution in original post


All Replies
PROC Star
Posts: 311

Re: MACRO with %IF (...) IN (...)

Are you using the "MINOPERATOR" option?

Contributor
Posts: 26

Re: MACRO with %IF (...) IN (...)

Posted in reply to collinelliot

Yes, with this

 

options MINOPERATOR;
%MACRO mymacro / MINDELIMITER=",";
%IF %substr(&n50prc0_aaaamm.,5,2) in (03,06,09,12)
%THEN %DO;
[...]

 

I get this error:


ERROR 13-12: Unrecognized SAS option name, MINOPERATOR.

2 %MACRO RAPPEL_PROD / MINDELIMITER=",";
ERROR: Extraneous information on %MACRO statement ignored.
ERROR: A dummy macro will be compiled.
3

Super User
Posts: 23,370

Re: MACRO with %IF (...) IN (...)

You need  single quotes in the MINDELIMITER

 

This works for me:

%MACRO myMacro(n50prc0_aaaamm=) / mindelimiter=',' minoperator ;
%IF %substr(&n50prc0_aaaamm.,5,2) in (03,06,09,12)
%THEN %DO;

%PUT &n50prc0_aaaamm. -> MATCH;

%END;
%ELSE %DO;
%PUT &n50prc0_aaaamm. -> NO MATCH;

%END;

%MEND myMacro;

%mymacro(n50prc0_aaaamm=199501);
%mymacro(n50prc0_aaaamm=199502);
%mymacro(n50prc0_aaaamm=199503);
%mymacro(n50prc0_aaaamm=199504);
%mymacro(n50prc0_aaaamm=199505);
%mymacro(n50prc0_aaaamm=199506);
%mymacro(n50prc0_aaaamm=199509);
%mymacro(n50prc0_aaaamm=199512);
%mymacro(n50prc0_aaaamm=199513);
Contributor
Posts: 26

Re: MACRO with %IF (...) IN (...)

thank you, but it doesn't for me. I should mention that I run this SAS code via a secure shell. My first code does work though, but I don't think the options are recognized

Super User
Posts: 23,370

Re: MACRO with %IF (...) IN (...)

@x2PSx then likely Tom has the correct idea, you're working on an older version of SAS that doesn't support MINDELIMITER - which would be pretty old. Did you verify the SAS version? Or are you using WPS? That doesn't work the same either.

Contributor
Posts: 26

Re: MACRO with %IF (...) IN (...)

I work with SAS 9.3 TS Level 1M1 W32_7PRO

Super User
Posts: 23,370

Re: MACRO with %IF (...) IN (...)

MINDELIMITER has been available as of SAS 9.2+

Solution
‎11-08-2017 05:00 PM
Super User
Posts: 6,648

Re: MACRO with %IF (...) IN (...)

Macro language does support an IN operator.  As @collinelliot indicated, you need two options properly set, plus the matching syntax in your code.  Tedious, but it is possible.  (For example, get rid of the parentheses around the list of values.)

 

An easy workaround for this case might (depending on the range of values) be:

 

%let two_digits = %substr(&n50prc0_aaaamm.,5,2);

%if &two_digits = &two_digits / 3 * 3 %then %do;

 

This statement would apply integer arithmetic (truncating remainders).

Contributor
Posts: 26

Re: MACRO with %IF (...) IN (...)

Posted in reply to Astounding

That is a very nice shortcut indeed for my specific problem, thank you

Super User
Super User
Posts: 7,948

Re: MACRO with %IF (...) IN (...)

What version of SAS are you using?

791  %put &sysver ;
9.4

You could use a little arithmetic.  Try this little test.

%macro test;
  %do m=1 %to 12 ;
    %let mm=%sysfunc(putn(&m,z2));
    %let mod4=%sysfunc(mod(&mm,4));
    %put &=m &=mm &=mod4 ;
    %if not %sysfunc(mod(&mm,4)) %then %put Found end of quarter ;
  %end;
%mend ;
%test;

 

☑ This topic is solved.

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

Discussion stats
  • 10 replies
  • 229 views
  • 0 likes
  • 5 in conversation