BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10

%macro FileExist; %if %sysfunc(fileexist(&InPath_Core.&InFileName)) %then   x rm "&InPath_Core.&InFileName."; %mend; %FileExist;

I just inherited a report that includes the above named code.  I believe the x rm is referencing a string variable to be used later in the report.  Is there a best practices method of decoding when not quite sure as to what a macro code sequence is doing?

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

The x rm has nothing to do with macro language.  X is the system command function which passes system commands through to the operating system.  RM is a dos command function remove.  What you code there is doing is:

Does this file xyz exist

If yes then remove the file xyz

return.

Personally I feel its quite dangerous to do this, if your macro resolves incorrectly you could erase whole directories or the wrong file etc.

ballardw
Super User

This is an example of a questionable practice: assuming a variable not passed as a parameter.

The referenced code is either called within another macro that sets values for the two macro variables &InPathCore and &InFileName OR before this macro is used there should be two %let statements assigning values.

In this case it looks like the code is to remove a file if it exists. The macro variables better be defined before the %FileExist is executed. Not the best name for a macro, DeleteFileExist might be better as Delete is the action performed.

Best practice is to provide a comment in the code as to what the macro accomplishes. If it modifies the values of variables created elsewhere it is a good idea to document that as well. In the case of "mystery" variables such as used here it may not be a bad idea to say where they are defined.

SAS documentation will get you as far use of such items as the %sysfunc, FileExist and X statements. Once you see X then you need to go to the operating system or other programs as X provides the ability to run system commands including other programs.

Astounding
PROC Star

The most important step to take is to learn more SAS.  That might include the X command, but could include virtually any SAS pieces.  Remember, macro language doesn't process your data.  It merely generates the program that processes your data.  So the more you know about the programming language itself the better.

The next most important step would be to learn more macro language.  To address your original question, learn about the options MPRINT, SYMBOLGEN, and MLOGIC.  Learning how and when to turn them on/off will help you view the generated SAS program as well as the macro language logic used to produce that program.  With more macro language knowledge, for example, you might notice that the macro you posted contains a flaw.  It is missing a semicolon that would end the X statement.

Another tool that might help in decoding is the MFILE option.  It would let you store the generated SAS program in a file that you designate so you can examine it with all the macro language stripped away.

There are few shortcuts.  Much of this is a long-term learning process.  Good luck.

Ron_MacroMaven
Lapis Lazuli | Level 10

a better name and parameter might be

%macro delete_if_exist(filename=)

then the action is obvious and easy to read

%delete_if_exist(filename=&InPath_Core.&InFileName)


Ron Fehd  readability maven

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
  • 4 replies
  • 2197 views
  • 1 like
  • 5 in conversation