Hi all,
I am not a SAS user at all, so please I apology if my question is a bit stupid. I was given access to data in SAS format and I want to covert it to Stata (.dta), which is the software I am more comfortable with (btw, I cannot do it using the usesas command in Stata because the data is stored in a computed with no access to internet, so I am unable to download user-written commands).
I can do it file by file, with the following code:
libname in 'folder_sas_data';
data temp;
set in.file_name;
proc export data=temp outfile='folder_stata_data/file_name.dta';
run;
However, there are thousands of files with different names that do not follow a clear pattern. Ideally I would like to access to all SAS files in the folder_sas_data directory, and open each of them and export them in dta format.
Any clue on how can I do this?
Thank you very much
No need to apologise if you don't know; at least you're trying. I'm not sure I see the need for data and the set statements, because you should be able to export all of the SAS data sets directly, without first having to copy them to a temp SAS data set.
The following untested code makes use of SAS system's dictionary tables to get the names of the SAS data sets in library 'in' then from that it constructs the proc export statements and writes them out to a file for you to examine and then submit in SAS if you are happy with what you see. Note the comment to "replace 'full-file-path.sas'", the SAS code to run will be contained in there.
libname in 'folder_sas_data';
/* replace 'full-file-path.sas' with a path and name of a file that will have SAS code written to it */
data _null_;
file 'full-file-path.sas';
set sashelp.vtable;
where upcase(libname) eq 'IN';
put "proc export data=in." memname "outfile='folder_stata_data/" memname +(-1) ".dta;";
put 'run;';
run;
Report back if you have any issues with the log showing the code and any errors, warnings, etc.
Regards,
Amir.
No need to apologise if you don't know; at least you're trying. I'm not sure I see the need for data and the set statements, because you should be able to export all of the SAS data sets directly, without first having to copy them to a temp SAS data set.
The following untested code makes use of SAS system's dictionary tables to get the names of the SAS data sets in library 'in' then from that it constructs the proc export statements and writes them out to a file for you to examine and then submit in SAS if you are happy with what you see. Note the comment to "replace 'full-file-path.sas'", the SAS code to run will be contained in there.
libname in 'folder_sas_data';
/* replace 'full-file-path.sas' with a path and name of a file that will have SAS code written to it */
data _null_;
file 'full-file-path.sas';
set sashelp.vtable;
where upcase(libname) eq 'IN';
put "proc export data=in." memname "outfile='folder_stata_data/" memname +(-1) ".dta;";
put 'run;';
run;
Report back if you have any issues with the log showing the code and any errors, warnings, etc.
Regards,
Amir.
1) The SAS code I have presented looks for all the SAS data sets (tables) in the "folder_sas_data" location that is in your code.
2) My code then creates a SAS program containing proc export statements for each and every SAS data set it found from step (1).
3) This generated code from (2) is written to a file. This file is referred to in my code with:
file 'full-file-path.sas';
4) The location and name of the file containing the proc export statements is up to you, as long as it ends in '.sas'. Whatever you decide upon should replace the 'full-file-path.sas', e.g. (depending upon whether your'e using Windows or Unix or Linux, etc.):
file 'c:\tmp\my_proc_export_code.sas';
5) Once the code I have posted completes (without issues - see SAS log), open your version of 'full-file-path.sas' in SAS and you should see all of the proc export statements - one per SAS data set.
6) If you are happy with what you see then you can submit the proc export code, perhaps try the first one initially to see if there are any issues, then, if OK, the rest can be submitted in one go.
I hope that is clearer.
Please ask if you have any questions.
Regards,
Amir.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.