Google SAS Search

Add to Google

Tuesday, January 23, 2007

SAS Unix Process ID

Today a friend called and wanted to know if there was an easy way to use the Unix Process ID as part of the name of the log file when invoking SAS in batch mode. She wanted to make (semi-)unique log files. She is concerned about uniqueness enough to not want to immediatly overwrite another log file, but not so much that she's worried about possible collisions when the system recycles a process ID.

Well, it just so happens the script variable $$ contains the process ID of that script. So you can use that when constructing your log file name. Such as:

nohup /home/sas mySAScode.sas -log "/tmp/mySAScode_$$.log" &

In the above, nohup tells unix to keep the process alive even after we've closed down our terminal and logged off.
/home/sas is the sas executable (or the script that executes SAS).

An example of the log file created by this command would be /tmp/mySAScode_1298656.log

If you wanted to get the process ID that SAS was started with you can use the automatic macro variable &SYSJOBID within SAS.

If you wanted to learn some more about running SAS on Unix you could also bounce over to SASonUnix.blogspot.com. It has some very good tips.

7 comments:

  1. Interesting approach. To get a unique log file name I grab the date and exact time of starting the execution and have that as part of the log file name.

    For example:
    DATA _null_ ;
    daterun = SYMGET('sysdate9') ;
    daterun = substr(daterun, 1,2)||"_"||LOWCASE(substr(daterun, 3,3))||"_"||substr(daterun,6,4) ;
    CALL SYMPUT('daterun', daterun) ;
    RUN ;

    %LET daterun=%substr(&daterun,1,11) ;

    PROC PRINTTO PRINT="/pkg/ipums/napp/output_data/current/working_data/&samp._checking_&daterun..txt" ;
    RUN ;

    ReplyDelete
  2. Good comment, Evan. Unfortunately my colleague didn't want to touch the code she was administrating, so wanted to accomplish it outside of SAS. But that doesn't detract from the usefullness of the code you shared. One note though, it's been a while since I used proc printto, but doesn't the print= option refer to output, not the log?

    ReplyDelete
  3. Yes, what I'm doing there is re-directing the output, not the log. But IIRC PROC PRINTTO LOG="zzz" works in the same way.

    This approach has worked well for us in a multi-user (4 of us) environment where we're checking re-formatted data several times a day and want to re-run the same data conversion program until the output file from SAS shows the data is error free.

    ReplyDelete
  4. interesting stuff guys!
    is there a way to append the log of a new sas program to an existing log file?

    ReplyDelete
  5. Hi Reef! Are you on Windows/Unix/other? I ask because I checked on Windows real quick and it looks like the log file gets appended to across SAS sessions if you use:

    proc printto log="c:\temp\myLog.txt";
    run;

    Of course, you would use your own file name. The proc printto redirects the log output to the file. To turn off the redirection, just use a bare proc printto; run;

    Hope that helps and thanks for reading and commenting!

    ReplyDelete
  6. Thanks Stephen! Sorry for the delay due to a number of things including a fractured rib in a judo session. I use the same code in unix sas for re-directing to a new log file. Is there a way to append a new log to an existing log file?

    ReplyDelete
  7. Another way os to use the temp option of filename - this gives a unique name and removes it for you on close automatically.
    This works cross platform.

    ReplyDelete