I'm trying to decompress a file in the zip format using pure SAS code, that I can integrate as one of the steps of a SAS script. I'm running SAS via the web version of SAS Studio on the cloud hosted version (SSOD).
Reading the documentation, I was able to come up with this, which attempts to expand all files in the zip to the same directory:
data _null_; infile "unzip /project/input/file.zip" pipe ; input ; put _infile_; run;
But I couldn't find how to tell it a specific directory to write the files, and I couldn't find how I extract a specific file from the zip file.
If using a hosted SAS, you might not have the unzip command (OS shell) as an option. But you can use the FILENAME ZIP method to list and read files.
You could first extract a list of files, and then extract all files one-by-one to stdout, all by using filename pipe.
See the documentation of unzip: https://linux.die.net/man/1/unzip
If using a hosted SAS, you might not have the unzip command (OS shell) as an option. But you can use the FILENAME ZIP method to list and read files.
That works! Thank you
That doesn't work.
Can you let me know exactly how to extract just a single file from a gz?
Here's my code. The directory is in a Unix server. This crashes even if I add GZIP at the end of the filename clause.
23 GOPTIONS ACCESSIBLE;
24 filename inzip ZIP "/home/jrsousa2/my_content/discogs_20081014_releases.xml.gz";
25
26 /* identify a temp folder in the WORK directory */
27 %let Nome=discogs_20081014_releases.xml;
28 filename xml "%sysfunc(getoption(work))/&Nome" ;
29
30 %put ### %sysfunc(getoption(work));
### /saswork/SAS_workF376000058C4_odaws02-prod-us/SAS_work3611000058C4_odaws02-prod-us
31
32 /* hat tip: "data _null_" on SAS-L */
33 data _null_;
34 /* using member syntax here */
35 infile inzip(&Nome) lrecl=256 recfm=F length=length eof=eof unbuf;
36 file xml lrecl=256 recfm=N;
37 input;
38 put _infile_ $varying256. length;
39 return;
40 eof:
41 stop;
42 run;
ERROR: The file "/home/jrsousa2/my_content/discogs_20081014_releases.xml.gz" exists and is not a zip file. The output file must be
a zip file.
43
44 GOPTIONS NOACCESSIBLE;
45 %LET _CLIENTTASKLABEL=;
46 %LET _CLIENTPROCESSFLOWNAME=;
47 %LET _CLIENTPROJECTPATH=;
48 %LET _CLIENTPROJECTNAME=;
49 %LET _SASPROGRAMFILE=;
50
51 ;*';*";*/;quit;run;
52 ODS _ALL_ CLOSE;
What did you get when you used the gzip option?
What version of SAS do you have? gzip was introduced with 9.4M5.
It seems that I was able to make the code work.
It didn't issue me any error messages, I had to modify the code though.
I ran this on SAS for academics, EG 7.1 or so:
%let Arq=%sysfunc(getoption(work))/releases.xml; filename fromzip ZIP "/home/jrsousa2/my_content/releases.xml.gz" GZIP; filename target "&Arq" encoding='utf-8'; data _null_; infile fromzip; file target ; input; put _infile_ ; run;
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!
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.