Help using Base SAS procedures

export

Accepted Solution Solved
Reply
Regular Contributor
Posts: 161
Accepted Solution

export

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


Accepted Solutions
Solution
‎11-08-2012 10:40 AM
Super Contributor
Posts: 1,636

Re: export

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


All Replies
Super Contributor
Posts: 1,636

Re: export

Hi,

try :

PROC EXPORT data=CED

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

dbms=csv replace;

putnames=yes;

run;

Regular Contributor
Posts: 161

Re: export

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

Respected Advisor
Posts: 3,124

Re: export

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

Regular Contributor
Posts: 161

Re: export

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;

Respected Advisor
Posts: 3,124

Re: export

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

Super Contributor
Posts: 1,636

Re: export

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;

Respected Advisor
Posts: 3,124

Re: export

LinLin,

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

Haikuo

Solution
‎11-08-2012 10:40 AM
Super Contributor
Posts: 1,636

Re: export

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;

Respected Advisor
Posts: 3,124

Re: export

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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