This post was kindly contributed by The SAS Dummy - go there to comment and to read the full post. |
In previous articles, I’ve shared tips about how you can work with SAS and ZIP files without requiring an external tool like WinZip, gzip, or 7-Zip. I’ve covered:
- How to create ZIP files with ODS PACKAGE ZIP (available since SAS 9.2)
- How to “unzip” and read ZIP files using FILENAME ZIP (SAS 9.4 and later)
- How to create and update ZIP files with FILENAME ZIP (SAS 9.4 and later)
But a customer approached me the other day with one scenario I missed: how to add SAS data sets to an existing ZIP file. It’s a variation of a tip that I’ve already shared, but with two differences. First, in order to add a data set to a ZIP file, you have to know its physical filename — not just the LIBNAME.MEMBER reference that you use in SAS procedure steps. And second, I had not shown how to add a new file to an existing ZIP archive — though it turns out that’s pretty simple.
Find the file name for a SAS data set
There are several ways to do this. For my approach, I used the output from PROC CONTENTS. Notice that I had to capture the ODS output (not the OUT= data set) to grab the file name. I wrapped it in a macro for easy reuse. And since I ultimately need a SAS fileref to map to the path, I’ve assigned one (data_fn) in my macro.
/* macro to assign a fileref to a SAS data set in a Base library */ %macro assignFilerefToDataset(_dataset_name); %local outDsName; ods output EngineHost=File; proc contents data=&_dataset_name.; run; proc sql noprint; select cValue1 into: outDsName from work.file where Label1="Filename"; quit; filename data_fn "&outDsName."; %mend; |
How to add a new member to a ZIP file
Now that I have the source file, I need to designate a destination file in a ZIP archive. The FILENAME ZIP method will create a new ZIP file if one does not yet exist, or it can add to an existing ZIP. To ensure I’m starting from scratch, I assign a simple fileref to my target destination and then delete the file.
/* Assign the fileref - basic file method */ filename projzip "&projectDir./project.zip"; /* Start with a clean slate - delete ZIP if it exists */ data _null_; rc=fdelete('projzip'); run; |
To create a new ZIP file and designate a path and file name within it, I used the FILENAME ZIP method with the MEMBER= option. Note that I specified the “data/” subfolder in the MEMBER= value; this will place the file into a named subfolder within the archive.
/* Use FILENAME ZIP to add a new member -- CLASS */ /* Put it in the data subfolder */ filename addfile zip "&projectDir./project.zip" member='data/class.sas7bdat'; |
Then finally, I need to actually “copy” the file into the archive. I do this by streaming the source file into the target fileref byte-by-byte:
/* byte-by-byte copy */ /* "copies" the new file into the ZIP archive */ data _null_; infile data_fn recfm=n; file addfile recfm=n; input byte $char1. @; put byte $char1. @; run; filename addfile clear; |
That’s it! I now have a ZIP file with one member entry. Now I can “press repeat” to add a second entry:
%assignFilerefToDataset(sashelp.cars); /* Use FILENAME ZIP to add a new member -- CARS */ /* Put it in the data subfolder */ filename addfile zip "&projectDir./project.zip" member='data/cars.sas7bdat'; /* byte-by-byte copy */ /* "copies" the new file into the ZIP archive */ data _null_; infile data_fn recfm=n; file addfile recfm=n; input byte $char1. @; put byte $char1. @; run; filename addfile clear; |
Optional: Report on the ZIP file contents
If I want to report on the total contents of the ZIP file now, here’s a DATA step and PROC CONTENTS step that does the job:
/* OPTIONAL for reporting */ /* Report on the contents of the ZIP file */ /* Assign a fileref wth the ZIP method */ filename inzip zip "&projectDir./project.zip"; /* Read the "members" (files) from the ZIP file */ data contents(keep=memname); length memname $200; fid=dopen("inzip"); if fid=0 then stop; memcount=dnum(fid); do i=1 to memcount; memname=dread(fid,i); output; end; rc=dclose(fid); run; /* create a report of the ZIP contents */ title "Files in the ZIP file"; proc print data=contents noobs N; run; |
Result:
Files in the ZIP file memname --------------------- data/class.sas7bdat data/cars.sas7bdat N = 2
I hope that this helps to make the FILENAME ZIP method more useful to those who want to try it out. I’m sure that there will be more scenarios that people will ask about; someday, if I write enough blog posts, I’ll have it all covered!
Sample program: You can view/download the entire SAS program (containing the snippets I’ve featured and more) from my GitHub profile.
The post Add files to a ZIP archive with FILENAME ZIP appeared first on The SAS Dummy.
This post was kindly contributed by The SAS Dummy - go there to comment and to read the full post. |