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

Hi, 

I am exporting a sas dataset into csv using ods csv file statement and i am getting a complaint that the csv file has a blank row at the bottom. I have searched through the topics and found one which is very similar to my issue but when use that the csv file is created without column headers. I need to display column headers but i couldnt find a way how to do that.

any help is appreciated

current process below - delta is the SAS table that I am trying to export into csv with headers in it

/* export to csv */

%let rundate = %SYSFUNC(today(),YYMMDDN.);
%put rundate is &rundate;


ods listing close;
ods csv file="c\Data_&rundate..csv";
proc print data=delta noobs label;
run;
ods csv close; 

 

But when I do this, recipient says there is a blank row (which I dont see anywhere) at the bottom of the csv file which causes issue on their end.

So, I found this topic and solution in the SAS forums but when I use the code below, the csv file that is generated is missing the headers. I need the headers to be in the csv file as well. Thanks in advance.

atesera_0-1707921121172.png

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The simplest thing is not use PROC PRINT to make a CSV file.

 

You can use PROC EXPORT instead.

proc export data=delta file="c\Data_&rundate..csv" replace dbms=csv label;
run;

View solution in original post

11 REPLIES 11
atesera
Calcite | Level 5

Thanks. I tried that and now I can see the column headers. I hope the blank row issue at the bottom of the csv file will be solved by using proc export instead of using ods option for export. 

Kurt_Bremser
Super User

On top of what has already been said, ODS CSV plus PROC PRINT is the slowest way to write a csv file, while the DATA step (and that is what PROC EXPORT creates) is the fastest.

Tom
Super User Tom
Super User

The simplest thing is not use PROC PRINT to make a CSV file.

 

You can use PROC EXPORT instead.

proc export data=delta file="c\Data_&rundate..csv" replace dbms=csv label;
run;
atesera
Calcite | Level 5

Thanks. yes that worked and now I have the column headers in the csv file. Hope proc export step eliminates this blank row issue at the bottom of csv file. Previously I was using ods to export to csv and I guess that might be the issue. 

Tom
Super User Tom
Super User

@atesera wrote:

Thanks. yes that worked and now I have the column headers in the csv file. Hope proc export step eliminates this blank row issue at the bottom of csv file. Previously I was using ods to export to csv and I guess that might be the issue. 


It should work. The ODS CSV tool definitely adds an actual extra empty line that PROC EXPORT does not.

 

The issue in the old topic you linked to was about creating a file that removed the end of line characters from the last actual line of data in the file. Hopefully you are sending your file to a program that actually understands how lines are formed in text files and does not expect the final line to stop before the end of line characters have been written to the file.

atesera
Calcite | Level 5

I haven't yet heard back from the team that loads the file after I switched to proc export instead of ODS csv tool. The reason I was using ODS csv was that I needed very long (over 32 characters) column headers to be displayed in the output file and ODS csv was doing that. But I think I didn't use the "label" option in my proc export step previously so now I use it and all looks good. Thanks for advice and help.

 

Ksharp
Super User
/*You could use the following code to output the variable name at first place.*/
data have;
 set sashelp.class;
run;


/*Get variable's name*/
proc sql noprint;
select name into :names separated by ','
 from dictionary.columns
  where libname='WORK' and memname='HAVE';
quit;
data _null_;
 set have end=last;
 file 'c:\temp\temp.csv' dsd recfm=n;
 if _n_=1 then put "&names." '0D0A'x;
 put (_all_) (:);
 if not last then put '0D0A'x;
 run;

Ksharp_0-1707968382118.png

 

atesera
Calcite | Level 5

Thanks for this. However, the issue with this is that I have to use labels to display in the output csv file. I cant name the column headers in the way that I want because they are very long and doesn't allow me. So, I found the solution of labelling them in the way that I want because in the label there is no restrictions like that. 

So, I cant put the labels here then I get error message saying it is too long and plus I have over 200 columns in my dataset

"&names."

 I believe that proc export step with label option would eliminate the issue and will update this topic once I hear back from the team uploading the output file into the system

Tom
Super User Tom
Super User

If you need headers that are longer than SAS labels allow (currently 256 bytes) then you could also just use a separate dataset for writing the header row.

 

Create a dataset, let's call it HEADERS, with a variable name, let's call it HEADER, that has observations in the same order as the variables appear in your dataset.  Now you can write the CSV file in two steps.

 

First write the header line:

data _null_;
  set headers;
  file csv dsd ;
  put header @;
run;

Then use the MOD option on the FILE statement to append the data lines.

data _null_;
  set have;
  file csv dsd mod;
  put (_all_) (+0);
run;
Ksharp
Super User

That is easy to use variable LABEL instead of variable name.

data have;
 set sashelp.class;
 label name='asasf sdfsfe' weight='w sd erbd s';
run;


/*Get variable's label*/
proc sql noprint;
select quote(strip(label)) into :label separated by ','
 from dictionary.columns
  where libname='WORK' and memname='HAVE';
quit;
data _null_;
 set have end=last;
 file 'c:\temp\temp.csv' dsd recfm=n;
 if _n_=1 then put %sysfunc(quote(%bquote(&label.))) '0D0A'x;
 put (_all_) (:);
 if not last then put '0D0A'x;
 run;

Ksharp_0-1708143003396.png

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 828 views
  • 0 likes
  • 4 in conversation