Only headers in csv when there's no data

Accepted Solution Solved
Reply
Occasional Contributor PWS
Occasional Contributor
Posts: 15
Accepted Solution

Only headers in csv when there's no data

Hi,

 

Is it possible to export a csv file with only the headers when there's no data to export?

 

For multiple purpose I automatically generate csv files that will be retrieved by other parties. They can only process the csv files when the column headers are always shown. 

 

But now, when for some reason there's no data to export, also the column headers are left out. This causes fail overs at the receiving end.

 

Can someone help me?

 

This is the example of the way I now publish the csv file;

 

Headers.PNG 


Accepted Solutions
Solution
‎01-22-2018 03:05 AM
Super User
Super User
Posts: 7,932

Re: Only headers in csv when there's no data

The order of the statements in your data step is wrong. SAS will stop when the SET statement reads past the end of the data.

The order should look like this.  Then it will write the headers even if the input has 0 observations.

data _null_;
  file ... ;
  if _n_=1 then ... ;
  set ... ;
  put ... ;
run;

View solution in original post


All Replies
PROC Star
Posts: 1,209

Re: Only headers in csv when there's no data

The %DS2CSV Macro handles this

 

 

/* With observations */
%ds2csv(data=sashelp.class, runmode=b, csvfile=YourPathHere\class.csv);

/* Without Observations */
data NoObsClass;
   set sashelp.class(obs=0);
run;

%ds2csv(data=NoObsClass, runmode=b, csvfile=YourPathHere\NoObsClass.csv);

 

PROC Star
Posts: 1,209

Re: Only headers in csv when there's no data

You can read about the macro in the DS2CSV Documentation 

Solution
‎01-22-2018 03:05 AM
Super User
Super User
Posts: 7,932

Re: Only headers in csv when there's no data

The order of the statements in your data step is wrong. SAS will stop when the SET statement reads past the end of the data.

The order should look like this.  Then it will write the headers even if the input has 0 observations.

data _null_;
  file ... ;
  if _n_=1 then ... ;
  set ... ;
  put ... ;
run;
Occasional Contributor PWS
Occasional Contributor
Posts: 15

Re: Only headers in csv when there's no data

Thanks! This works!
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 171 views
  • 2 likes
  • 3 in conversation