Creating ZIP Files With ODS: Jack Hamilton, Division of Research, Kaiser Permanente, Oakland, California
Creating ZIP Files With ODS: Jack Hamilton, Division of Research, Kaiser Permanente, Oakland, California
Creating ZIP Files With ODS: Jack Hamilton, Division of Research, Kaiser Permanente, Oakland, California
Paper 131-2013
INTRODUCTION
The ZIP file format was devised in the late 1980's by Phi Katz for his PKZIP utility. It has been updated over the years, and now incorporates several different compression algorithms. The ZIP algorithms are not always the most effective or efficient - there are several competitors that might work better in any given situation, that might compress better or faster or both - but it's a good overall performer. As a result, the ZIP format now used internally by many products (Java JAR files, SAS Enterprise Guide project files, etc.), in addition to being a standard format for distributing groups of files that a consumer might extract and use directly. Despite its wide use in the computer industry, reading and writing ZIP files has not historically been supported by SAS, even though DSD uses ZIP format files internally. Starting with SAS 9.2, it is possible to create ZIP files using SAS code. There is still no supported method for unzipping files (ask Mr. Google about "SAS saszipam " for an unsupported, undocumented method that often works). Rumor has it that an upcoming version of SAS will support reading and writing ZIP files through a new FILENAME engine.
ODS PACKAGES
Support for writing ZIP files in SAS 9.2comes as part of ODS, the source of so many great new features in SAS over the last few years. Its stated purpose is to create file packages as part of the SAS Publishing Framework, which is part of SAS's extensive product offerings for information distribution. But it's available in base SAS.
You will see a message in the log confirming that the ZIP file has been written: NOTE: Writing zip package c:\temp\ProdOutput.zip. Finally, tell SAS you're through adding files, using the CLOSE option: ods package(ProdOutput) close; If you look at the ZIP file (in this case, using the free utility 7-Zip), you can see the contents of the ZIP file (Figure 1):
AUTOMATING ZIPPING
The examples above assume that you know the names of all the files you will want to add to the ZIP file. If the file names vary, but you have the names in a SAS data set, you can write the ADD statements with CALL EXECUTE. filename dirlist pipe 'dir /b y:\wuss2012\*.sas'; data _null_; infile dirlist pad lrecl=80; input @1 filename $80.; call execute (catx (' ', 'ods package(vary)', 'add file=', quote('y:\wuss2012\' || trim(filename)), ';' ) ); run; You can create the data set containing file names while you are creating the files themselves, or in whatever other way you choose. For a platform-independent method of creating a list of files and directories in SAS, see http://www.sascommunity.org/wiki/Obtaining_A_List_of_Files_In_A_Directory_Using_SAS_Functions
FURTHER READING
A version of this paper is available online at http://www.sascommunity.org/wiki/Creating_a_ZIP_File_Using_Native_SAS_Features
REFERENCES
SAS Institute. Using ODS to Publish Packages. March 2013. Available at <http://support.sas.com/rnd/base/ods/odspackages/index.html>.
ACKNOWLEDGMENTS
I would like to thank Jamila Gul at the Kaiser Permanente Division of Research and Tom Billings at Union Bank for their help and encouragement.
CONTACT INFORMATION
Your comments and questions are encouraged. Contact the author at: Jack Hamilton jfh@acm.org
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. indicates USA registration. Other brand and product names are trademarks of their respective companies.