BookmarkSubscribeRSS Feed
Anupama29
Calcite | Level 5

Can we have a sas code to get the list of files that sas code is referring to while executing. For Eg: the macros it is referring or the files that has been included in the sas code through out the flow.

1 REPLY 1
SASJedi
Ammonite | Level 13

Here's a starting point for you. This data step reads a SAS dataset file, finds macro calls and %INCLUDE statements, and creates a dataset that includes the program name and the macros calls. 

%let myProgram=C:\temp\test.txt;
data calls;
   infile "%superq(myProgram)" end=lastrec;
   length Program $256 MacroName $32;
   retain rxID;
   retain Program "%qscan(%superq(myProgram),-1,\/)";
   drop rxID count;
   if _n_=1 then do;
      rxID=prxparse('/(%(?!(macro|mend))\s*\w*)/i');
   end;
   input;
   if prxmatch(rxID,_infile_) then do;
      MacroName=prxposn(rxID,1,_infile_);
      output;
      count+1;
   end;
   if lastrec and count=0 then do;
      put 'NOTE- ************************************************';
      put 'NOTE: No macro calls or %INCLUDEs in program ' program ;
      put 'NOTE- ************************************************' ;
   end;
run;
proc sql;
select distinct program, macroName 
   from calls
;
quit;

Here are a couple of test "programs" to play with. Save them as test.sas and test2.sas

/* test.sas */
%macro include;
%mend myMacro;
%myMacro
%include "c:\temp";
%include fileref;
print "This is a %test";
/*test2.sas */
macro include;
mend myMacro;
myMacrol
include "c:\temp";
include fileref;
print "This is a test";

When executed against test.sas, this is the result:

Program MacroName
test.sas %include
test.sas %myMacro
test.sas %test

May the SAS be with you!

Mark

Check out my Jedi SAS Tricks for SAS Users

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 790 views
  • 0 likes
  • 2 in conversation