/* Macro sas_get (modified by F. Harrell 30Jan90, Bill Dunlap Dec90)
    Sets up for conversion of SAS dataset to S dataset.

    Arguments:
	dataset - name of SAS dataset
	temp1	- Name of temporary dataset to contain data dictionary 		  	  	  (unquoted)
		  default=/tmp/file.1
	temp2	- Name of temporary dataset to contain ASCII version of SAS
		  dataset (unquoted)
		  default=/tmp/file.2
	dates	- SAS to store date variables in SAS format (# days from 1/1/60)
		  (default)
		- YEARFRAC to store as days from 1/1/1900, divided by 365.25
		- YEARFRAC2 to store as year + fraction of current year
		- YYMMDD to store as numeric YYMMDD
	vars    - list of variable in dataset that you want returned to Splus
                  (unquoted, separate variable names with spaces)  If empty,
                  then return all variables.
        ifs     - sequence of SAS subsetting if statements, (unquoted,
                  separated by semicolons).
  To run this on your own (for sas.fget) copy this file and add 3 lines
  to the end of the copy:  2 to define libnames for libraries
  containing data and formats used in data, respectively, # and 1 line
  to call the above macro.  If your SAS data object foo is stored in
  library SAS.data and formats used by foo are in library SAS.formats
  and you call your temporary files (to be read by sas.fget) file.1 and
  file.2 then the 3 lines to add would be:

  libname temp 'SAS.data';
  libname library 'SAS.formats';
  %sas_get(temp.foo, file.1, file.2, dates=sas, vars=, ifs=);
                                                                              */
%macro sas_get(dataset,  temp1, temp2, dates=SAS, vars=, ifs=);
%IF %QUOTE(&temp1)=  %THEN %LET temp1=/tmp/file.1;
%IF %QUOTE(&temp2)=  %THEN %LET temp2=/tmp/file.2;
%LET dates=%UPCASE(&dates);
%LET _s_=_sav_;
/* Subset by observation first */
%IF &ifs^= %THEN %DO;
 data _osub_ ;
  set &dataset ;
  &ifs ;
 %LET dataset=_osub_ ;
 %END;
/* Then subset by variable */
%IF &vars^= %THEN %DO;
 data _vsub_ ;
  set &dataset ;
  keep &vars ;
 %LET dataset=_vsub_ ;
 %END;
proc contents data=&dataset out=&_s_(KEEP=name type length label format nobs 
 varnum) noprint; 
PROC SORT DATA=&_s_;BY varnum;

data _null_;
 set &_s_ end=eof;
 file "&temp1";

 if _n_ = 1 then do;
  put "data _null_; set &dataset end=eof;";
  put '  file "&temp2" RECFM=D LRECL=4096;';
  put "  retain __delim 18 _bk_ -1; LENGTH _xx_ $ 20; ";
  end;

 IF type=2 THEN DO;
  PUT 'IF ' name '=" " THEN PUT __delim IB1. @;';
  PUT 'ELSE PUT ' name '+_bk_ __delim IB1. @;';
  END;
 ELSE DO; 
  PUT 'IF ' name '<=.Z THEN _xx_="NA";' @;
  PUT 'ELSE _xx_=LEFT(PUT(' @;
  format=UPCASE(format);
  IF format="DATE"|format="MMDDYY"|format="YYMMDD"|format="DDMMYY"|format="YYQ"|format="MONYY"|format="JULIAN" THEN DO;
   %IF &dates=SAS %THEN
    PUT name ",BEST18.)";
   %ELSE %IF &dates=YYMMDD %THEN
    PUT name ",YYMMDD6.)";
   %ELSE %IF &dates=YEARFRAC %THEN
    PUT "(" name "-MDY(1,1,1900))/365.25,7.3)";
   %ELSE %IF &dates=YEARFRAC2 %THEN %DO;
    PUT "YEAR(" name ")-1900+(" name "-MDY(1,1,YEAR(" name ")))/" @;
    PUT "(MDY(12,31,YEAR(" name "))-MDY(1,1,YEAR(" name "))+1),7.3)";
    %END;
   ;
   END;	
  ELSE DO;PUT name ",BEST18.)" @;END;
  PUT ');  PUT _xx_ __delim IB1. @;';
  END;
if eof then PUT 'PUT; RUN;';
run;
%include "&temp1";
data _null_; set &_s_;
 retain __delim 18 _bk_ -1; 
 file "&temp1";
 name=TRANSLATE(name,".abcdefghijklmnopqrstuvwxyz",
		     "_ABCDEFGHIJKLMNOPQRSTUVWXYZ");
 put name __delim IB1. type __delim IB1. length __delim IB1.
  format __delim IB1. label +_bk_ __delim IB1. nobs __delim IB1.;

run;
%mend sas_get;
