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

I have a variety of macros stored in a directory structure on my hard-drive.  In my autoexec.sas I have the line:

SASAUTOS = ( "F:\_macros\util" "F:\_macros\rprt" "F:\_macros\plot" SASAUTOS);

I would like to test for the presence of certain macros located within any of the folders found in my SASAUTOS statement.  I have used %sysmacexist, but it searches the work macro catalog and I want to search for a macro that hasn't yet been compiled.  My justification for doing this is to break apart a complex macro into many smaller ones.  However, in doing so, I have to make sure the users have these smaller macro dependencies installed on their systems.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You just need to work a little harder.

Here is a program I have been using to find location of compiled macros.

I commented out the part that read the list of compiled macros from dictionary metadata and replaced it with inline card images instead.  You could modify it to reference a source dataset or even just pass the macro name as the value of a macro variable if you want.

data maclist ;

  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;

*----------------------------------------------------------------------;

* Get SASAUTOS option into D(1) temporary string variable and copy into;

* D(2) expanding any filerefs such as SASAUTOS ;

*----------------------------------------------------------------------;

  array d(3) $32767 _temporary_;

  if _n_=1 then do;

    d(1)=getoption('sasautos') ;

    do i=1 by 1 until(d(3)= ' ');

      d(3)=scan(d(1),i,'( )','q');

      if length(d(3))<=8 then if 0=fileref(d(3)) then

        d(3)=pathname(d(3))

      ;

      d(2)=catx(' ',d(2),d(3));

    end;

  end;

/*

  set sashelp.vcatalg;

  where libname='WORK' and memname='SASMACR'

    and memtype='CATALOG' and objtype='MACRO';

  macro=objname;

*/

  input macro ;

  file=lowcase(trim(macro))||'.sas';

*----------------------------------------------------------------------;

* Scan through the paths in SASAUTOS until file found or end of list ;

*----------------------------------------------------------------------;

  found=0;

  done=0;

  do i=1 by 1 until (found or done);

    dname=dequote(scan(d(2),i,'( )','q'));

    if dname=' ' then done=1;

    else if fileexist(catx('/',dname,file)) then found=1;

  end;

  if ^found then dname=' ';

cards;

init

rtrim

trim

run;

View solution in original post

15 REPLIES 15
ballardw
Super User

The function FILEEXIST may be helpful. Find some way step through your folder names, a DO loop should work, and concatenate the macro name .

