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.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.

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.

It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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