Techie Tip: Stamping logs
with date, time and user name
By Craig Kasper
We run some of our SAS programs used for ongoing
processes in batch mode. (From Windows, this is simple: Right-click
on the file from Windows Explorer and select Batch Submit with SAS
9.1 from the context menu.) Running programs in batch mode ensures
that the log and program output is saved. When multiple people may
be running a specific program in batch mode at multiple times, it is
typically a good idea to include information about when the program
was run and by whom.
The following lines of macro code can be executed
anywhere outside a PROC or a data statement to stamp output with date,
time and user name.
Date:
%put NOTE: The current date is %scan(%sysfunc(date(),downame.),1),
%scan(%sysfunc(date(),monname.),1) %scan(%sysfunc(date(),day2.),1),
%scan(%sysfunc(date(),year4.),1).;
Time:
%put NOTE: The current time is %sysfunc(time(),tod8.).;
User Name (Windows only):
%put NOTE: The current user is %sysget(username).;
By way of explanation, the %sysfunc function calls
regular SAS functions within macro code. %put is a macro statement
for outputting text, so we need to call SAS functions like date() using
%sysfunc as an intermediary. (The same effect could be achieved using
a DATA _null_ statement, but this is more compact.) Also, note that
downame, monname, day2 and year4 are all date formats, and can be replaced
with other date formats if you prefer; the same goes for the time format
tod. (When a function called by %sysfunc returns a value, the %sysfunc
function applies any SAS format you specify before returning the value).
Similarly, the %sysget function returns the value
of a variable tracked by the operating system. Recent versions of Windows
make the name of the current user available in the USERNAME environment
variable.
Avoiding overwriting files by generating
filenames on the fly
Because macro functions can be used within double quotes in ordinary
SAS statements, there are a number of ways to use macro functions to
build filenames on the fly. Here’s a useful one, building on the previous
tip. The following FILENAME statement generates a file name which includes
the date and time that the file was run, which can then be used with
PROC PRINTTO to allow the log to be saved to this file.
filename saveto "C:\SAS Logs\Log of Prog1 run - Date %sysfunc(date(),yymmddn8.)
- Time %sysfunc(hour(%sysfunc(time())),z2.)-%sysfunc(minute(%sysfunc(time())),z2.)-%sysfunc(second(%sysfunc(time())),z2.).txt";
When the values from all of the macro functions are substituted, the
actual filename will be something like:
Log of Prog1 run - Date 20081205 - time 10-13-22.txt
Note that by including the date before the time, and formatting time
as four digit year/two digit month/two digit day, the files will be
chronologically sorted when sorted by filename.
Adding your own help files to the SAS help
menu
If your organization has a library of macros implemented for its users
(as we do), it can be very useful to have information about the macros
readily accessible. I recommend doing this by creating an HTML file
which contains the documentation, then registering it as a help file
in your SAS config file. Creating the HTML file with the documentation
is straightforward enough, as Word and most other recent word processors
will allow you to save your document as HTML.
To register your help file, add a line like the following to your
SAS config file, which is typically located in your SAS install directory:
-helpregister "Text appearing in Help Menu" "C:\sample\location\of\file\Sample
help file name.htm" "Status Line Text on Mouseover" "" HTML
This line may be added anywhere before the “/* DO NOT EDIT BELOW THIS
LINE - INSTALL Application edits below this line */” line. You may
add multiple help files this way by adding a helpregister line multiple
times.
|