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.
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;
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
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?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.