BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Hi Co's,
 
Can you please suggest, what is the issue with this code, and it is unable to resolve macro variable from highlighted
 
%macro macro1(server1);
data have_&server1.;
storage_cnt=100;
run;
 
proc sql noprint;
select storage_cnt into :storage_cnt_&server1. from have_&server1.;
quit;
 
%put storage_cnt_&server1.=&&storage_cnt_&server1.;
%mend macro1;
 
%macro1(SASAPP1);
%macro1(SASAPP2);
 
DATA _null_;
total_storage=SUM(&storage_cnt_SASAPP1.,&storage_cnt_SASAPP2.);
call symput('total_storage',total_storage);
RUN;
%put total_storage=&total_storage.;
 
ERROR:
89 DATA _null_;
90 total_storage=SUM(&storage_cnt_SASAPP1.,&storage_cnt_SASAPP2.);
_
386
200
76
WARNING: Apparent symbolic reference STORAGE_CNT_SASAPP1 not resolved.
WARNING: Apparent symbolic reference STORAGE_CNT_SASAPP2 not resolved.
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Please, from now on, when there are errors in the log, show us the ENTIRE log for the macro or for the data step. Do not just show us the ERRORs.

 

The problem is the macro scope. This means that macro variables which are assigned values within a macro (in this case, inside macro %macro1) do not exist outside of this macro.

 

There are generally 3 ways to fix this:

 

  1. Use a %GLOBAL statement inside of %macro1.
  2. Do not assign the macro variables their values inside a macro
  3. Add the DATA _NULL_; step into the macro.

 

Regarding #2, this would work to replace %macro1

 

%let server=SASAPP1;
data _null_;
call symputx("storage_cnt_&server.1",100);
run;
%put &=storage_cnt_&server.1;

/* Repeat with %let serve=SASAPP2 */

 

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Please, from now on, when there are errors in the log, show us the ENTIRE log for the macro or for the data step. Do not just show us the ERRORs.

 

The problem is the macro scope. This means that macro variables which are assigned values within a macro (in this case, inside macro %macro1) do not exist outside of this macro.

 

There are generally 3 ways to fix this:

 

  1. Use a %GLOBAL statement inside of %macro1.
  2. Do not assign the macro variables their values inside a macro
  3. Add the DATA _NULL_; step into the macro.

 

Regarding #2, this would work to replace %macro1

 

%let server=SASAPP1;
data _null_;
call symputx("storage_cnt_&server.1",100);
run;
%put &=storage_cnt_&server.1;

/* Repeat with %let serve=SASAPP2 */

 

--
Paige Miller
Kurt_Bremser
Super User

Macro variable scope.

You create the macro variable in a macro, and if it does not exist in the global table before, it will be local to the macro and vanish once the macro terminates. Use the %GLOBAL statement:

%macro macro1(server1);
data have_&server1.;
storage_cnt=100;
run;

%global storage_cnt_&server1.;

proc sql noprint;
select storage_cnt into :storage_cnt_&server1. from have_&server1.;
quit;
 
%put storage_cnt_&server1.=&&storage_cnt_&server1.;
%mend macro1;
Tom
Super User Tom
Super User

Make sure the macro variables exist so they don't end up being defined as LOCAL to the macro.

 

Also do not use ancient CALL SYMPUT() method unless you really have a need to add leading or trailing spaces into the macro variable.  Use CALL SYMPUTX().  

%let storage_cnt_SASAPP1=;
%let storage_cnt_SASAPP2=;
%macro1(SASAPP1);
%macro1(SASAPP2);
 
DATA _null_;
total_storage=SUM(&storage_cnt_SASAPP1.,&storage_cnt_SASAPP2.);
call symputX('total_storage',total_storage);
RUN;
%put total_storage=&total_storage.;

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