#!/usr/bin/sh ################################################################################### # Script name: pp # # Show SAS proc print output # # example: # > pp dm "where=(age <= 35 & race='WHITE') firstobs=1 obs=10 keep=race sex age" ################################################################################### # Basename and Dirname of this script typeset basename=$(basename $0) typeset dirname=$(dirname $0) [[ $dirname = .* ]] && dirname=$PWD/$dirname # Lower case left-justified dataset name typeset -lL dataset typeset library # User-specified input typeset input variables="" dsoptions="" ls=${COLUMNS:=80} ps=${LINES:=40} more=${PAGER:=more} if (($ls < 64)); then ls=64 elif (($ls > 256)); then ls=256 fi if (($ps < 15)); then ps=15 elif (($ps > 32767)); then ps=32767 fi function usage { cat << END_OF_HELP SYNTAX: pp dataset [variable list] ["constraint(s)"] DESCRIPTION: Prints to the screen (via SAS "proc print") the SAS dataset that is specified as a command line argument. OPTIONS: variable list - The variables selected to print (separated by space and without quotes, e.g. var1 var2 var3). This is equivalent to specifying the variables in the "var" statement in SAS "proc print". If none are specified, then all variables are displayed. "constraint(s)" - Allows the user to subset the dataset and select observations based on user-specified constraint(s). This option can take one or more conditions separated by space and enclosed in quotes. Keywords used in the subsetting can be "where=()", "firstobs=", "obs=", "keep=" and "drop=". USAGE: This tool will search for the first occurence of the specified SAS dataset in the following order: 1. current directory 2. current directory/libraries 3. under parallel directory structure in sdtmroot You can use this tool on only one SAS dataset at a time. EXAMPLE: #1: pp dm #2: pp dm subjid race sex age #3: pp dm "where=(age <= 35 & race='WHITE') firstobs=1 obs=10 keep=race sex age" #4: pp dm "drop=studyid domain usubjid" exiting... END_OF_HELP exit 1 } [[ $# -ge 1 ]] || usage typeset count=0 # step through the command line parameters while [ -n "${1:-}" ] do ((count=$count+1)) if [[ $count = 1 ]]; then # Derive library path from first parameter library=$(dirname $1) dataset=$(basename $1) shift elif [[ ${1#where=} != $1 || ${1#where =} != $1 || ${1#firstobs=} != $1 || ${1#firstobs =} != $1 || ${1#obs=} != $1 || ${1#obs =} != $1 || ${1#keep=} != $1 || ${1#keep =} != $1 || ${1#drop=} != $1 || ${1#drop =} != $1 ]]; then dsoptions="$dsoptions $1" shift else #assume to be variable name variables="$variables $1" shift fi done # Search path per APPSDEV-830 if [[ $library = "." ]]; then if [[ -f $dataset || -f "$dataset.sas7bdat" ]]; then library=$PWD elif [[ -f ./libraries/$dataset || -f ./libraries/$dataset.sas7bdat ]]; then library="$PWD/libraries" elif [[ -f $SDTMROOT/${PWD##*/}/$dataset || -f $SDTMROOT/${PWD##*/}/$dataset.sas7bdat ]]; then library=$SDTMROOT/${PWD##*/} else print -u2 "Dataset not found in any location" exit 4 fi else if [[ ! -f $library/$dataset && ! -f $library/$dataset.sas7bdat ]]; then print -u2 "Specified dataset not found" exit 4 fi fi # Derive SAS dataset name from first parameter and remove extension dataset="$(print -- $dataset | sed 's!.sas7bdat$!!')" # Build SAS proc print var statement if [[ -n $variables ]]; then variables="var $variables;" # SAS proc print statement fi if [[ -n $dsoptions ]]; then dsoptions="($dsoptions)" fi # Create unique and temporary log and sas program names sasfile=$(mktemp -d ~/).sas log=$(mktemp -d ~/).log # Begin constructing the sas program echo "options nocenter nofmterr nonumber linesize=$ls nodate;" >> $sasfile echo "title;footnote;" >> $sasfile echo "libname LIBRARY '$library' access=readonly; " >> $sasfile echo "proc PRINT data=LIBRARY.$dataset$dsoptions; " >> $sasfile echo "$variables; run; " >> $sasfile echo "Running proc print on dataset: $dataset ($library)" sas9 -nodms -noautoexec -stdio $sasfile 2>$log | more grep -F -e 'ERROR' -e 'WARNING' <$log rm -f $sasfile $log