BookmarkSubscribeRSS Feed
rnmishra
Calcite | Level 5

I have a number of macro programs that take different data sets for processing.

For example,

%macro Abc; /* Takes data set abc*/

..............

.............

%Mend Abc;

This way I have created a number of macro programs.

Now I need to execute macro program 'Abc' if data set Abc exists,

%Macro  Action(Dsn);

%IF  %SYSFUNC(EXIST(Dsn))>0  %THEN %DO;

        %(&Dsn);  /*Here is the problem*/

%END;

%ELSE %DO;

%PUT***********Dataset &Dsn does not exist;

%END;

%MEND Action;

Now if I use

%Action(Abc);     The program fails to execute because  in %(&Dsn) , &Dsn fails to resolve so %Abc does not execute.

Is there a way to fix this?

Thanks every one for your attention.

Raghu.


3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You have brackets around the &dsn., the below works (I note you also miss the ampersand on the exists function):

%macro abc;
  %put hello;
%mend abc;

%macro action (dsn);
  %&dsn.;
%mend action;

%action(abc);

Although I would suggest there are better ways of doing something like this than having a macro per dataset, even creating one macro which accepts the dataset as a parameter:

%macro do_something (dset=);

     ...

%mend;

%do_something(dset=abc);

Or alternatively create a loop dataset and then use that to generate code with call execute.

Reeza
Super User

This sounds like a bad design.

Perhaps explain more about what you're trying to do and someone can suggest better design techniques.

Tom
Super User Tom
Super User

That is because the code the macro generated:  %(abc) is not valid syntax for a macro call.  You want it to generate %abc; or %abc() .

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