I have a folder of SAS data files. My goal is to take each of those files and convert it into a csv file. I want all those csv files to be in their own folder. Thus, I will have one folder of the initial SAS files, as well as one folder of the respective csv files.
I know how to convert individual SAS files to csv, but I'm not sure how to do it across an entire library.
Also, I need the csv files to have the same name as the respective SAS files, except with a .csv at the end, obviously.
Retrieve the dataset names for a library from SASHELP.VTABLE in a DATA _NULL_ step, and use CALL EXECUTE to create the PROC EXPORT code for each.
Hi, thanks for the response. Sorry to bother but could you elaborate on how to retrieve the dataset names from sashelp.vtable? I understand I need to do
data _null_;
set sashelp.vtable;
but I'm not sure where to go from there. Thanks!
data _null_;
set sashelp.vtable;
where libname = "YOUR_LIB"; /* all uppercase! */
call execute("
proc export
data=YOUR_LIB." !! strip(memname) !! "
outfile='path_to_your_files/" !! lowcase(strip(memname)) !! ".csv'
dbms=csv
replace
;
run;
");
run;
Untested, posted from my tablet.
Here's a full solution.
Change the library at the top of the program and the path to where you want the files to be saved and it will dynamically create the folders and files for you.
options dlcreatedir;
proc options option=dlcreatedir;
run;
*SET THESE TO BE FOR YOUR CODE;
%let lib2export = WORK;
%let path2files = /home/demo/;
*DO NOT CHANGE BELOW;
data _null_;
*list of files;
set sashelp.vtable(where = (libname = "&lib2export") keep = libname memname);
*create folder path for files to be stored;
folder4file = catt("&path2files.", memname);
*use libname to create folders;
rc_open = libname('demo1', folder4file) ;
rc_close = libname('demo1');
file2export = catt(folder4file, "/", memname, ".csv");
*export file;
length str $300.;
str = catt("proc export data=",
"&lib2export.",
".",
memname,
" outfile=",
trim(quote(file2export)),
" dbms=csv replace;run;");
call execute (str);
run;
If you want to understand the code, I would suggest saving it to a data set and then examining the data set.
data _null_;
*becomes;
data exportFiles;
If you need to debug it, comment out the CALL EXECUTE line and make sure the code generated in STR is correct.
@AbOnTheCob wrote:
I have a folder of SAS data files. My goal is to take each of those files and convert it into a csv file. I want all those csv files to be in their own folder. Thus, I will have one folder of the initial SAS files, as well as one folder of the respective csv files.
I know how to convert individual SAS files to csv, but I'm not sure how to do it across an entire library.
Also, I need the csv files to have the same name as the respective SAS files, except with a .csv at the end, obviously.
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.
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.