DATA Step, Macro, Functions and more

How to determine what folder is the SAS macro stored in

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

How to determine what folder is the SAS macro stored in

I have a program (it was developed by my colleagues) with 20 paths in SASAUTOS. So, when I see calling of some macro in the code I can't easily determine what folder the macro is stored in. Is there some function for this purpose or system table with names and physical paths to macroes which can be used in current SAS session?


Accepted Solutions
Solution
‎04-17-2017 05:30 AM
Super User
Super User
Posts: 7,042

Re: How to determine what folder is the SAS macro stored in

Posted in reply to DmitryErshov

Are you asking how to analyze the code and when you see a macro call figure out which file has the source code for that macro?

If you know the SASAUTOS settings then just loop over the folders in that list and look for the file that the right name.

So for example this program will read a list of macro names from input stream and generate a dataset with the the location.

This one should find the source files for SAS autocall macros LEFT and VERIFY.

 

data search ;
  attrib MACRO length=$32  label='Macro name';
  attrib FOUND length=3    label='Found? (0/1)';
  attrib FILE  length=$36  label='Filename';
  attrib DNAME length=$200 label='Directory name';
  keep macro found file dname;

  if _n_=1 then do;
*----------------------------------------------------------------------;
* Get SASAUTOS option into string variable and copy individual paths ;
* into DLIST, expanding any filerefs that are found (such as SASAUTOS).;
*----------------------------------------------------------------------;
    length dlist autos path $32767 ;
    retain dlist ;
    autos=getoption('sasautos');
    do i=1 by 1 until (path= ' ');
      path=scan(autos,i,'( )');
      if length(path) <= 8 then do;
        if 0=fileref(path) then path=pathname(path) ;
      end;
      dlist=catx(' ',dlist,path);
    end;
  end;
  input macro ;
  file=lowcase(trim(macro))||'.sas';

*----------------------------------------------------------------------;
* Scan through the paths in DLIST until file found or end of list ;
*----------------------------------------------------------------------;
  found=0;
  done=0;
  do i=1 by 1 until (found or done);
    dname=dequote(scan(dlist,i,'( )'));
    if dname=' ' then done=1;
    else if fileexist(catx('/',dname,file)) then found=1;
  end;
  if ^found then dname=' ';
cards;
left
verify
;
proc print; run;

Note this will not work if you have defined more than one macro in each source file. That is something that you should not do with an autocall library, but even SAS sometimes does to make distribution easier.  If you have that situation then might need to use a search tool like GREP to search the source for the %MACRO statements.

View solution in original post


All Replies
Super Contributor
Posts: 266

Re: How to determine what folder is the SAS macro stored in

Posted in reply to DmitryErshov

Use option mlogic and then check the logs. Log would tell you the path from where this macro being called.

Contributor
Posts: 23

Re: How to determine what folder is the SAS macro stored in

Thanks for reply, RahulG.

But I don't want the macro to be executed. Is there some way to prevent macro execution?
Super User
Posts: 5,503

Re: How to determine what folder is the SAS macro stored in

Posted in reply to DmitryErshov

In theory, what you are asking for is not 100% possible.  It is conceivable that multiple folders contain a definition for the same macro (possibly the same definition, possibly a different definition).  The one that gets used would depend on which macros have already been defined within the session (whether hard-coded, or by %include, or by invoking the macro with SASAUTOS), as well as the order of the folders within the SASAUTOS option.  Also note, it is possible for the SASAUTOS definition to change as the program proceeds.

 

For just a basic approach, it shouldn't be too difficult to automate:

 

  • Get a list of all folders defined within SASAUTOS
  • For each such folder, get a list of the files within
Solution
‎04-17-2017 05:30 AM
Super User
Super User
Posts: 7,042

Re: How to determine what folder is the SAS macro stored in

Posted in reply to DmitryErshov

Are you asking how to analyze the code and when you see a macro call figure out which file has the source code for that macro?

If you know the SASAUTOS settings then just loop over the folders in that list and look for the file that the right name.

So for example this program will read a list of macro names from input stream and generate a dataset with the the location.

This one should find the source files for SAS autocall macros LEFT and VERIFY.

 

data search ;
  attrib MACRO length=$32  label='Macro name';
  attrib FOUND length=3    label='Found? (0/1)';
  attrib FILE  length=$36  label='Filename';
  attrib DNAME length=$200 label='Directory name';
  keep macro found file dname;

  if _n_=1 then do;
*----------------------------------------------------------------------;
* Get SASAUTOS option into string variable and copy individual paths ;
* into DLIST, expanding any filerefs that are found (such as SASAUTOS).;
*----------------------------------------------------------------------;
    length dlist autos path $32767 ;
    retain dlist ;
    autos=getoption('sasautos');
    do i=1 by 1 until (path= ' ');
      path=scan(autos,i,'( )');
      if length(path) <= 8 then do;
        if 0=fileref(path) then path=pathname(path) ;
      end;
      dlist=catx(' ',dlist,path);
    end;
  end;
  input macro ;
  file=lowcase(trim(macro))||'.sas';

*----------------------------------------------------------------------;
* Scan through the paths in DLIST until file found or end of list ;
*----------------------------------------------------------------------;
  found=0;
  done=0;
  do i=1 by 1 until (found or done);
    dname=dequote(scan(dlist,i,'( )'));
    if dname=' ' then done=1;
    else if fileexist(catx('/',dname,file)) then found=1;
  end;
  if ^found then dname=' ';
cards;
left
verify
;
proc print; run;

Note this will not work if you have defined more than one macro in each source file. That is something that you should not do with an autocall library, but even SAS sometimes does to make distribution easier.  If you have that situation then might need to use a search tool like GREP to search the source for the %MACRO statements.

Super User
Posts: 10,023

Re: How to determine what folder is the SAS macro stored in

Posted in reply to DmitryErshov

Check dictionary table.

 

proc sql;
select *
 from dictionary.macros ;
quit;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 436 views
  • 4 likes
  • 5 in conversation