#!/bin/bash
#<pre><b>
# Script     : contents
# Version    : 2.0
# Author     : Roland Rashleigh-Berry
# Date       : 06-Feb-2007
# Purpose    : To list the contents (in short form) of one or more SAS datasets
#              (or a whole library) in the current directory (uses SAS).
# SubScripts : contents_win
# Notes      : Must be run with data directory as current directory. Dataset
#              file extensions will be ignored.
# Usage      : contents adv
#              contents adv acct
#              contents    # all datasets are listed
#===============================================================================
# PARAMETERS:
#-pos- -------------------------------description-------------------------------
#  1   one or more datasets (optional - will work on whole library by default)
#===============================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description------------------------
# rrb  24Nov05         -stdio used on sas invocation
# rrb  06Feb07         Call contents_win for Windows configurations
#===============================================================================
# This is public domain software. No guarantee as to suitability or accuracy is
# given or implied. User uses this code entirely at their own risk.
#===============================================================================

# if a Windows config then call contents_win and exit on return
if [ -n "$CYGWIN" ] ; then
  contents_win "$@"
  exit $?
fi

if [ $# -lt 1 ]
then

###### no datasets specified so do for the whole library #####

# Feed SAS code into standard input
sas -stdio -noautoexec -sasuser work 2>/dev/null << -----FINISH-----
options validvarname=any nofmterr;
libname here './' access=readonly;
proc contents noprint data=here._all_ out=contents;
data _null_;
  file stdout;
  set contents;
  put @1 memname @10 name @22 label;
run;
-----FINISH-----


else

########### one or more dataset specified so do for each #########

for dataset in "$@" ; do

dset=$(echo $dataset | cut -d. -f1)

# Feed SAS code into standard input
sas -stdio -noautoexec -sasuser work 2>/dev/null << -----FINISH-----
options validvarname=any nofmterr;
libname here './' access=readonly;
proc contents noprint data=here.$dset out=contents;
data _null_;
  file stdout;
  set contents;
  put @1 memname @10 name @22 label;
run;
-----FINISH-----


done

fi
