DATA Step, Macro, Functions and more

Proc export to CSV from an empty dataset

Reply
New Contributor
Posts: 2

Proc export to CSV from an empty dataset

Hello All,

 

I am trying to export an empty dataset to csv.

I am able to get the .csv file with zero records.  But in the log I am getting an error.

 

0 records created in /DQ_VERIFICATION/EXP_XXXXXX_F_20180508.csv from

EXP_XXXXXX.

 

ERROR: Export unsuccessful. See SAS Log for details.

 

How to avoid the Error in log.

 

Thanks in advance!

Super User
Posts: 9,932

Re: Proc export to CSV from an empty dataset

[ Edited ]

Please post the complete log of your failed step.

 

Edit: I guess you not only have zero observations, but also no variables in your dataset. In that case, proc export won't work. And a situation like that makes no sense in my eyes.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,441

Re: Proc export to CSV from an empty dataset

What you are saying and what you expect do not match.  CSV - Comma Separated Variable file is a text file with one row of header information, and then a row for each observation with the data separated by a comma.  Having no data means it is not a CSV, its just a text file with a line of text.  Hence why you are getting errors.  

Instead of trying to export something which does not exist, just write a text line to a text file (and don't call it CSV as its not):

data _null_;
  file "empty_file.txt";
  put "abc,def,ghi";
run;
  
Super User
Posts: 23,357

Re: Proc export to CSV from an empty dataset

Here's an example of a macro that prints the data set if it exists and otherwise prints a message to the log. You can modify it to have each section do what you need it to do.

 

http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=p011imau3tm4jen1us2a45cyenz9.htm&docset...

Super User
Super User
Posts: 7,944

Re: Proc export to CSV from an empty dataset

Proc export works fine for a dataset with zero observations. It fails for a dataset that does not have any variables.

Try this program to see the difference.

data zero nothing(drop=_all_);
 set sashelp.class(obs=0);
run;

%let path=c:\downloads;
proc export data=zero file="&path/zero.csv" dbms=csv  replace ; 
run;
proc export data=nothing file="&path/nothing.csv" dbms=csv  replace ; 
run;

I expect the error is higher up in your process that created the dataset that does not have any variables. But if you did want to create a text file from that nothing dataset you could use another method to make the CSV file. For example you could use this macro:

https://github.com/sasutils/macros/blob/master/csvfile.sas

%csvfile(zero,outfile="&path/zero.csv");
%csvfile(nothing,outfile="&path/nothing.csv");

For ZERO it will create a file with only the header row. And for NOTHING it will create a file with zero bytes since it does not even have any variables to include in the header row.

 

Ask a Question
Discussion stats
  • 4 replies
  • 79 views
  • 0 likes
  • 5 in conversation