A ZIP file is useful to compress a file and to combine multiples files with different formats into one file. If we’re speaking about SAS, a ZIP file might come in handy when you have multiple datasets you want to archive together. But, how do you create a ZIP file in SAS?
You create a ZIP file in SAS with the ODS PACKAGE statement. With this statement, you can create a ZIP file and add one or more files of different extensions to it. By default, the ODS PACKAGE statement compresses the size of the files. It isn’t possible to add a password to the ZIP file.
In this article, we create a CSV file and a PDF file, and add them to a ZIP file.
Create a CSV File
The SAS code below creates a CSV file from the CARS dataset from the SASHELP library.
/* CREATE A CSV FILE */ proc export data=sashelp.cars outfile="/folders/myfolders/files_to_zip/cars.csv" dbms=csv replace; run;
Do you know? How to Export a SAS Dataset as a CSV File
Create a PDF File
With the code below we create a PDF file. The PDF file contains summary statistics of the CARS dataset, as well as a histogram of the HorsePower variable.
/* CREATE A PDF FILE */ ods pdf file="/folders/myfolders/files_to_zip/cars.pdf" startpage=no; proc means data=sashelp.cars; run; proc sgplot data=sashelp.cars; histogram HorsePower; run; ods pdf close;
Do you know? How to Create a PDF file in SAS
As you can see in the image below, we have created a CSV file and a PDF file in the file_to_zip folder.
How to Create a ZIP file in SAS?
As mentioned above, you can create a ZIP file in SAS with the ODS PACKAGE statement. To do so, you need to carry out 4 steps:
- Open a ZIP file. You can open the ZIP file with the ODS PACKAGE OPEN statement.
- Add files to the ZIP file. You can add one or more files to the ZIP file with the ODS PACKAGE ADD statement. After this statement, you define the files you want to add with the FILE=-option. You can add files of different extensions.
- Define the location and name of the ZIP file. You define the location and name of the ZIP file with the ODS PACKAGE PUBLISH statement. With the archive properties (i.e., archive_path and archive_name), you define the location and name. You must place the location and name between double-quotes.
- Close the ZIP file. You close the ZIP file with the ODS PACKAGE CLOSE statement.
With the code below we create a ZIP file of the cars.csv file and the cars.pdf document. We call the ZIP file cars.zip and save it in the files_to_zip folder.
/* CREATE A ZIP FILE */ /* 1. Open a ZIP file */ ods package open nopf; /* 2. Define the location and name of the ZIP file */ ods package publish archive properties (archive_path="/folders/myfolders/files_to_zip/" archive_name="cars.zip"); /* 3. Add files to the ZIP file */ ods package add file="/folders/myfolders/files_to_zip/cars.csv"; ods package add file="/folders/myfolders/files_to_zip/cars.pdf"; /* 4. Close the ZIP file */ ods package close;
Note that the code above automatically overwrites existing ZIP files without warning.
After running the code above, SAS has created a ZIP file in the files_to_zip folder.
Besides archiving the CSV file and the PDF file into one ZIP file, the SAS code above also compressed both files. We reduced the size of the CSV file with 71% and the size of the PDF file with (just) 5%.