Skip to content

Instantly share code, notes, and snippets.

@statgeek
Created November 21, 2018 18:24
Show Gist options
  • Select an option

  • Save statgeek/ae153e40af0d35dce02d1c8102ff3b94 to your computer and use it in GitHub Desktop.

Select an option

Save statgeek/ae153e40af0d35dce02d1c8102ff3b94 to your computer and use it in GitHub Desktop.
SAS - export to CSV with labels and names
/*This is an example of how to export a data set with two header rows,
one that is labels and oen that is the variable names
*/
*Create demo data;
data class;
set sashelp.class;
label age='Age, Years' weight = 'Weight(lbs)' height='Height, inches';
run;
proc sql noprint;
create table temp as
select name as _name_, label as _label_
from dictionary.columns
where libname="WORK" and upcase(memname)="CLASS";
select cats(quote(name),"n") into :varList separated by ' '
from dictionary.columns
where libname="WORK" and upcase(memname)="CLASS";
quit;
data _null_;
file "&sasforum.\datasets\TwoLinesHeader.csv" dsd;
set class;
if _n_ = 1 then do;
do until(eof);
set temp end=eof;
put _name_ @;
end;
put;
eof = 0;
do until(eof);
set temp end=eof;
put _label_ @;
end;
put;
end;
put (&varList) (:);
run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment