#!/usr/bin/sh ################################################################################### # Script name: pf # # Show SAS proc freq output # # example: # > pf adsl "armcd*sex / list missing" ################################################################################### # Base name of this script typeset basename=${0##*/} # Lower case left-justified dataset name typeset -L dataset # Check if there are too many parameters if [[ $# -gt 4 ]];then print "Too many paramters, please refer to usage by typing pf at the prompt..." exit 5 fi # Check if the first argument is linesize, or that the first and second argument is '-a' if [[ $1 = "-a" ]]; then print "Please use the '-a' option only after you have specified the dataset and the numeric cut-off..." exit 2 elif [[ $2 = "-a" ]]; then typeset all="yes" set $1 "" $3 $4 elif [[ $3 = "-a" ]] || [[ $4 = "-a" ]]; then typeset all="yes" else typeset all="no" fi # Defaults the linesize of the SAS output to 80, or set to some other value if there are user inputs if echo "$1" | grep -q -E '^-[0-9]+$'; then print "Please use the linesize option only after you have specified at least one other parameter..." exit 3 elif echo "$2" | grep -q -E '^-[0-9]+$'; then typeset linesize=`echo $2 | sed "s/-//g"` set $1 "" $3 $4 elif echo "$3" | grep -q -E '^-[0-9]+$'; then typeset linesize=`echo $3 | sed "s/-//g"` elif echo "$4" | grep -q -E '^-[0-9]+$'; then typeset linesize=`echo $4 | sed "s/-//g"` else typeset linesize=80 fi # Create unique and temporary log and sas program names log=$(mktemp -d ~/)$basename.log sasfile=$(mktemp -d ~/).sas # Usage description function usage { cat << END_OF_HELP SYNTAX: 1. pf dataset "tables" [-linesize] OR 2. pf dataset [number] [-a] [-linesize] DESCRIPTION: 1. Produce one-way to n-way frequency and contingency tables of the specified SAS dataset (like SAS "proc freq") if the "tables" statement is specified as a command line argument, OR 2. Display pertinent variable attributes (e.g. the type, the number of level, the number of missing levels) if the "tables" argument is missing OPTIONS: number - Displays only variables with number of levels <= number -a - Produce one-way frequency tables for all variables selected. -linesize - Allows you to specify the linesize of the SAS output. Default is 80. USAGE: This tool will search for the first occurence of the specified SAS dataset in the following order: 1. current directory You can use this tool on only one SAS dataset at a time. EXAMPLE: #1: pf dm #2: pf dm 10 #3: pf dm 10 -a OR #4: pf dm "race*sex/list missing" #5: pf dm "race*sex/list missing" edit exiting... END_OF_HELP } # If no dataset is specified, show usage if [[ "$1" = "" ]]; then usage exit 4 fi # The first parameter is the file path typeset filepath=`echo $1 | sed "s/.ssd01//;s/.sas7bdat//;s/.*\.//"` typeset library=$(dirname $filepath) typeset dataset=$(basename $filepath) # Save the present working directory typeset pwd=`pwd` # Search for the SAS dataset, first in $pwd, then in $pwd/libraries, and finally in sdtmroot if [ -f $filepath.ssd01 ] || [ -f $filepath.sas7bdat ]; then if [[ $library = "." ]]; then typeset sasdir="$pwd" else typeset sasdir="$library" fi elif [ -f libraries/$dataset.ssd01 ] || [ -f libraries/$dataset.sas7bdat ]; then typeset sasdir="$pwd/libraries" else sdtmroot find_file=$(find `pwd` \( -name $dataset.ssd01 -o -name $dataset.sas7bdat \) -print -quit) if [[ $find_file != "" ]] && [[ `pwd` != $pwd ]]; then sasdir="${find_file%/*}" else echo "Dataset: $dataset.ssd01 or $dataset.sas7bdat not found. Exiting . . . " exit 1 fi fi # The second parameter refers to the table statement typeset tables="$2" # Begin constructing the sas program echo "libname LIBRARY '$sasdir' access=readonly;" >> $sasfile echo "options nocenter nofmterr pagesize=max linesize=$linesize nonumber nodate;" >> $sasfile echo 'title; footnote;' >> $sasfile echo "proc freq data=LIBRARY.$dataset;" >> $sasfile echo "tables $tables ; run;" >> $sasfile echo "Running proc freq on dataset: $dataset ($sasdir)" sas9 -nodms -noautoexec -stdio $sasfile 2>$log grep -F -e 'ERROR' -e 'WARNING' <$log rm -f $sasfile $log