I save this bash script as a file named sasprint in a directory in my path, and I call it by saying
sasprint /wrds/crsp/sasdata/sm/msf.sas7bdat #prints 20 observations (default) sasprint /wrds/crsp/sasdata/sm/msf.sas7bdat 30 #prints 30 observations sasprint /wrds/crsp/sasdata/sm/msf.sas7bdat 20 'abs(prc)>5' #prints 20 observations where PRC>5 sasprint /wrds/crsp/sasdata/sm/msf.sas7bdat 20 'abs(prc)>5' 'permno date ret' #prints permno date ret
The code follows:
#!/bin/bash if [ ! -f $1 ]; then echo "File $1 does not exist" exit fi fullfilename="$1" filepath="${fullfilename%/*}" filename=$(basename $fullfilename) #only the filename filename=${filename%.*} #without the extension numberoflines=$2 if [ -z $2 ]; then numberoflines=20 fi wherestatement="" if [ ! -z "$3" ]; then wherestatement="where $3;" fi varstatement="" if [ ! -z "$4" ]; then varstatement="var $4;" fi command="libname xtemp '$filepath/'; proc print data=xtemp.$filename (obs=$numberoflines); $wherestatement $varstatement" blank=" " #Hack: sas seems to chop off the first few characters echo "$blank $command" | sas -sysin /dev/stdin -print /dev/stdout -log /dev/null #Check if SAS returned an error if [ $? -ne 0 ]; then echo "Error in program: rerunning for log" echo "$blank $command" | sas -sysin /dev/stdin -log /dev/stdout -print /dev/null fi
Notice the use of the sysin option, which tells SAS where to find code (in this case, in standard input: STDIN). Notice also the redirection of the log and lst files (see section 15.4).