#!/bin/bash
#<pre><b>
# Script     : contentsl_win
# Version    : 1.0
# Author     : Roland Rashleigh-Berry
# Date       : 11-Feb-2007
# Purpose    : To list the contents (in long form) of one or more SAS datasets
#              (or a whole library) in the current directory (uses SAS).
# SubScripts : none
# Notes      : WINDOWS VERSION
#              Must be run with data directory as current directory. Dataset
#              file extensions will be ignored. This is for Linux on Windows
#              calling SAS installed on Windows. You need to set up and export
#              a system environment variable in your .bashrc named HOMEW like
#              this:
#                    HOMEW=C:\\\cygwin\\\home\\\rrash
#                    export HOMEW
#              ...where "rrash" is your user id (it might be "Default")
# Usage      : contentsl_win adv
#              contentsl_win adv acct
#              contentsl_win    # 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------------------------
# 
#===============================================================================
# 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 [ $# -lt 1 ]
then

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

# Write sas code to a temporary file
cat > $HOME/contentsl_$$.sas << -----FINISH-----
options validvarname=any nofmterr;
proc format;
  value type 1='N' 2='C';
run;
libname here './' access=readonly;
filename _outfile "$HOMEW\contentsl_$$.tmp";
proc contents noprint data=here._all_ out=contents;
data contents;
  length newfmt $ 15;
  set contents;
  newfmt=format;
  if formatl>0 then do;
    newfmt=trim(newfmt)||compress(put(formatl,3.))||'.';
    if formatd>0 then newfmt=trim(newfmt)||compress(put(formatd,3.));
  end;
  else if newfmt ne ' ' then newfmt=trim(newfmt)||'.';
data _null_;
  file _outfile notitles noprint ;
  set contents;
  put @1 memname @10 name @21 length @25 type type. @27 newfmt @40 label;
run;
-----FINISH-----

sas -nosplash -nologo -icon -sasuser work -log "$HOMEW" -sysin "$HOMEW\contentsl_$$.sas"

if [ -f $HOME/contentsl_$$.tmp ] ; then
  cat   $HOME/contentsl_$$.tmp
  rm -f $HOME/contentsl_$$.tmp
fi

rm -f   $HOME/contentsl_$$.sas  $HOME/contentsl_$$.log



else

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

for dataset in "$@" ; do

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

# Write sas code to a temporary file
cat > $HOME/contentsl_$$.sas << -----FINISH-----
options validvarname=any nofmterr;
proc format;
  value type 1='N' 2='C';
run;
libname here './' access=readonly;
filename _outfile "$HOMEW\contentsl_$$.tmp";
proc contents noprint data=here.$dset out=contents;
data contents;
  length newfmt $ 15;
  set contents;
  newfmt=format;
  if formatl>0 then do;
    newfmt=trim(newfmt)||compress(put(formatl,3.))||'.';
    if formatd>0 then newfmt=trim(newfmt)||compress(put(formatd,3.));
  end;
  else if newfmt ne ' ' then newfmt=trim(newfmt)||'.';
data _null_;
  file _outfile notitles noprint;
  set contents;
  put @1 memname @10 name @21 length @25 type type. @27 newfmt @40 label;
run;
-----FINISH-----

sas -nosplash -nologo -icon -sasuser work -log "$HOMEW" -sysin "$HOMEW\contentsl_$$.sas"

if [ -f $HOME/contentsl_$$.tmp ] ; then
  cat   $HOME/contentsl_$$.tmp
  rm -f $HOME/contentsl_$$.tmp
fi

rm -f   $HOME/contentsl_$$.sas  $HOME/contentsl_$$.log



done



fi
