I am creating CSV file through the below piece of code
ods csv file="c:\test\test.csv";
proc print data=WORK.QUERY7095;
ods csv close;
When there is no data the csv file is created but without column header.
How should i include column header.
Any option or any other way to carete csv which will give me column header when no data is there in my report.
But you could use a SAS Macro program to get "around" the issue. There's even a DATA Step method that uses only a few macro variables to get around the issue.
** run a bad query and set data set name;
%let mydsn = work.myquery;
create table &mydsn as
select * from sashelp.class
where sex = 'm' ;
** now open the data set, get the number of obs, ;
** and close the query data set.;
%put -----> dsid= &dsid nobs=&nobs close sysrc=&sysrc;
%put -----> Your query ran and created &nobs observations;
ods csv file="c:\temp\want_hdr_anyway.csv";
if &nobs = 0 then do;
file print ods;
If you run this and the query is bad, the DATA _NULL_ step writes out the headers and one row of missing data because the PDV is built and so the PUT _ODS_ executes BEFORE the set statement tries to read the 0 obs file. Then the PROC PRINT just fails because there are 0 obs -- but the outcome is that you have a CSV file with just the headers.
There's a lot that could be done further to "macroize" the program. But without doing much more than this, someone could get headers even if the query failed.
IF you try the reverse (create a good query) like this (only change these 2 statements):
** change the LET for the dsn;
%let mydsn = work.OKquery;
** change the WHERE statement to generate a good query;
where sex = 'M' ;
*** may also want to change the FILE= name if you want to compare the 2 files;
And then the DATA _NULL_ will execute, but the IF statement will fail, because &NOBS will be greater than 0. So, no harm, no foul from the DATA step -- nothing gets written to ODS. The PROC PRINT will execute and the CSV file will have headers and data.