Using the WinZip Command Line Utility in SAS Kenneth W. Borowiak, PPD, Inc. Michael Kwee, PPD, Inc. - PDF

Please download to get full document.

View again

of 7
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information Report
Category:

Automobiles

Published:

Views: 8 | Pages: 7

Extension: PDF | Download: 0

Share
Related documents
Description
Using the WinZip Command Line Utility in SAS Kenneth W. Borowiak, PPD, Inc. Michael Kwee, PPD, Inc. ABSTRACT A tidy way to send, receive and archive project materials is to put them in a zip file. For
Transcript
Using the WinZip Command Line Utility in SAS Kenneth W. Borowiak, PPD, Inc. Michael Kwee, PPD, Inc. ABSTRACT A tidy way to send, receive and archive project materials is to put them in a zip file. For ad hoc tasks, manually adding materials to and extracting from a zip file is not unusual. However, a hands-free approach for repetitive zipping and unzipping is desirable. This paper explores using the WinZip Command Line utility in SAS programs to streamline common tasks. INTRODUCTION WinZip is a popular compression software for Windows that is commonly used with its graphical user interface. For ad hoc tasks, using the GUI to add materials to and extracting from a zip file is not unusual. However, a hands-free approach for repetitive zipping and unzipping is desirable. The WinZip Command Line Support Add-On provides a command line interface that provides the ability to use WinZip without the GUI. This paper will provide a series of examples demonstrating the use of the WinZip Command Line Utility (WCLU) in SAS code. While calling the WCLU can be accomplished a number ways in SAS such as the X and %sysexec statements, all examples provided the context of a DATA step using the SYSTEM call routine 1. The DATA step allows for systematic building of the command when complications arise from quoting, embedded spaces in paths and filenames and resolving macro variables. The examples begin using the files the four RTF files and 5 SAS data sets in the directory C:\NESUG, as shown below in Figure 1. The code to generate the files can be found in the Appendix at the end of the paper. The examples were tested with WinZip Version 9.0 and WinZip Command Line Support Add-On 1.0. /* Figure 1 Sample Directory */ 1 Using the SYSTEM function to capture the return code from the WCLU call is ideal, but the current set of return codes are sparse and do not allow for detailed error trapping. 1 ZIPPING The command format for populating a zip file is: winzip32 action [options] filename.zip files where: winzip32 - Refers to the call to the WinZip executable action - The activity being performed, such as adding or moving files into a zip file options Examples of options are adding a password or specifying the compression method filename.zip Name of the zip file created files The files to be put in the zip file In our first example, consider adding the AE data set to a zip file with the following DATA step in Figure 2. The variable ZIPEXE contains the entire path to the WinZip executable, which contains a space, so it needs to be encapsulated within a pair of double quotes. The field also contains the min option, which says to run in a minimized state. When this option is invoked it must appear as the first option. The a option says that the file referenced in the FILE variable will be added to the zip file referenced in the ZIPFILE variable. Note that entire path is included in the FILE and ZIPFILE variables. The command to be processed is built in CMD variable, which serves as the input to the SYSTEM call routine. /* Figure 2 - Add a file to a zip file */ zipexe=' c:\program Files\WinZip\WinZip32.exe -min -a' ; zipfile='c:\nesug\example1.zip' ; Examples: file='c:\nesug\ae.sas7bdat '; cmd=zipexe ' ' zipfile /* Example ' ' 1 file - Add ; a file to a zip file * data putlog _null_; NOTE-Processing command cmd ; zipexe=' c:\program call system( cmd ) Files\WinZip\Winzip32.exe ; -min -a'; zipfile='c:\nesug\example1.zip' ; file='c:\nesug\ae.sas7bdat '; cmd=zipexe ' ' zipfile ' ' file ; putlog NOTE-Processing command cmd ; call Processing system( cmd command ) ; C:\Program Files\WinZip\Winzip32.exe -min -a run C:\NESUG\Example1.zip ; C:\NESUG\ae.sas7bdat 2 Some of the strings in the first example could have been shortened, as shown in Figure 3. First, the ZIPEXE fields uses the short file naming convention where only the first characters of a filepath are displayed followed by ~1. Since the path to the WinZip executable no longer contains an embedded space it no longer needs to be surrounded by double quotes. Secondly, since the location of the file to be zipped and the output zip file reside in the same location, changing the location of the system directory removes the need to explicitly state in the ZIPFILE and FILE variables. /* Figure 3 Shortening strings in the command */ cmd1='cd C:\NESUG' ; putlog NOTE-Processing command cmd1 ; call system( cmd1 ) ; zipexe='c:\progra~1\winzip\winzip32.exe -min -a' ; zipfile='example1b.zip' ; file='ae.sas7bdat '; In cmd2=zipexe the next example, consider ' ' moving zipfile multiple ' files ' (i.e. file AE and ; CM data sets) into a zip file Example2.zip. putlog NOTE-Processing The DATA step below command looks similar cmd2 to the ; one in the first example, but the a option call is system( replaced with cmd2 the ) m ; option and the files to be moved a listed and separated by a space. In the next example in Figure 4 multiple data sets are moved to a zip file. The AE and CM data sets are listed in the FILES field separated by a space and physically moved from their current location in the NESUG folder to the zip file Example2.zip. /* Figure 4 - Move multiple files to a zip file */ zipexe=' c:\program Files\WinZip\Winzip32.exe -min -m ' ; zip= c:\nesug\example2.zip ; files='c:\nesug\ae.sas7bdat C:\NESUG\cm.sas7bdat'; cmd=zipexe ' ' zip ' ' files ; Processing command C:\Program Files\WinZip\Winzip32.exe -min -m C:\NESUG\Example2.zip C:\NESUG\ae.sas7bdat C:\NESUG\cm.sas7bdat Some other useful features for zipping are demonstrated in Figure 5. The asterisk can be used as a wildcard, where in the example below it is used to add all files in the NESUG directory to a zip file. It could also have been used to add all SAS data sets (e.g. *.sas7bdat) or all files that begin with the letter T (e.g. T* ). A password is also added to secure the zip file with the s option, where the actual password directly follows it without a space. Passwords containing a space should be enclosed with double quotes. 3 /* Figure 5 - Use a wildcard to add all files in a directory to a zip file */ /* Add a password to the zip file */ zipexe=' c:\program Files\WinZip\Winzip32.exe -min -a -s' ; pw=' myob ' ; zipfile= c:\nesug\example3.zip ; files='c:\nesug\*'; cmd=zipexe pw ' ' zipfile ' ' files ; Processing command C:\Program Files\WinZip\Winzip32.exe -min -a -s myob C:\NESUG\Example3.zip C:\NESUG\* UNZIPPING Using the WCLU for unzipping files is similar to zipping, where the main difference is the use of the e command for extraction. The other difference is that you need to specify the desired location of the extracted files. The example below if Figure 6 demonstrates extracting all the files in the Example2.zip zip file to the NESUG folder. /* Figure 6 - Extract all files from a zip file */ unzipcmd=' c:\program Files\WinZip\Winzip32.exe -min -e ' ; zipfile= c:\nesug\example2.zip ; whereto='c:\nesug' ; cmd=unzipcmd ' ' zipfile ' ' whereto ; Processing command C:\Program Files\WinZip\Winzip32.exe -min -e C:\NESUG\Example2.zip C:\NESUG When unzipping a file to a directory it is possible that a version of that file may already exist, in which case WinZip will ask you if you want to replace the file. To avoid this manual intervention, particularly when running your program in batch mode, you can use the o option to overwrite any files in conflict without being prompted. This is demonstrated in Figure 7, where a password is also provided in order to extract just the AE data set from the Example3.zip zip file. 4 /* Figure 7 - Extract a file from a zip file */ /* Provide the password */ /* Replace the file if it already exists */ unzipcmd=' c:\program Files\WinZip\Winzip32.exe -min -e -o -s' ; pw= myob ; zipfile= c:\nesug\example3.zip ; whereto='c:\nesug' ; file='ae.sas7bdat' ; cmd=unzipcmd pw ' ' zipfile ' ' whereto ' ' file ; Processing command C:\Program Files\WinZip\Winzip32.exe -min -e -o -smyob C:\NESUG\Example3.zip C:\NESUG ae.sas7bdat ZIP CONTENTS You may have the need to extract a list of files contained in a zip file. This can be accomplished with a FILENAME pipe to call the WZUNZIP executable with the v option, as demonstrated in Figure 8. The view option v is followed by a second option b, for a brief description of the contents. Information such as the file size and compression ratio is provided for each of the elements in the zip file. The DATA step FOO can be augmented with steps to extract a list of a certain type of file (e.g. RTF) using character string functions and call routines, such as PRX. 5 /* Figure 8 - Extracting the contents of a ZIP file */ filename= filename foo pipe ' ; viewzip=' C:\Program Files\WinZip\WZUnzip.exe -vb' ; zipfile= c:\nesug\example3.zip ; fnstmt= filename ' ' viewzip '' zipfile ' ; ; call execute( fnstmt ) ; data foo ; infile foo truncover ; input x $300. ; Obs x 1 WinZip(R) Command Line Support Add-On Version 1.1 SR-1 (Build 6224) 2 Copyright (c) WinZip Computing, Inc All Rights Reserved 3 4 Zip file: C:\NESUG\Example3.zip 5 6 Length Size Ratio Date Time Name % 15/08/ :29 ae.sas7bdat % 15/08/ :29 cm.sas7bdat % 15/08/ :29 dm.sas7bdat % 15/08/ :29 ds.sas7bdat % 15/08/ :29 mh.sas7bdat % 15/08/ :29 T rtf % 15/08/ :29 T rtf % 09/08/ :27 T rtf CONCLUSION This paper provided a series of examples to script the addition and extraction of files from a zip files using the WinZip Command Line Utility. Not all of the functionality and options of the WCLU were discussed, such as alternative levels of encryption and compression, but the curious reader should refer to its documentation. Additional examples and related papers can be found in the References section. 6 ACKNOWLEDGEMENTS 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. The authors would like to thank Jim Worley and Kipp Spanbauer for their comments in their careful review of the paper. CONTACT INFORMATION Your comments and questions are valued and encouraged. Contact the authors at: Kenneth W. Borowiak Michael Kwee PPD, Inc. PPD, Inc Paramount Parkway 3900 Paramount Parkway Morrisville, NC Morrisville, NC REFERENCES Borowiak, Kenneth (2008), PRX Functions and Call Routines: There is Hardly Anything Regular About Them, Proceedings of the 21 st Annual NorthEast SAS Users Group Brennan, David (2006), A Quick Guide to the WinZip Command Line Add-On, PhUSE Proceedings Sample 26011: Reading zipped files on Windows - WinZip Command Line Support Add-On Release 1.0 APPENDIX libname NESUG C:\NESUG ; data nesug.ae nesug.cm nesug.dm nesug.ds nesug.mh nesug.vs ; set sashelp.class ; x 'cd C:\NESUG' ; %macro sample_table(dsn=, tableno=, where= ) ; ods rtf file= &tableno..rtf style=minimal ; proc print data=&dsn. label ; %if %length(&where.) 0 %then where &where.;; ods rtf close ; %mend ; %sample_table(dsn=nesug.ae, tableno=t140101) %sample_table(dsn=nesug.ae, tableno=t140202) %sample_table(dsn=nesug.ae, tableno=t , where=%str(sex='f')) %sample_table(dsn=nesug.ae, tableno=t , where=%str(sex='m')) 7
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks