BookmarkSubscribeRSS Feed
sas_lak
Quartz | Level 8

Hello All,

How can i import the txt file which is compressed into    .ZIP or .Gz or .7z

using infile statement from (windows) SAS 9.2 version.

11 REPLIES 11
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You need to decompress the file first, there is no functionality at this time to unzip within SAS.  You can either do it manually when you receive the file, or pipe an OS command out using x or call system, e.g.:

data _null_;

     winzip_path="'c:\programs\winzip\winzip.exe'";  /* Note the double quotes around single quotes */

     filepath="'s:\zips\myzip.zip'";

     cmd=winzip_path||" "||filepath;

     x cmd;

run;

sas_lak
Quartz | Level 8

Thanks for quik reply, but every day we received number of files and each compressed file having minimum 5GB,

is there any other option to import into SAS.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Why not run a script in your OS to unzip the files.  They will need to be unzipped to be used.

Patrick
Opal | Level 21

You can unzip & read the files "on-the-fly" with syntax as suggests.

With SAS 9.4 there is now also a zip engine available which would allow you to read such archives

SAS(R) 9.4 Statements: Reference, Third Edition

Kurt_Bremser
Super User

filename oscmd pipe "gzip -dc &infilename";

data import;

infile oscmd;

/* process infile as if unzipped */

run;

This will stream the unzipped contents of the file specified in infilename to your data step.

Consult the proper manuals for other compressed file formats and the correpsonding utilities.

Babloo
Rhodochrosite | Level 12

May I request you to explain the "gzip -dc &infilename" from your filename statement?

Thanks.

Patrick
Opal | Level 21

I suggest that you do a Google search using "site:support.sas.com filename pipe". Also searching here in this forum with terms like "filename pipe zip" will return quite a few links.

"gzip -dc &infilename"


gzip -dc           Is calling the 3rd party GZIP tool with appropriate switches for this tool

&infilename     A SAS macro variable which you could define and populate earlier in your code. This is the path and name of your GZIP archive which you pass in as parameter into GZIP


The example is for GZIP. What's the right 3rd party tool for your archive will depend on what's available in your specific environment.

Kurt_Bremser
Super User

gzip:

-d: decompresses the file (without the -d gzip will do a compression)

-c: instead of writing a compressed/decompressed file, stream the output to stdout

The filename pipe works similar to the reverse apostroph (backtick) under UNIX. The string is executed, and the output is provided to SAS as if you read a file.

The filename pipe is also very useful if you want to include the output and error messages of a command in the SAS log:

filename oscmd pipe "what_you_want_to_execute 2>&1"; * 2>&1 redirects stderr to stdout, so you get everything;

data _null_;

infile oscmd;

input;

put _infile_; * write the whole input line to the log;

run;

As Patrick suggested, you may need to adapt the command to the zip tool available in your environment. All those tools should have options for batch processing.

sas_lak
Quartz | Level 8

Thank you one and all,

Here I am receiving minimum 50 compressed files in a week time, and it contains approximate 100 GB.

Every time I used to uncompressed and import flat files into SAS. It consumes huge disc space.

Any way am deleting the all files, once SAS datasets are placed into my libelers.

Thank you very much for your suggestions.

Patrick
Opal | Level 21

Content of post deleted as wrong and being convinced otherwise by

Kurt_Bremser
Super User

Actually, gzip -dc will not use any diskspace at all. So the pipe approach as described only needs the space for the zipped file and the space for the SAS dataset.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 6921 views
  • 15 likes
  • 5 in conversation