re = fileexist (cats("F:\_macros\util","\",macroname,".sas");

in a data step.

Tom
Super User Tom
Super User

You can use the optional last parameter to the FILENAME function to help.

First you need to convert your SASAUTOS option setting into a valid list of pathnames to use for a FILENAME statement or function call.

If your setting for SASAUTOS is more complex or the path returned by the PATHNAME('sasautos') call is more complex you might have to adjust this code.

This will test if the file "trim.sas" is in the SASAUTOS search path.

%let macname=trim;

data _null_;

  length fileref1 fileref2 $8 ;

  length pathlist $32000 ;

  pathlist=getoption('sasautos');

  pathlist=tranwrd(pathlist,'sasautos',pathname('sasautos'));

  fileref1='_autos_';

  rc1 = filename(fileref1,pathlist);

  rc2 = filename(fileref2,"%lowcase(&macname).sas",,,fileref1);

  fid = fopen(fileref2,'I');

  put rc1= rc2=  fid= ;

  if fid then put "Found &macname";

  else put "NOT found &macname";

  if fid then rc=fclose(fid);

  if not rc2 then rc2=filename(fileref2);

  if not rc1 then rc1=filename(fileref1);

run;

Peter_C
Rhodochrosite | Level 12

Hi Tom

I could only get this to work by removing the SASAUTOS fileref from PATHLIST

Before I do that, the RC1 value returned = 20017

This translates by sysrc.sas like

  %let _SEINVPN =   20017; /* physical name is invalid               */

implying an invalid path is tolerated by the SAS Autocall routines, but not by the FILENAME() function

Automating the "cleanup" looks tedious - unless you know another useful idea.

peterC

Tom
Super User Tom
Super User

You just need to work a little harder.

Here is a program I have been using to find location of compiled macros.

I commented out the part that read the list of compiled macros from dictionary metadata and replaced it with inline card images instead.  You could modify it to reference a source dataset or even just pass the macro name as the value of a macro variable if you want.

data maclist ;

  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;

*----------------------------------------------------------------------;

* Get SASAUTOS option into D(1) temporary string variable and copy into;

* D(2) expanding any filerefs such as SASAUTOS ;

*----------------------------------------------------------------------;

  array d(3) $32767 _temporary_;

  if _n_=1 then do;

    d(1)=getoption('sasautos') ;

    do i=1 by 1 until(d(3)= ' ');

      d(3)=scan(d(1),i,'( )','q');

      if length(d(3))<=8 then if 0=fileref(d(3)) then

        d(3)=pathname(d(3))

      ;

      d(2)=catx(' ',d(2),d(3));

    end;

  end;

/*

  set sashelp.vcatalg;

  where libname='WORK' and memname='SASMACR'

    and memtype='CATALOG' and objtype='MACRO';

  macro=objname;

*/

  input macro ;

  file=lowcase(trim(macro))||'.sas';

*----------------------------------------------------------------------;

* Scan through the paths in SASAUTOS until file found or end of list ;

*----------------------------------------------------------------------;

  found=0;

  done=0;

  do i=1 by 1 until (found or done);

    dname=dequote(scan(d(2),i,'( )','q'));

    if dname=' ' then done=1;

    else if fileexist(catx('/',dname,file)) then found=1;

  end;

  if ^found then dname=' ';

cards;

init

rtrim

trim

run;

Peter_C
Rhodochrosite | Level 12

many thanks Tom

regards

peter

jakarman
Barite | Level 11

A complete different approach could be to use what SAS already has.

Suppose:

- your macro's are having a description/help option just compling and doing nothing

- SAS version is like 9.3 having mautlocdisplay (9.2) mautolocindes (9.3) options SAS(R) 9.3 Macro Language: Reference

Solution:

a/ set options accordingly  mautlocdisplay (mautolocindes)

b/ do a call of your macros with the description option

---->-- ja karman --<-----
Marty
Calcite | Level 5

Hi Jaap,

Thank you for pointing out the SAS options related to macro source location. I have found mautocomploc option, which writes the macro location to the log as it is compiling. Do  you know if there is a corresponding macro variable that would contain the location, so that I could refer to it programmatically?

Or would I need to use the option mautolocindes and then use proc catalog to see the path names of macros that have been compiled/invoked in the program?

I am running programs in batch mode, using Version 9.3 (TS1M1).

Marty Porter

Peter_C
Rhodochrosite | Level 12

Could this solution deal with macro source files containing more than one macro

- my problem?

I want to build a register linking macros to .sas programs. Then I shall "know" all the macros "out there". Sometimes the method might fail because a macro invocation might be named dynamically (%&macroName), but I cannot believe a macro definition could be named that way.

With a way of addressing all sas code ( piping dir /b /s \*.sas or find / -name '*.sas' ) a data step could (perhaps) provide a register of all macro invocation.

I could use that even more.

peterC

ballardw
Super User

Autocall macros are supposed to be one macro with the name of the file matching that of the Macro. ie c:\directory\subdirectory\macroname.sas

contains %macro macroname ...

If you violate that then odd things may happen when related to SASAutos.

Patrick
Opal | Level 21

It's certainly not good practice but I've seen code like:

%macro init;

%mend;

%macro something;

%mend;

....

And the whole code had been stored in a file "init.sas" in a location defined in SASAUTOS. So when calling %init: in a program all the other macros got compiled as well and became available for calling.

pchegoor
Pyrite | Level 9

When i run the following code on SAS EG 6.1 connected to SAS 9.4  :

proc catalog catalog=work.sasmac1;

contents;

Run;

This is what i see in results window.  But i don't understand how these macros are getting precompiled. Also i don't see where the Macro CHECKFMT is even located. I see that  macro TRIM is located in SASAUTOS location but what about the others including CHECKFMT ? Also how is it that ONLY  the macro TRIM from SASAUTOS location gets precompiled but NOT others. This means it is being invoked at the time of Workspace server invocation somehwere but i am not sure where exactly.

Anyone have any ideas on this?

    

1CHECKFMTMACRO10/03/2014 00:50:5010/03/2014 00:50:50
2ECLIBASSIGNMACRO10/03/2014 00:50:5010/03/2014 00:50:50
3ECLIBUNASSIGNMACRO10/03/2014 00:50:5010/03/2014 00:50:50
4ENTERPRISEGUIDEMACRO10/03/2014 00:50:5010/03/2014 00:50:50
5TRIMMACRO10/03/2014 00:50:5110/03/2014 00:50:51
6_EG_CONDITIONAL_DROPDSMACRO10/03/2014 00:50:5010/03/2014 00:50:50
7_EG_ENSUREHTMLBLUEEXISTSMACRO10/03/2014 00:50:5110/03/2014 00:50:51
8_EG_HIDENOTESANDSOURCEMACRO10/03/2014 00:50:5010/03/2014 00:50:50
9_EG_RESTORENOTESANDSOURCEMACRO10/03/2014 00:50:5010/03/2014 00:50:50
10_EG_WHEREPARAMMACRO10/03/2014 00:50:5010/03/2014 00:50:50
11_SAS_POPCHARTSIZEMACRO10/03/2014 00:50:5010/03/2014 00:50:50
12_SAS_PUSHCHARTSIZEMACRO10/03/2014 00:50:5010/03/2014 00:50:50
13_SAS_VERCOMPMACRO10/03/2014 00:50:5010/03/2014 00:50:50
14_SAS_VERCONDCODEMACRO10/03/2014 00:50:5010/03/2014 00:50:50
jakarman
Barite | Level 11

trying to link macro-namings to sas code  is a very limited approach

Suppose you could gather all sas logging of your involved sas programs. In that case you could analyze the logs of them to get the image what macro-s are touched with that subset. 

The MAUTOCOMPLOC System Option is the one giving  the location and you can choose to display the full path. 

This only is applicable to mautocall type macro-s.

These autocall macro-s are the subset of macros that are not been pre-compiled.

They must have a fitted name in OS level, the one must be found also using concatenation with filenames.

An Unix issue: use only lowercase letters in the OS-filenames, no capitals. The SAS languages is not case sensitive but Unix is, a translations is imbedded.  

a Windows issue is that the OS is case-insensitive but allowed are naming containing all low and upper cases a you like.

The concatenation is a good technical trick to solve Life-cycle references questions. The known path on a OS is similar. The sas-config eg the SAShelp SASmacros is full with this approach. In that cases minimizing additional technical tricks for accessing objects and rely on naming conventions for isolation.  

Autocall macros are not the only way to define those.

-  A %include or other type of sas-code can define and use also macro-s

- Running some compiled sas-code can result in including additional sas-code containing macro-s.

There are a lot of reasons making an source-code analyzes on use macro-s impossible even you only look at the subset of the autocall ones.

- nested macro-s are possible only called when a certain condition is met. Error recovery is a common one. That macro is essential a part of the logical design seldom used, but when needed may be essential.

- redefining a macro as a standard in-line stream is often done. DI (and others) generated code is having a lot of that.

You are not the fist one or the last with this kind of questions. SAS  has tried to do this with scaproc. Conversion old code to more modular approach for parallel processing.

Base SAS(R) 9.4 Procedures Guide, Second Edition (scaproc), see alo the limitations http://support.sas.com/resources/papers/proceedings12/237-2012.pdf. Usage of this is hidden in the menus of Eguide.  

---->-- ja karman --<-----
jakarman
Barite | Level 11

You see a lot of namings that are related to Eguide. The standard Trim macro function look to be one that is a macro not SAS internal resolved.

Eguide will put those in every time it will run some code. SAS-macro-s are also used by SAS not only by you.

Not very interesting where SAS did get those unless:

- You are having some strange problems and are debugging those.

- You are changing macro options where macro-s can be found. The SAS-system is delivered with a set related to the installation.

  Do not overwrite that one, extend those settings with own paths when needed.  

---->-- ja karman --<-----
Peter_C
Rhodochrosite | Level 12

further to Jaap's message

since 9.2 the base SAS language provides 2 unusual options - INSERT and APPEND allow us to extend the value of the SASAUTOS option with paths to additional macro libraries.

INSERT and APPEBD also work for other "list" options - most helpfully FMTSEARCH

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 15 replies
  • 4480 views
  • 5 likes
  • 8 in conversation