SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
dustychair
Pyrite | Level 9
filename indata pipe 'dir P:\ALG/b';
/*to read the name of files in a folder*/
data file_list_ALG;
length fname $70;
infile indata truncover;
input fname $70.;
call symput ('num_files_alg',_n_);
run;
filename inzip ZIP 'P:\ALG\????';
 
/* Read the "members" (files) from the ZIP file */
data contents(keep=memname isFolder);
 length memname $200 isFolder 8;
 fid=dopen("inzip");
 if fid=0 then
  stop;
 memcount=dnum(fid);
 do i=1 to memcount;
  memname=dread(fid,i);
  /* check for trailing / in folder name */
  isFolder = (first(reverse(trim(memname)))='/');
  output;
 end;
 rc=dclose(fid);
run;



filename xl "%sysfunc(getoption(work))/?????.txt" ;
 
/* hat tip: "data _null_" on SAS-L */
data _null_;
   /* using member syntax here */
   infile inzip(?????.txt) 
       lrecl=256 recfm=F length=length eof=eof unbuf;
   file   xl lrecl=256 recfm=N;
   input;
   put _infile_ $varying256. length;
   return;
 eof:
   stop;
run;

Hi all,

I have a folder that includes zipped folders. I want to unzip those files and get the data. Here is what i am trying to do:

1. At the first step i am getting name of zipped folders.

2. Then i am trying to use those names. The question marks mean how I can use those folders name that I got from the first step.

3.I am trying to use the files in the folders which i get from the second step. 

 

Since i have more than one zipped folder i don't want to do manually.

any ideas??

 

Thanks

3 REPLIES 3
Tom
Super User Tom
Super User

It is not clear what you are trying to do with the ZIP files.

Are you trying to find out the names of the files in the ZIP files?

Or are you trying to unzip the files?   Since it looks like you can run operating system commands why not just run an operating system command that can unzip the zip file?

 

But whatever you are trying to do get it to work for ONE zip file.

Convert that code into a macro that takes the name of the ZIP file as input.  So something like:

%macro unzip(filename);
filename zip zip "&filename";
....
%mend unzip;

Then use the data from your first data step (the list of file names) to call that macro once for each file.

data _null_;
  set file_list_ALG;
  where lowcase(scan(fname,-1,'.'))='zip';
  call execute(cats('%nrstr(%unzip)(',fname,')'));
run;
dustychair
Pyrite | Level 9

Thank you @Tom. Long time ago I asked how to read (I meant unzip) csv files, you answered my question again but I could not resolve it. so, since i still don't know how to unzip files i am trying to find a way to do it. i used @ChrisHemedinger's example but since I have more than one folder i failed.

that's the story of my question. 

simple and silly question: Should i use "filename" in the parenthesis after unzip?

Tom
Super User Tom
Super User

On my Unix system where SAS runs I can use the command unzip to unzip a ZIP file. The syntax is just 

 

unzip filename

Where FILENAME is the name of the zip file.  What ever files were in the ZIP file will be written to the current directory.  If the files in the ZIP are organized into folders then those folders are created under the current directory.

 

So if I wanted the unzip the files in /home/xyz/FRED.ZIP and write them into the directory /home/xyz/target I could just run this code:

data _null_;
  infile "cd /home/xyz/target ; unzip /home/xyz/FRED.ZIP" pipe ;
  input;
  put _infile_;
run;

So the easiest thing to do is to find out what is the command line command you can use to unzip a file on the machine where SAS is running and then generate that command.  For example a lot of users are using 7Zip as the tool for working with ZIP files.

 

Should i use "filename" in the parenthesis after unzip?

I assume you are asking what you should pass to the macro when you call it?  That is totally up to you and depends on how you write the macro.  In the example I posted the macro parameter FILENAME is used to create a SAS fileref using the ZIP engine.  So the value that it should have is the fully qualified name of the a ZIP file.  So something like one of these depending on whether SAS it running on Windows or Unix.

%unzip(c:\users\xyz\myfile.zip)
%unzip(/home/xyz/myfile.zip)

Once you have a flieref named ZIP that points to a ZIP file you can open it with the DOPEN() command and use the DREAD() command to get the list of members in the file.

 

Once you have one member name in the ZIP you could use that to point to that specific member in that specific zip file by using syntax like:

filename next zip "zipfilename" member="membername" ;

Which you might be able to use the FCOPY() function to copy to an actual physical file somewhere.  Make sure you create a valid filename for the new file (and one that does not conflict with any existing file).

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 3 replies
  • 1029 views
  • 0 likes
  • 2 in conversation