BookmarkSubscribeRSS Feed
Am8
Calcite | Level 5 Am8
Calcite | Level 5

I am trying to call macro from data set which has attributes of files. 

macro load(fn,tblnm,head);

proc import data file=“&fn.” out=fl_data dbms=dlm repack;

delimiter=‘,’;

getnames= yes;

guessingrows=max;

quit;

 

proc sql;

select name into :varstr separated by ‘,’

from dictionary.columns

where libname = ‘WORK’ and memname= ‘FL_DATA’;

 

quit;

%if “&head.” eq “&varstr.” %then %do;

   *call another macro;

    %end;

 

%mend;

 

data _null_;

set fl_attr;

call execute(‘fl_load(‘||path||’,’||tbl_name||’,’||header||’)’);

run;

 

when I execute I get varstr not resolved. I would appreciate if you have any suggestions or what am I doing wrong 

 

 

3 REPLIES 3
Tom
Super User Tom
Super User

There is a timing issue when using CALL EXECUTE() to run a macro call that generates macro variables with SAS code that the macro then uses to control future code.  This is caused by the macro executing when CALL EXECUTE() pushes the code onto a stack to run after the data step.  The macro logic runs, but the actual SAS code it generates is waiting in that stack to run after the current datastep ends.  You can see this be the lines with + at the front that CALL EXECUTE() displays in the log. Instead of the macro call you see the lines of code that the macro generated.

 

The solution is to delay the actual execution of the macro until it is retrieved to be run after the data step. You can do this by wrapping the %MACRO_NAME inside of the %NRSTR() macro function.

data _null_;
  set fl_attr;
  call execute(cats('%nrstr(%fl_load)(',path,',',tbl_name,',',header,')'));
run;

 

This will also make the SAS log much easier to read.

Am8
Calcite | Level 5 Am8
Calcite | Level 5

Thank you Tom. 

That worked.

 

 

happy Christmas 

PaigeMiller
Diamond | Level 26
%if “&head.” eq “&varstr.” %then %do;
   *call another macro;
%end;

Please note that the way you are creating &varstr, there are likely multiple values in &varstr, while &head. likely contains a single value, and so this %IF command will always fail. Furthermore, you almost never want the values of macro variables to be enclosed in quotes or double quotes to do %IF comparisons.

 

Also please note the "curly" quotes here, indicating you pasted the SAS code into Microsoft Word or PowerPoint at somewhere along the way, SAS will never recognize these as actual quotes anyway, so SAS code in Microsoft Word or PowerPoint is never a good thing to do. So two mistakes here are fixed via

 

%if &head. eq &varstr. %then %do;
   *call another macro;
%end;

but even that fails because &varstr likely contains multiple variable names ... and I'm not really sure what you are trying to do with this portion of code anyway ... so I'm not sure how to fix this.

--
Paige Miller

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 967 views
  • 0 likes
  • 3 in conversation