Skip to content

Instantly share code, notes, and snippets.

@statgeek
Last active January 20, 2023 16:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statgeek/878e585102c14e01581f55dbe972d27e to your computer and use it in GitHub Desktop.
Save statgeek/878e585102c14e01581f55dbe972d27e to your computer and use it in GitHub Desktop.
SAS - Macro to convert XLSX to CSV
/*This program will take a folder path (default) and look for files
that are specifically with the specified extension (ext) and convert
the files to CSV. It uses XCMD so you need to have that option enabled.
The original script is from here:
http://support.sas.com/kb/43/496.html
Modified to convert XLSX to CSV.
Author:F. Khurshed
Date: 2018-03-23
*/
options noxwait noxsync;
%macro convert_files(default=,ext=);
data _null_;
file "'&default\temp.vbs'";
put "set xlapp = CreateObject(""Excel.Application"")";
put "set fso = CreateObject(""scripting.filesystemobject"")";
put "set myfolder = fso.GetFolder(""&default"")";
put "set myfiles = myfolder.Files";
put "xlapp.DisplayAlerts = False";
put " ";
put "for each f in myfiles";
put " ExtName = fso.GetExtensionName(f)";
put " Filename= fso.GetBaseName(f)";
put " if ExtName=""&ext"" then";
put " set mybook = xlapp.Workbooks.Open(f.Path)";
put " xlapp.Visible = false";
put " mybook.SaveAs ""&default.\"" & Filename & "".csv"", 6";
put " End If";
put " Next";
put " mybook.Close";
put " xlapp.DisplayAlerts = True";
/* Removes original files */
/*put " FSO.DeleteFile(""&default\*.&ext""), DeleteReadOnly";*/
put " xlapp.Quit";
put " Set xlapp = Nothing";
put " strScript = Wscript.ScriptFullName";
put " FSO.DeleteFile(strScript)";
run;
x "cscript ""&default\temp.vbs""";
%mend;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment