BookmarkSubscribeRSS Feed
pavan_reddy
Calcite | Level 5

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.

3 REPLIES 3
Vish33
Lapis Lazuli | Level 10

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;

Jagadishkatam
Amethyst | Level 16

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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 532 views
  • 0 likes
  • 4 in conversation