BookmarkSubscribeRSS Feed
Peter_Y
Calcite | Level 5

Hello all:

  I need a macro with the functionality to accept both numeric and list as parameters. What I need is sth. like

%macro check(value, var);

   %if &value is list %then %do ......

   %if &value is a single number %then %do ......

%mend;

so I can call the macro function either

%check(10,measure);

%check((1,2,3,4,5), measure);

Any suggestion on this?

Thanks,

Peter

5 REPLIES 5
Haikuo
Onyx | Level 15

Maybe I took your question too simple, isn't just adding a quote?

%macro test (VALUE,measure);

  %if &value="1,2,3" %then %put &MEASURE.;

%mend;

%test("1,2,3",SUCCESS!);

Haikuo

stat_sas
Ammonite | Level 13

This is my try

data have;

do measure=1 to 10;

output;

end;

run;

%macro check(value, var);

proc means data=have (where=(&var in (&value)));

   var &var;

run;

%mend;

%check (10,measure);

%check (7 8 9,measure);

Ksharp
Super User

You can use IN in macro . details check documentation. I can't remember too much.

%macro check(value, list)/mindelimiter=, ;

   %if &value in &list %then %do ......

Tom
Super User Tom
Super User

Just use spaces as the delimiter in the value of the parameter.  If you need commas in the code that the macro is generating you always put them in later.


%check(10,measure);

%check(1 2 3 4 5, measure);

%macro check(value, var);

   %if %sysfunc(countw(&value,%str( )) > 1 %then %do; ... %end;

   %else %do;  ...... %end;

%mend;




RW9
Diamond | Level 26 RW9
Diamond | Level 26

Just to note on the above very good examples, there is one element of macro functionality which does come up much.  It is described in this article: http://www.lexjansen.com/pharmasug/2006/technicaltechniques/tt28.pdf

So you could change your macro slightly to:

%macro check (var) / pbuff;

Then scan the syspbuff variable.  Just another option.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 1067 views
  • 0 likes
  • 6 in conversation