SAS Procedures As UNIX Commands Sriharsha Rachabattula, Covance Inc
Total Page:16
File Type:pdf, Size:1020Kb
NESUG 2011 Coders' Corner SAS Procedures as UNIX Commands Sriharsha Rachabattula, Covance Inc. ABSTRACT: UNIX is one of the most widely used operating system in pharmaceutical industry, as it is renowned for being the most reliable, secure system. With SAS being GUI and UNIX being CUI, doing a quick check on the contents of a dataset or simple stats like frequencies, means, medians etc, is tedious. A SAS code file has to be created and executed in UNIX to check the .LST file generated by SAS for the results. A simple solution for this would be executing SAS procedures like Proc Contents, Proc Freq or Proc Means right at the UNIX command prompt and displaying the results on your UNIX screen. This paper explains and gives you the code to create and use SAS Procedures as UNIX commands and get the results on your UNIX screen, without creating any permanent SAS files. INTRODUCTION: When working with SAS, a GUI application on UNIX in a traditional method would be creating a SAS file with all the SAS statements and executing using SAS command. Though the method is very secured and successful, but at times doing a quick check on data or finding frequency of a data value would not only create multiple permanent results files though the traditional method but would also be a time and resource consuming. A UNIX command that could display the SAS procedure results right on the screen without creating a SAS file or any permanent files would always come in handy. Here in the paper a UNIX Korn Shell script and a corresponding SAS program is detailed and explained in order to generate results of few SAS procedures that would normally display in an interactive SAS output window. UNIX SHELL SCRIPT: Following UNIX shell script is ready to use on Korn Shell. This single script file works for following commands: 1) PROCCONTENTS – to display the contents of a SAS dataset 2) PROCPRINT – to display the SAS data and with options to display certain number of records. 3) PROCFREQ – with options to display the frequency of a variable mentioned in the options 4) PROCUNIPLOT – will run similar to proc univariate and displays a box plot for a numeric variable. This script calls a SAS program described below and displays the results on the computer screen. This script also generates all default SAS files in a temporary directory and later deleted as the command execution completes. IMPORTANT: In order for the shell script to function for multiple commands, this script file should be linked to each above described commands using a soft link (ln). #!/usr/bin/ksh ####################################################################### ## sasprocs: Displays SAS procedure(s) results. ####################################################################### usrid=`whoami` pwd=`pwd` parm2=no ################################################## ##if user interupts, jump to cleanup trap cleanup 1 2 3 9 15 cleanup() { echo "Cleanup all temporary files ..." /usr/bin/rm /tmp/${usrid}pt.* exit 0 } ################################################## ## Read the command if [ `echo $0 | grep "proccontents" | wc -l` -eq 1 ] then 1 NESUG 2011 Coders' Corner parm0=proccontents popt0= if [ "$1" = "" ] then echo "SAS Data set name is required for execution." echo " Syntax: $parm0 <sas data set name> + <Option>" echo " Example: $parm0 dm $popt0" echo " " exit 0 elif [ "$2" = "" ] then parm2=no elif [ "$2" != "" ] then echo "" echo "ERROR: $2 option not found." echo "" echo "SAS Data set name is required for execution." echo " Syntax: $parm0 <sas data set name> + <Option>" echo " Example: $parm0 dm $popt0" echo " " exit 0 fi elif [ `echo $0 | grep "procprint" | wc -l` -eq 1 ] then parm0=procprint popt0=-flm if [ "$1" = "" ] then echo "SAS Data set name is required for execution." echo " Syntax: $parm0 <sas data set name> + <Option>" echo " Example: $parm0 dm $popt0" echo " " flm option: for first 20 , last 20 and exit 0 middle 20 records elif [ "$2" = "-flm" ] then parm2=yes elif [ "$2" = "" ] then parm2=no elif [ "$2" != "-flm" ] then echo "" echo "ERROR: $2 not a valid option." echo "" echo "SAS Data set name is required for execution." echo " Syntax: $parm0 <sas data set name> + <Option>" echo " Example: $parm0 dm $popt0" echo " " exit 0 fi elif [ `echo $0 | grep "procuniplot" | wc -l` -eq 1 ] then parm0=procuniplot popt0=age if [ "$1" = "" ] then echo "SAS Data set name is required for execution." echo " Syntax: $parm0 <sas data set name> + <Option>" echo " Example: $parm0 dm $popt0" echo " " exit 0 elif [ "$2" = "" ] || [ "$2" = "_all_" ] || [ "$2" = "_ALL_" ] then echo "" 2 NESUG 2011 Coders' Corner echo "ERROR: $2 not a valid option." echo "" echo "SAS Data set name is required for execution." echo " Syntax: $parm0 <sas data set name> + <Option>" echo " Example: $parm0 dm $popt0" echo " " exit 0 elif [ "$2" != "" ] || [ "$2"! = "_all_" ] || [ "$2" != "_ALL_" ] then parm2=`echo $2` fi elif [ `echo $0 | grep "procfreq" | wc -l` -eq 1 ] then parm0=procfreq popt0=race*gender if [ "$1" = "" ] then echo "SAS Data set name is required for execution." echo " Syntax: $parm0 <sas data set name> + <Option>" echo " Example: $parm0 dm $popt0" echo " " exit 0 elif [ "$2" = "" ] || [ "$2" = "_all_" ] || [ "$2" = "_ALL_" ] then echo "" echo "ERROR: $2 not a valid option." echo "" echo "SAS Data set name is required for execution." echo " Syntax: $parm0 <sas data set name> + <Option>" echo " Example: $parm0 dm $popt0" echo " " exit 0 elif [ "$2" != "" ] || [ "$2"! = "_all_" ] || [ "$2" != "_ALL_" ] then parm2=`echo $2` fi else exit 0 fi ################################################## ## Check if user entered any sas dataset file name if [ "$1" = "" ] then echo "" echo "SAS Data set name is required for execution." echo " Syntax: $parm0 <sas data set name> + <Options>" echo " Example: $parm0 dm $popt0" echo " " exit 0 fi ################################################## ## Strip the SAS file extension ".sas7bdat" dataset=`echo $1 | sed 's/\.sas7bdat//'` fuldst=`echo "$dataset" | sed 's/[ ]*$/\.sas7bdat/'` ################################################## ### logdisp module ### Display logfile if any errors logdisp() { echo " LOG FILE " echo "------------------------------------------------------------" more /tmp/${usrid}pt.log /bin/rm /tmp/${usrid}pt.* 3 NESUG 2011 Coders' Corner exit 0 } ################################################## # Check if data file exists, if not then exit with message if [ ! -f $fuldst ] then echo "" echo "ERROR: The \"$fuldst\" Dataset does not exist in $pwd." echo "" exit 0 elif [ -f $fuldst ] then libdir=`pwd` ################################################## ##export all the data to the sas file export libdir export dataset export usrid calling SAS program file mentioned export parm0 below export parm2 ################################################## ##copy the procsas.sas file to the unix temporary folder cp /home/sriharsha/bin/msasprocs.sas /tmp/${usrid}pt.sas ################################################## ##execute the SAS file cd /tmp sas ${usrid}pt.sas grep -i "sas stopped" /tmp/${usrid}pt.log ################################################## ###Check if the results file exists and display if exists if test -f /tmp/${usrid}pt.lst then lines=`head -10 /tmp/${usrid}pt.lst | wc -l` if [ $lines -le 2 ] then echo "Empty Results Generated. ." else more /tmp/${usrid}pt.lst fi else echo " " echo "No Results Generated. ." echo " " clog /tmp/${usrid}pt.log echo "" fi fi ################################################## ###Cleanup all temporary files /usr/bin/rm /tmp/${usrid}pt.* exit 0 ################################################## ## End of file 4 NESUG 2011 Coders' Corner SAS PROGRAM: Following SAS program is called by the above UNIX shell script when one of the UNIX command is executed. /***************************************************************************; ** PROGRAM NAME: msasprocs.sas ** SAS VERSION: 9.1.3 ** ** DEVELOPER: Sriharsha Rachabattula ** ** PURPOSE: for Quick display of data based on the UNIX command call ****************************************************************************/ options obs= max firstobs= 1 compress= no pagesize= 60 linesize= 132 ; %global srcdst srclib usrid statparm lstobs; %let usrid=%sysfunc(strip(%sysget(usrid))); ** user id imported from unix **; %let srclib= %sysfunc(strip(%sysget(libdir))); ** libname imported from unix **; %let srcdst= %sysfunc(strip(%sysget(dataset))); ** dataset name imported from unix **; %let statparm=%sysfunc(strip(%sysget(parm0))); ** Stat procedure imported from unix ; %let lstobs=%sysfunc(strip(%sysget(parm2))); ** Listing number of observations imported from unix **; %let univar=%sysfunc(strip(%sysget(parm2))); **univariate variable imported from unix; %put User ID: &usrid.; %put dataset location from unix: &srclib.; %put dataset from unix: &srcdst.; %put Stat procedure imported from unix: &statparm.; %put Listing number of observations imported from unix: &lstobs.; %put univariate variable imported from unix: &univar.; libname dstlib "&srclib." access=readonly; *** Macro for storing total number of observations in the data set ***; %macro nobs(libname= , data= ); %local libname data; %let nobs = ; data _null_; set sashelp.vtable( where=(libname= %upcase("&libname.") and memname eq %upcase ("&data.")) keep= nobs libname memname ); format = 'F'; %*** lets make sure that the field width here is at least 1; if nobs gt 1 then width = ceil( log( nobs ) ) + 1 ; else width = 1; call symput( 'nobs', putn( nobs, format, width )); stop; run; %put data nobs: &nobs.; %if &nobs eq