#!/bin/bash
#<pre><b>
# Script     : missvars_win
# Version    : 1.0
# Author     : Roland Rashleigh-Berry
# Date       : 20-Feb-2007
# Purpose    : List all-missing variables (uses SAS)
# SubScripts : none
# Notes      : WINDOWS VERSION
#              Uses the sasautos extensions macros. You need to make sure these
#              are available on the sasautos path.
# Usage      : missvars_win  ds1 ds2
#              missvars_win
#===============================================================================
# PARAMETERS:
#-pos- -------------------------------description-------------------------------
#  1   (optional) dataset or list of datasets to check for all-missing variables.
#      Defaults to the whole library.
#===============================================================================
# AMENDMENT HISTORY:
# init --date-- mod-id ----------------------description------------------------
# rrb  20Feb07         Remove call to allocation macro
#===============================================================================
# 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 [ $# -eq 0 ] ; then

############ no parameters specified so do for whole library ############

# Write SAS code to a temporary file
cat > $HOME/missvars_$$.sas << -----FINISH-----
options validvarname=any nofmterr noovp nodate nonumber;
filename _outfile "$HOMEW\missvars_$$.tmp";
libname here './' access=readonly;

%dslist(here)

%macro doit;
  %do i=1 %to %words(&_dslist_);
    %let memname=%scan(&_dslist_,&i,%str( ));
    %missvars(here.&memname)
  
    %if %length(&_miss_) %then %do;
      data miss;
        length memname $ 10 varname $ 20;
        retain memname "&memname";
        %do j=1 %to %words(&_miss_);
          varname="%scan(&_miss_,&j,%str( ))";
          output;
        %end;
      run;

      proc append base=base data=miss;
      run;
    %end;
  %end;

  data _null_;
    file _outfile notitles noprint;
    set base;
    put @1 memname @12 varname;
  run;
%mend doit;

%doit
-----FINISH-----

# run the code
sas -nosplash -nologo -icon -log "$HOMEW" -sysin "$HOMEW\missvars_$$.sas"

# if output file exists then cat it and delete it
if [ -f $HOME/missvars_$$.tmp ] ; then
  cat $HOME/missvars_$$.tmp 
  rm -f $HOME/missvars_$$.tmp 
fi

# tidy up
rm -f $HOME/missvars_$$.sas $HOME/missvars_$$.log



else


for dataset in "$@" ; do

########## do for individual specified datasets ##########

# Write SAS code to a temporary file
cat > $HOME/missvars_$$.sas << -----FINISH-----
options validvarname=any nofmterr noovp nodate nonumber;
filename _outfile "$HOMEW\missvars_$$.tmp";
libname here './' access=readonly;

%macro doit;
  %missvars(here.$dataset)
  
  %if %length(&_miss_) %then %do;
    data miss;
      length memname $ 10 varname $ 20;
      retain memname "&memname";
      %do j=1 %to %words(&_miss_);
        varname="%scan(&_miss_,&j,%str( ))";
        output;
      %end;
    run;
  %end;
%mend doit;

%doit

data _null_;
  file _outfile notitles noprint;
  set miss;
  put @1 "%upcase($dataset)" @12 varname;
run;
-----FINISH-----

# run the code
sas -nosplash -nologo -icon -log "$HOMEW" -sysin "$HOMEW\missvars_$$.sas"

# if output file exists then cat it and delete it
if [ -f $HOME/missvars_$$.tmp ] ; then
  cat $HOME/missvars_$$.tmp 
  rm -f $HOME/missvars_$$.tmp 
fi

# tidy up
rm -f $HOME/missvars_$$.sas $HOME/missvars_$$.log



done

fi
