Read ZIP or GZ Files in SAS

Deepanshu Bhalla 6 Comments
This tutorial describes how to read or unzip .ZIP or .GZ files in SAS. When i tried to unzip files in SAS first time, i struggled a lot. It's a little bit tricky to read .GZ files into SAS directly.

Steps to unzip GZ files in SAS

1. Download GZIP software
2. Extract file and copy path of gzip.exe
3. Replace the highlighted line of code below with your path
filename foo pipe '"C:\Users\Deepanshu\Downloads\gzip124xN\gzip.exe" -cd C:\Users\Deepanshu\Downloads\Newfolder\20090827.gz' ; 
DATA mydata;
INFILE foo dlm='|' lrecl=32767 dsd;
length sym $ 10 se $ 2 cf $ 2;
input sym $ se $ OP HP LP CP LTP TQ VS NT  cf$ ;
RUN;
If you encounter the following issues while importing zipped files in SAS, it means either you have not downloaded GZIP software or you have not assigned path of executable GZIP file.

  1. 'gzip' is not recognized as an internal or external command, operable program or batch file
  2. cannot locate the end of the central directory 


Prior to SAS 9.4 : Steps to unzip ZIP files

Download WINZIP software.

The following example reads the cars.txt file from the cars.zip file.
filename foo pipe '"C:\Users\Deepanshu\Winzip\winzip.exe" -o -c
                     c:\cars.zip   cars.txt' ;
DATA mydata;
INFILE foo dlm='|' lrecl=32767 dsd;
length sym $ 10 se $ 2 cf $ 2;
input sym $ se $ OP HP LP CP LTP TQ VS NT  cf$ ;
RUN;
SAS 9.4  : Steps to unzip ZIP files

In SAS 9.4, FILENAME ZIP was introduced to read and import ZIP files.

filename foo ZIP 'C:\cars.zip' member="cars.txt" ;
DATA mydata;
INFILE foo dlm='|' lrecl=32767 dsd;
length sym $ 10 se $ 2 cf $ 2;
input sym $ se $ OP HP LP CP LTP TQ VS NT  cf$ ;
RUN;
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

6 Responses to "Read ZIP or GZ Files in SAS"
  1. Doesn't SAS 9.4 include a built-in method to read ZIP files without external ZIP software?

    ReplyDelete
    Replies
    1. Yes you are right. SAS 9.4 includes a built-in method to read ZIP files. As most of SAS users are still using version of SAS prior to 9.4, it would be required to have external ZIP software. I have also added FILENAME ZIP method for SAS 9.4 users.

      Delete
  2. I have a .gz file on my server (linux) and have 7z on my local machine , can i will be able to read the .gz file. task is to read the no. of file inside it and i am willing to use dopen , DNUM AND DREAD to get it done.

    ReplyDelete
  3. I have a .gz file on my server (linux) and have 7z on my local machine , can i will be able to read the .gz file. task is to read the no. of file inside it and i am willing to use dopen , DNUM AND DREAD to get it done.

    ReplyDelete
  4. Great tutorial, thanks. I use zip files all my life, and think that its very comfortable to do this especially when you are beginner in PC. Here guide how to open .zip file https://wikiext.com/zip, so I wanna u to try it and then write me your expert's opinion about it.

    ReplyDelete
Next → ← Prev