#!/bin/bash
#<pre><b>
# Script     : misscnt_win
# Version    : 1.0
# Author     : Roland Rashleigh-Berry
# Date       : 20-Feb-2007
# Purpose    : Display a count of variables and their number of missing 
#              observations (uses SAS)
# SubScripts : none
# Notes      : WINDOWS VERSION
#              Uses some of the sasautos extensions macros.
# Usage      : misscnt_win ds1 ds2
#              misscnt_win
#===============================================================================
# PARAMETERS:
#-pos- -------------------------------description-------------------------------
#  1   (optional) Dataset or list of dataset for analysis. 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/misscnt_$$.sas << -----FINISH-----
options validvarname=any nofmterr noovp nodate nonumber;
filename _outfile "$HOMEW\misscnt_$$.tmp";
libname here './' access=readonly;
%dslist(here)

%macro doit;
  %do i=1 %to %words(&_dslist_);
    %let memname=%scan(&_dslist_,&i,%str( ));
    %misscnt(here.&memname)
  
    %if %length(&_miss_) %then %do;
      data miss;
        length memname $ 10 varname $ 20;
        retain memname "&memname";
        %do j=1 %to %words(%str(&_miss_));
          varname="%scan(%str(&_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\misscnt_$$.sas"

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

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



else


for dataset in "$@" ; do

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

%macro doit;
  %misscnt(here.$dataset)
  
  %if %length(&_miss_) %then %do;
    data miss;
      length memname $ 10 varname $ 20;
      retain memname "&memname";
      %do j=1 %to %words(%str(&_miss_));
        varname="%scan(%str(&_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\misscnt_$$.sas"

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

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


done


fi
