sas macro

Reply
Occasional Contributor
Posts: 8

sas macro

Hi, Is there any SHORTER way of writing the below piece of code %if &dsn. = pavan and (%substr("&ym.",6,2) = 02 or                                   %substr("&ym.",6,2) = 03 or                                   %substr("&ym.",6,2) = 05 or                                   %substr("&ym.",6,2) = 06 or                                   %substr("&ym.",6,2) = 08 or                                   %substr("&ym.",6,2) = 09 or                                   %substr("&ym.",6,2) = 11 or                                   %substr("&ym.",6,2) = 12) %then %do; Here ym and dsn are macro variables.

Frequent Contributor
Posts: 117

Re: sas macro

Try IN instead of or . (not tested)

%if &dsn. = pavan and %substr("&ym.",6,2)  in (02,03,05,06,08,09,11,12) %then %do;

Trusted Advisor
Posts: 1,128

Re: sas macro

agree, we can use IN operator in the macro. However it is important to know that we cannot use the IN operator directly as we use in the datastep. Macro does not recognize the IN operator. There are options which make the macro recognize the IN operator, also we need to make the delimiter to be recognized by the macro and even this can be made recognizable by macros using the options.

there are two options which does these two things

minoperator and mindelimiter

so please try something like below in your actual code to reduce the code and to simplify it

also please try to avoid using the quotation marks for the parameters in within the macro, like %substr("&ym",6,2) , instead use only %substr(&ym,6,2). Macro recognized every character the text. so no quotations needed.

options minoperator mindelimiter=',';

%macro inoperator(dsn,ym);

  %if &dsn.=pavan and %substr(&ym,6,2) in (02,03,05,06,08,09,11,12) %then %do;

    proc print data=sashelp.class;

    run;

  %end;

%mend inoperator;

%inoperator(pavan,1022002);

Thanks,

Jag

Thanks,
Jag
Super User
Super User
Posts: 7,392

Re: sas macro

Well, I can think of several possible answers to this, but it very dependant on what you are doing outside that bit of code.  For instance, from the code given I do not see a reason why you would need to be doing this in macro code, it doesn't seem generic enough for such a thing.  Try moving it back to standard SAS, then you have full functionality.  Without the surrounding code its hard to say, but I assume you are dealing with year part of a date?

Ask a Question
Discussion stats
  • 3 replies
  • 225 views
  • 0 likes
  • 4 in conversation