Hi everyone,
I have large code written in the sas main.sas program, my problem is I couldn't able to read the data present in the zip files, when I also unzip the files with the macro written below...,
%macro unzip(file=, file_out=, path_zip=, path_unzip=);
/* Versione pre SAS94 */
%sysexec cd "C:\Program Files\7-Zip";/* deve contenere il path dell'eseguibile zip */
%sysexec 7z x "&path_zip.\&file..zip" "-o&path_unzip.";
%sysexec rename "&path_unzip.\&file..csv" "&file_out..csv";
%mend;
%unzip;
but I don't understand where is the problem when I open the libraries and see the tables it doesn't contain any data it shows zero observations for me and also in log file I don't see any erros , this makes me not able to figure it out where might be the error. Any suggestions would be commendable.
Thank you in andance.
You can read your data directly from the zip file and no unzipping necessary.
This blog post illustrates the full process, including getting a zip file downloaded from the internet.
https://blogs.sas.com/content/sasdummy/2014/01/29/using-filename-zip/
I'll comment your code (which you should do) so you understand what it's doing.
%macro unzip(file=, file_out=, path_zip=, path_unzip=);
*Opens the 7-zip application;
%sysexec cd "C:\Program Files\7-Zip";/* deve contenere il path dell'eseguibile zip */
*unzips the zipped file;
%sysexec 7z x "&path_zip.\&file..zip" "-o&path_unzip.";
*Renames the file to the desired file name;
%sysexec rename "&path_unzip.\&file..csv" "&file_out..csv";
%mend;
%unzip; * no parameters passed so that's problematic unless they were declared somewhere else;
You should be calling the macro as follows with parameters, but you didn't either so I don't think anything did happen anyways.
%unzip(file=Sample.zip ,
file_out=Want ,
path_zip=C:\temp\demo\ ,
path_unzip= C:\temp\demo_out\)
UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/
Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
Examples of common macro usage
https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...
@shailaja3 wrote:
Hi everyone,
I have large code written in the sas main.sas program, my problem is I couldn't able to read the data present in the zip files, when I also unzip the files with the macro written below...,
%macro unzip(file=, file_out=, path_zip=, path_unzip=);
/* Versione pre SAS94 */
%sysexec cd "C:\Program Files\7-Zip";/* deve contenere il path dell'eseguibile zip */
%sysexec 7z x "&path_zip.\&file..zip" "-o&path_unzip.";
%sysexec rename "&path_unzip.\&file..csv" "&file_out..csv";%mend;
%unzip;
but I don't understand where is the problem when I open the libraries and see the tables it doesn't contain any data it shows zero observations for me and also in log file I don't see any erros , this makes me not able to figure it out where might be the error. Any suggestions would be commendable.
Thank you in andance.
Nothing in that code you show reads any data into SAS.
That code apparently extracts and renames a CSV file. You need additional code to read the CSV file. Either a data step or Proc Import.
You can read your data directly from the zip file and no unzipping necessary.
This blog post illustrates the full process, including getting a zip file downloaded from the internet.
https://blogs.sas.com/content/sasdummy/2014/01/29/using-filename-zip/
I'll comment your code (which you should do) so you understand what it's doing.
%macro unzip(file=, file_out=, path_zip=, path_unzip=);
*Opens the 7-zip application;
%sysexec cd "C:\Program Files\7-Zip";/* deve contenere il path dell'eseguibile zip */
*unzips the zipped file;
%sysexec 7z x "&path_zip.\&file..zip" "-o&path_unzip.";
*Renames the file to the desired file name;
%sysexec rename "&path_unzip.\&file..csv" "&file_out..csv";
%mend;
%unzip; * no parameters passed so that's problematic unless they were declared somewhere else;
You should be calling the macro as follows with parameters, but you didn't either so I don't think anything did happen anyways.
%unzip(file=Sample.zip ,
file_out=Want ,
path_zip=C:\temp\demo\ ,
path_unzip= C:\temp\demo_out\)
UCLA introductory tutorial on macro variables and macros
https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/
Tutorial on converting a working program to a macro
This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md
Examples of common macro usage
https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...
@shailaja3 wrote:
Hi everyone,
I have large code written in the sas main.sas program, my problem is I couldn't able to read the data present in the zip files, when I also unzip the files with the macro written below...,
%macro unzip(file=, file_out=, path_zip=, path_unzip=);
/* Versione pre SAS94 */
%sysexec cd "C:\Program Files\7-Zip";/* deve contenere il path dell'eseguibile zip */
%sysexec 7z x "&path_zip.\&file..zip" "-o&path_unzip.";
%sysexec rename "&path_unzip.\&file..csv" "&file_out..csv";%mend;
%unzip;
but I don't understand where is the problem when I open the libraries and see the tables it doesn't contain any data it shows zero observations for me and also in log file I don't see any erros , this makes me not able to figure it out where might be the error. Any suggestions would be commendable.
Thank you in andance.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.