DATA Step, Macro, Functions and more

How to Export a dataset into CSV file with Column Names

Reply
Super Contributor
Posts: 276

How to Export a dataset into CSV file with Column Names

Hi All..

I am exporting a dataset into CSV format with using following code..

%macro exportdsPR;  

proc contents data=sasuser.admit  out=rcount(keep=NOBS NAME) details noprint;

   run;

%let empty=YES;

%let var=;

   data null;

      set rcount(obs=1);

      if nobs ge 1 then call symput('empty', 'NO');

   run;

   %if &empty = NO %then %do;

                 data null ;

          set  sasuser.admit  end=EFIEOD;

          %let EFIERR = 0;

          %let EFIREC = 0;   

          file "C:\Documents and Settings\sanjeev.k\Desktop\New Folder\test.csv" delimiter=',' DSD DROPOVER lrecl=32767;

          

          EFIOUT + 1;

          

      put   id $ @;

      put name  $ @;

      put sex $ @ ;

      put age $ @ ;

      put date  $ @ ;

      put height $ @;

      put weight $ @ ;

      put actlevel $ @;

      put fee $  ;

 

          if ERROR then call symputx('_EFIERR_',1); 

          if EFIEOD then call symputx('_EFIREC_',EFIOUT);

     run;

   %end;

  

%mend exportdsPR;

With this code ,CSV file exporting successfully without column names.

Is it possible to get Column Names in the exported CSV file??

Could you please provide any solution for this..

Thanks in Advance..

Regrads

Sanjeev.K

Contributor
Posts: 70

Re: How to Export a dataset into CSV file with Column Names

Posted in reply to kuridisanjeev

Please go through below, this may help

https://communities.sas.com/thread/31487?start=0&tstart=0

Thanks and Regards,

Yaswanth J.

Super Contributor
Posts: 282

Re: How to Export a dataset into CSV file with Column Names

Posted in reply to kuridisanjeev

Hi Sanjeev,

How about the following:

data _null_;

  call symputx('_EFIREC_',nobs);

  stop;

  set sashelp.class nobs=nobs;

run;

%macro writecsv;

  %if &_EFIREC_ %then

  %do;

    %ds2csv(data   =sashelp.class

           ,runmode=b

           ,csvfile=C:\Documents and Settings\sanjeev.k\Desktop\New Folder\test.csv

           ,var    =name sex age /* omit this line if you want all variables */

           );

  %end;

%mend writecsv;

%writecsv;

Regards,

Amir.

Message was edited by: Amir Malik - formatting.

Super Contributor
Posts: 276

Re: How to Export a dataset into CSV file with Column Names

Hi Amir..

Thanks for your solution..

but i can not continue with your solution,because i don't want to disturb the code which i have mentioned in my question as some conditions mentions in that code(IF dataset was empty Export file should not be created).

Can you please do work with my code and if possible provide the solution..

Thanks &Regards..

Sanjeev.K

Super Contributor
Posts: 282

Re: How to Export a dataset into CSV file with Column Names

Posted in reply to kuridisanjeev

Hi Sanjeev,

The condition:

%if &_EFIREC_ %then

in the code means the csv file is only produced if there are any records.

Regards,

Amir.

Super Contributor
Posts: 282

Re: How to Export a dataset into CSV file with Column Names

Posted in reply to kuridisanjeev

Hi Sanjeev,

Further, the link referred to by Yaswanth has a simple solution if you want to change your code as little as possible.

Regards,

Amir.

Ask a Question
Discussion stats
  • 5 replies
  • 681 views
  • 0 likes
  • 3 in conversation