05-28-2015 10:09 AM
%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?
05-28-2015 10:24 AM
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
Personally I feel its quite dangerous to do this, if your macro resolves incorrectly you could erase whole directories or the wrong file etc.
05-28-2015 10:34 AM
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.
05-28-2015 02:21 PM
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.
05-29-2015 01:42 PM
a better name and parameter might be
then the action is obvious and easy to read
Ron Fehd readability maven