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

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 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

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);

 

PeterClemmensen
Tourmaline | Level 20

You can read about the macro in the DS2CSV Documentation 

Tom
Super User Tom
Super User

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;
PWS
Fluorite | Level 6 PWS
Fluorite | Level 6
Thanks! This works!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 1600 views
  • 2 likes
  • 3 in conversation