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

Hi there, I'm trying to export several datasets to csv. I do export some files but they're not in CSV format. They're just 'File'.  My code is below:

%macro export;
%do  i =  1 %to &max;
proc export data=temp&i
outfile="C:\Users\SAS\Output\&&dset&i..csv" dbms=csv replace; 
run;

%end;

%mend export;

 

&&dset &i is a macro var. I created it by the following code. 

 

filename DIRLIST pipe 'dir "C:\Users\SAS\coded_logs*.csv" /b ';

data dirlist ;
   infile dirlist lrecl=200 truncover;
   input file_name $100.;
run;

data _null_;
   set dirlist end=end;
   count+1;
   call symputx('read'||put(count,4.-l),cats('C:\Users\SAS\',file_name));
   call symputx('dset'||put(count,4.-l),scan(file_name,1,'.'));
   if end then call symputx('max',count);
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You told PROC EXPORT to make CSV files, so that is what it made.  The only reason it wouldn't look like a CSV file would be if the data only had one variable.

 

But don't you normally want to have a period and extension on the filenames. Like .CSV?   They way you have it written your code both periods will be used by the macro processor.  The first to mark the end of the macro variable I and the second to mark the end of the macro variable DSET1, DSET2, etc. 

You want.

 

outfile="C:\Users\SAS\Output\&&dset&i...csv"

Or better still skip wasting your time and confusing yourself by making all of those macro variables.

 

 

data _null_;
   if end then call symputx('max',_n_-1);
   set dirlist end=end;
   call execute(catx(' '
,'proc export data=',cats('temp',_n_),'outfile='
,quote(cats('C:\Users\SAS\Output\',scan(file_name,1,'.'),'.csv'))
,'dbms=csv replace;run;'
));
run;

 

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

 Please look at data set DIRLIST and confirm it has the desired values.  

 

What do you see if you use the command

 

%put &=dset1;

 

 

Are there spaces in the value of &DSET1? are there spaces at the beginning of the value? Does it have the value you want it to have?

 

If that's not it, then use the command

options mprint;

aat the beginning of your program and run the code again and then you can look at what is in the log and see if something is not happening the way you want it to happen.

 
 
 
 
 
--
Paige Miller
Tom
Super User Tom
Super User

You told PROC EXPORT to make CSV files, so that is what it made.  The only reason it wouldn't look like a CSV file would be if the data only had one variable.

 

But don't you normally want to have a period and extension on the filenames. Like .CSV?   They way you have it written your code both periods will be used by the macro processor.  The first to mark the end of the macro variable I and the second to mark the end of the macro variable DSET1, DSET2, etc. 

You want.

 

outfile="C:\Users\SAS\Output\&&dset&i...csv"

Or better still skip wasting your time and confusing yourself by making all of those macro variables.

 

 

data _null_;
   if end then call symputx('max',_n_-1);
   set dirlist end=end;
   call execute(catx(' '
,'proc export data=',cats('temp',_n_),'outfile='
,quote(cats('C:\Users\SAS\Output\',scan(file_name,1,'.'),'.csv'))
,'dbms=csv replace;run;'
));
run;

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 824 views
  • 0 likes
  • 3 in conversation