BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
R_A_G_
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Linlin
Lapis Lazuli | Level 10

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;

View solution in original post

9 REPLIES 9
Linlin
Lapis Lazuli | Level 10

Hi,

try :

PROC EXPORT data=CED

outfile="C:\DATA\d3.csv"

dbms=csv replace;

putnames=yes;

run;

R_A_G_
Calcite | Level 5

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

Haikuo
Onyx | Level 15

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

R_A_G_
Calcite | Level 5

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;

Haikuo
Onyx | Level 15

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

Linlin
Lapis Lazuli | Level 10

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;

Haikuo
Onyx | Level 15

LinLin,

It surely will work, however, one more pass will be involved as well, if efficiency is part of your equation.

Haikuo

Linlin
Lapis Lazuli | Level 10

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;

Haikuo
Onyx | Level 15

LinLin,

I think you have just provided a solution that OP will most likely be comfortable with Smiley Happy. You are always the nicer one.

Haikuo

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1984 views
  • 3 likes
  • 3 in conversation