Hello,
How can I export a data set in csv format and keep the variable name in the first row.
This is mine but the names are not exported.
DATA _NULL_;
set CED;
file "C:\DATA\d1.csv" dsd dlm=',';
put CE $ 1-20 item1-item116;
run;
I tried another way with PROC EXPORT but have issues with it as well, it's keep asking for TABLE:
PROC EXPORT data=CED;
file="C:\\DATA\d3.csv" ;
dbms=dlm;
DELIMITER==',';
run;
thanks
Hi R.A.G.
If you don't care about efficiency then try the code below(replace the colored parts😞
data temp;
length ce $20;
set your_dataset;
run;
PROC EXPORT data=temp
outfile="C:\DATA\d3.csv"
dbms=csv replace;
putnames=yes;
run;
Hi,
try :
PROC EXPORT data=CED
outfile="C:\DATA\d3.csv"
dbms=csv replace;
putnames=yes;
run;
Thanks that was correct only one problem. I need to truncate the first variable to less than 20 spaces.
How can I do that in the above code?
CE $ 1-20
You can't do it using Proc Export, which does not has the control over individual variable. You may need to go back to data step:
filename out 'C:\DATA\';
proc sql NOPRINT;
select name into :name separated by ' ' from DICTIONARY.COLUMNS WHERE LIBNAME='WORK' AND MEMNAME='CED';/*HAS TO BE CAPS, HERE AND BELOW*/
SELECT NAME INTO :QNAME separated by ',' from DICTIONARY.COLUMNS WHERE LIBNAME='WORK' AND MEMNAME='CED';;QUIT;
data _null_;
length CE $20;
set CED;
file out(D3.csv) dsd;
IF _n_=1 then put "&QNAME";
put &name;
run;
Haikuo
I am not familiar with proc sql.
can I add a line or two to have the variable name in this coed?
Thank you
DATA _NULL_;
set Coded1;
file "C:\data\d1.csv" dsd dlm=',';
put CE $ 1-20 item1-item116;
run;
SQL part of code is just to save your effort of typing in all of your variable names, if you have too many. otherwise, you can simply ignore it.
However, the 'length' statement has to be put BEFORE 'set' statement, and I don't think you need dlm=',', as dsd already has it.
DATA _NULL_;
length CE $20;
set Coded1;
file "C:\data\d1.csv" dsd ;
if _n_=1 then put "CE, item1, item2, ......item116"; /*if you don't mind typing*/
put CE item1-item116;
run;
Haikuo
Hi Haikuo,
Do you think it would work if shorten the variable before exporting to csv file?
data coded1;
length ce $20;
set coded1;
run;
LinLin,
It surely will work, however, one more pass will be involved as well, if efficiency is part of your equation.
Haikuo
Hi R.A.G.
If you don't care about efficiency then try the code below(replace the colored parts😞
data temp;
length ce $20;
set your_dataset;
run;
PROC EXPORT data=temp
outfile="C:\DATA\d3.csv"
dbms=csv replace;
putnames=yes;
run;
LinLin,
I think you have just provided a solution that OP will most likely be comfortable with . You are always the nicer one.
Haikuo
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.