How to zip and Unzip files using SAS?
This post will you to create zip files or unzip files using SAS. There 3 different methods you can use to zip or unzip files. All the method requires that NOXCMD is enabled in your SAS system.
Do you want to zip or unzip files using SAS?
This post will you to create zip and unzip files using SAS. There are 3 different methods you can use to zip and unzip files in SAS. All the method requires that NOXCMD is enabled in your SAS system.
Method 1: Zip Files in SAS Using the X command
If the NOXCMD is not set, you can use the zip command as below.
x 'zip archivename.zip filename1 filename2 filename3';
x 'zip /home/9to5sas/external_files/inputs.zip /home/9to5sas/archives/Sales_2020.xlsx';
Method 2: Using pipe command to zip files
Filename Pipe is another means of executing Unix commands within your SAS program.
In general, it behaves like any other filename statement – it accepts data written from SAS (FILE/PUT) or provides data for SAS to read (INFILE/INPUT).
It does differently because the data comes from/is sent to a UNIX command rather than working with a simple file.
Using the PIP engine on an INFILE statement, the SAS data step can read any messages that the command generates. It will enable you to debug if the UNIX command doesn’t work.
filename oscmd pipe "zip /home/9to5sas/input_files/archive.zip /home/9to5sas/input_files/sales_2020.xlsx 2>&1"; data _null_; infile oscmd; input; put _infile_; run;
2>&1 reroutes stderr (standard error) output to standard output, so you get it in your data step.
After running your command like this, any messages that are returned by the system will appear in the SAS log.
adding: home/9to5sas/input_files/sales_2020.xlsx (deflated 25%)
Often, you’ll create a zip archive of a directory including the content of subdirectories. The
-r option allows you to traverse the whole directory structure recursively:
x 'zip -r archivename.zip directory_name';
The default compression method of Zip is deflate. If the zip utility determines that a file cannot be compressed, it simply stores the file in the archive without compressing it using the store method. In most Linux distributions, the zip utility also supports the bzip2 compression method.
To specify a compression method, use the
filename oscmd pipe "zip -Z bzip2 /home/9to5sas/input_files/archive1.zip /home/9to5sas/input_files/sales_2020.xlsx 2>&1";
adding: home/9to5sas/input_files/sales_2020.xlsx (bzipped 18%)
zip command allows you to specify a compression level using a number prefixed with a dash from 0 to 9. The default compression level is
-6. When using
-0, all files will be stored without compression.
-9 will force the
zip command to use an optimal compression for all files.
For example, to use the compression level
-9, you would type something like this:
x 'zip -9 -r archivename.zip directory_name';
The higher the compression level, the more CPU-intensive the zip process is, and it will take more time to complete.
Method 3: Zip files Using Systask command
We can also use the SYSTASK command to zip or unzip files in SAS. By default SYSTASK command executes asynchronously with SAS, therefore if we need to wait for the result of the SYSTASK command before we continue we need to use WAITFOR or
WAITFOR option suspends the execution of the SAS program until one or a few SYSTASK commands are completed. In the following example, we have used the WAIT option to ensure that
data _null_ step continues after the SYSTASK command is completed.
systask command 'zip /home/9to5sas/input_files/inputs.zip /home/9to5sas/input_files/sales.xlsx' wait status=zipfiles; data _null_; if &zipfiles>0 or &sysrc>0 then do; put 'ERROR Occured during the zip statement'; abort abend; end; else put 'NOTE: Archiving completed successfully'; run;
The returned code from
SYSTASK command is stored in the automatic macro variable
If the value of
SYSRC=0 then the SYSTASK command has been executed successfully, otherwise if
SYSRC>0 some problems have occurred.
SYSRC variable works the same way for SYSTASK command as for the X command. The status macro variable ‘zipfiles’ indicates if the zip command was successful or not and returns an error code from the operating system command.
Creating Split Zip File
Imagine you want to store the Zip archive on a file hosting service that has a file size upload limit of 1GB, and your Zip archive is 5GB.
You can create a new split Zip file using the
-s option followed by a specified size. The multiplier can be k (kilobytes), m (megabytes), g (gigabytes), or t (terabytes).
x 'zip -s 1g -r archivename.zip directory_name';
The command above will keep creating new archives in a set after it reaches the specified size limit.
archivename.zip archivename.z01 archivename.z02 archivename.z03 archivename.z04
How to zip all files in the current directory?
To create a Zip archive of all the files in the current directory use the zip command followed by the archive name and a * symbol.
x 'cd /home/9to5sas/input_files/input_files/'; x 'zip archive.zip *'
How to zip all .csv files in the current directory?
To zip all CSV files in the current directory use the below zip command.
'x zip /home/9to5sas/archivename.zip /home/9to5sas/inputs/*.csv'
How to Unzip files using SAS?
Similar to the zip command you can use the unzip command to unzip the files. Like any other Unix commands unzip can also be used in the x statement, pipe command or the systask command.
filename oscmd pipe "unzip -d /home/9to5sas/input_files/ /home/9to5sas/input_files/inputs.zip"; data _null_; infile oscmd; input; put _infile_; run;
The -d option is used to uncompress files to a directory.