BookmarkSubscribeRSS Feed
Tom
Super User Tom
Super User

@Shmuel wrote:

@Tom may I ask, suppose you have a macro program in a macro program:

/* %let choice = xxx; -- either exists as global or does not exits */

%macro main;
   %local choise;
    ...
    %called(....);
   ...
%mend;

%macro called(...arguments ...);
    %global choice;
     %if  %symexist(choice) %then ... ;
%mend;

%main;
      

in such case which macro variable CHOICE shall %symexist() check - the %local or the %global ?


So that is the combination will generate an error message. When the sub-macro CALLED starts and tries to create a global symbol named CHOICE it will issue an error message since CHOICE already exists in the symbol table for the MAIN macro.

 

We are essentially trying to deal with what I call "external" macro variables.  They are not defined as parameters to the macro or defined as local macro variables that the macro uses for its own purpose.  The reference to them just seems to appear in the code and how they are supposed to have ever gotten assigned a value is magic of some type. 

 

Here is the general pattern I would follow.

1) Test if the macro variable exists or not and then create it in the appropriate scope.

2) Test if the macro variable is populated or not and if appropriate assign it a default value (or take appropriate action for when the value is empty.).

%macro called(...arguments ...);
    %if not %symexist(choice) %then %local choice;
    %if 0=%length(%superq(choice)) %then ... ;
     ...
%mend;

 

Shmuel
Garnet | Level 18

Thanks @Tom , you absolutely clarified the point.

DanD999
Quartz | Level 8

Thanks to everyone for their help. I wasn't able to get the code to work like I wanted so I changed other parts of the code to solve the problem. 

 

Thanks again

Prashanth_J
Calcite | Level 5

Hey..

 

It pretty much simple.

 

data check_var;
dsname=open("sashelp.class");
varname_=varnum(dsname,'AGE');
if varname_ >0 then flag="Y"; else flag="N";
/* if flag="Y";*/
run;

 

** If you want to create a macro then use the below code .

data _NULL_;
call symputx("Checkv", varnum(open("sashelp.class"),'AGE'));
run;
%put &Checkv.;

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