BookmarkSubscribeRSS Feed
kuridisanjeev
Quartz | Level 8

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

5 REPLIES 5
yaswanthj
Calcite | Level 5

Please go through below, this may help

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

Thanks and Regards,

Yaswanth J.

Amir
PROC Star

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.

kuridisanjeev
Quartz | Level 8

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

Amir
PROC Star

Hi Sanjeev,

The condition:

%if &_EFIREC_ %then

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

Regards,

Amir.

Amir
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 2918 views
  • 0 likes
  • 3 in conversation