BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
PJ007
Calcite | Level 5

Hi all, 

 

I have a macro variable inside a macro. The problem is that the value of this macro variable maxnum is not getting refreshed when the macro is executed again in a loop. It is still retaining the old value from the first run of the loop. 

 

%macro abc;

proc sql noprint;
select max(anumber) into :maxnum
from table1;
quit;

%mend abc;

 

%macro import;

%do I=1 %to &totobs. ;
data _null_; 
set bcf;
If obs=&i. ;

IF filetype=".csv" THEN call execute ('%abc') ;
run;

%end;
%MEND Import;
%Import;

 

Any help is greatly appreciated. 

 

Thanks! 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You seem to have posted the same code again.

Note that running %abc macro multiple times in a row will not magically cause it to do anything different if nothing has changed between the runs.

Perhaps you have over simplified your actual problem?

In that case you should make sure to use %NRSTR() when calling a macro via CALL EXECUTE() this will prevent SAS from trying to execute the macro while CALL EXECUTE() is running and instead delay the macro processor from expanding the macro until it pulls the command back from the stack.

 

call execute ('%nrstr(%abc);') ;

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

Your ABC macro has nothing in it that changes. You an run it a thousand times and it will always do the same thing.

 

PJ007
Calcite | Level 5

I am sorry, I cannot post the full code. But in the actual code, the import macro scans the directory and picks one csv file after another. 

Even though the csv file changes the value of the macro variables in the actual macro does not change, that is the issue I am facing. 

PJ007
Calcite | Level 5

%macro abc;

proc sql noprint;
select max(anumber) into :maxnum
from table1;
quit;

%mend abc;

 

%macro import;

%do I=1 %to &totobs. ;
data _null_; 
set bcf_directory;
If obs=&i. ;

IF filetype=".csv" THEN call execute ('%abc') ;
run;

%end;
%MEND Import;
%Import;

Tom
Super User Tom
Super User

You seem to have posted the same code again.

Note that running %abc macro multiple times in a row will not magically cause it to do anything different if nothing has changed between the runs.

Perhaps you have over simplified your actual problem?

In that case you should make sure to use %NRSTR() when calling a macro via CALL EXECUTE() this will prevent SAS from trying to execute the macro while CALL EXECUTE() is running and instead delay the macro processor from expanding the macro until it pulls the command back from the stack.

 

call execute ('%nrstr(%abc);') ;
PJ007
Calcite | Level 5

Thank you so so much! This worked!! 

ballardw
Super User

@PJ007 wrote:

%macro abc;

proc sql noprint;
select max(anumber) into :maxnum
from table1;
quit;

%mend abc;

 

%macro import;

%do I=1 %to &totobs. ;
data _null_; 
set bcf_directory;
If obs=&i. ;

IF filetype=".csv" THEN call execute ('%abc') ;
run;

%end;
%MEND Import;
%Import;


You might at least indicate where that "table1" data set is expected to change. You do not have anything here that reads or imports(possibly a poor choice for an automated process) a different data set. If you expect the macro %abc to get a different value then the actual table1 has to change. If there is actual import code you need to show where it might be in relationship to the call execute for %abc. There are possibly some timing problems with the stack of instructions call execute is generating. You might consider actually writing the program statements to a text file to examine if they are as expected. If the file looks good then %include that to run the code.

 

Very little code is so sensitive it can't be shared. Mask sensitive things like constants representing password, users, literal values but hiding actual program logic is not going to get answers directly to your problem.

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 6 replies
  • 2040 views
  • 1 like
  • 3 in conversation