*
;

/*Subject: Re: Convert a SAS dataset to csv-format
From: Andrew Smith 

> I have a small dataset containing 70.000 obs :-) A friend of mine
> would like a copy of the data. Unfortunately he doesn't have SAS,
> but he thinks that he can use the data if it is in a csv-formated
> file. But where is the export function in SAS? 

You can use this macro, which was in a recent issue of SAS Observations;
before calling the macro, you may need a libname statement to define the 
libref parameter (lib) and a filename statement to define the output 
fileref parameter (out). It worked ok for me but I didn't find it was 
perfect when it comes to character variables with quotes in them.

Andrew Smith, University of Reading

-----------------------------------------------------------------------------
*/

%macro delim(lib, dsn, out);

proc contents data=&lib..&dsn
  out=_temp_ (keep=name type npos) noprint;
run;

proc sort data=_temp_; by npos; run;

data _null_;
  set _temp_ end=eof;
  call symput('var'||(left(put(_n_,5.))),name);
  call symput('typ'||(left(put(_n_,5.))), left(put(type,8.)));
  if eof then call symput('total',left(put(_n_,8.)));
run;

data _null_;
  file &out noprint;
  set &lib..&dsn;
  format _numeric_ best12.;
  put
  %do i = 1 %to &total;
    %if &&typ&i=1 %then %do; /* numeric variable */
      &&var&i +(-1) ','
    %end;
    %else %do;  /* character variable */
      """" &&var&i +(-1) ""","
    %end;
  %end;
  +(-1) ' ';  /* remove the extra comma at the end */
run;

%mend delim;

*
;