04-02-2013 10:59 PM
I want to write a macro which exports all sas data files in a folder to a .csv files.
Thanks for the help from all of you in this community, I could successfully imports several files in a folder using macro.
But this time, I have to export multiple files in a library as a .csv files.
I tried to change the macro for importing to the macro exporting, but it did not work well.
I would like to generate the list of .sas7bdat files in a folder and use a macro that can export all of them at the same time.
I am using Linux and the list of filenames is..
The macro that I am considering is..
filename dir "&subdir.*.sas7bdat";
length filename fname $ 200;
infile dir eof=last filename=fname ;
proc sort data=new nodupkey;
proc print data=new;
set new end=last;
if last then call symputx('nobs',_n_);
%do i=1 %to &nobs;
proc export data=&&dsn&i outfile='&&filename&i/&&dsn&i.csv'
I think that the bold parts have some problems...
Can anyone help me figure out how I can fix this problem?
Or, if you think that this code has many problems and you would like to suggest other code, please let me know.
Thank you for your help in advance!
04-03-2013 02:40 AM
First take a look at your FILENAME statement. I do not know Linux so do not know if the * will sufficiently concatenate the files - if not there are other ways. But at least there is a missing /.
filename dir "&subdir./*.sas7bdat";
have you verified that the data set NEW contains the list? What are its variable values?
04-03-2013 04:03 AM
here i'm sending sample code by trating all the datafiles in a folder as library in windows envornment.
select memname into :dnames separated by ' ' from dictionary.tables where libname='WORK' and memtype='DATA';
select count(*) into :counts from dictionary.tables where libname='WORK' and memtype='DATA';
%do i= 1 %to &counts ;
%let dname=%scan(&dnames,&i,' ' );
ods csvall file="c:\test\&dname..csv";
proc print data=&dname noobs;
ods csvall close;
04-03-2013 07:03 AM
When working with SAS files use SAS tools.
You can gen the code you need with a data step and sashelp.vmember. You will use a different LIBNAME and the other parts of thw WHERE statement will be different. Also OUTFILE will need to be specific to your OS and desired path. I have UNIX and just wrote to my HOME.