BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
the_yeti
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

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.

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.

View solution in original post

6 REPLIES 6
ChrisHemedinger
Community Manager

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.

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.
the_yeti
Obsidian | Level 7

That works! Thank you

jrsousa2
Obsidian | Level 7

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;

jrsousa2
Obsidian | Level 7

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;

 

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 17508 views
  • 4 likes
  • 4 in conversation