BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
the_yeti
Obsidian | Level 7

I'm generating a CSV file from a SAS dataset, and I'd like to compress it to zip before downloading it.

 

proc export data=sashelp.cars
  outfile=_dataout
  dbms=csv replace;
run;

%let _DATAOUT_MIME_TYPE=text/csv;
%let _DATAOUT_NAME=cars.csv;

 

How can I do that?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

<spock>Fascinating</spock>. 7-zip can open the file, while Windows Explorer shows it empty. UNIX unzip (AIX) comes back with

Archive:  cars.csv.zip
mapname:  conversion of  failed

unzip -l shows

Archive:  cars.csv.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
    37713  05-14-2018 09:34   
---------                     -------
    37713                     1 file

so there seems to be an unnamed entry that confuses some zip programs.

 

This research led me to reread the documentation of filename zip (following Maxim 1), and I came up with this solution:

filename _dataout zip "$HOME/sascommunity/cars.csv.zip" member="cars.csv";

proc export data=sashelp.cars
  outfile=_dataout
  dbms=csv replace;
run;

The unzip -l now shows

  Length      Date    Time    Name
---------  ---------- -----   ----
    37713  05-14-2018 09:50   cars.csv
---------                     -------
    37713                     1 file

and UNIX unzip works. Similarly, Windows Explorer now shows the file, and I guess WinZip will also work.

 

BTW filename zip with the gzip option, as suggested by @ChrisHemedinger, works perfectly without any additional options.

 

PS all my tests run on SAS 9.4M5 and AIX, Windows tests on Windows 7.

View solution in original post

16 REPLIES 16
the_yeti
Obsidian | Level 7

Thank you Kurt.

 

Could you point me to an example?

 

I tried a few different combinations, but no luck...

 

proc export data=sashelp.cars
  outfile=test.zip zip
  dbms=csv replace;
run;
proc export data=sashelp.cars
  outfile=test zip
  dbms=csv replace;
run;
proc export data=sashelp.cars
  outfile='test.zip' zip
  dbms=csv replace;
run;
the_yeti
Obsidian | Level 7

Thank you Kurt.

 

That code seems to generate some sort of compressed file, instead of the plain text CSV, however it doesn't seem to be in the ZIP format... It's some other compression I'm afraid.

 

Is there any hint I can use in the `filename` instruction to tell it o use LZMA, etc?

 

This is the beginning of the file:

 

compressed-file.png

the_yeti
Obsidian | Level 7

Hmm I think something odd is happening. The file cannot be opened by Windows. It shows an empty folder.

ChrisHemedinger
Community Manager

Running on a 9.4 M5 environment?  Try GZIP.

 

filename _dataout zip "/path/cars.csv.gz" GZIP;

proc export data=sashelp.cars
  outfile=_dataout
  dbms=csv replace;
run;
Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.
the_yeti
Obsidian | Level 7

Thank you Chris.

 

I think my environment might not support GZIP for some reason... I get the following error message:

 

ERROR: Error in the FILENAME statement.
ERROR 23-2: Invalid option name GZIP.
ERROR: Insufficient authorization to access /sso/biconfig/940/Lev1/SASApp/_DATAOUT.

 

The code I'm running:

 

filename _dataout zip "/path/that/I/know/I/can/write/to/cars.csv.gz" GZIP;

proc export data=sashelp.cars
  outfile=_dataout
  dbms=csv replace;
run;

 

 

 
ChrisNZ
Tourmaline | Level 20

@Kurt_Bremser 

Your code generates a 11-kB file (CSV is 38kB) that when opened with Winzip on Win7 

yields a warning "Cannot open file xxx.zip. It does not appear to be a valid archive"

with error message : Error: invalid central directory entry (No. 1) encountered.

 

My version of Winzip seems to be old though. Version 17.5 (10652) 64-bit.

It may be the version that came with Win7. 

 

 

Kurt_Bremser
Super User

<spock>Fascinating</spock>. 7-zip can open the file, while Windows Explorer shows it empty. UNIX unzip (AIX) comes back with

Archive:  cars.csv.zip
mapname:  conversion of  failed

unzip -l shows

Archive:  cars.csv.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
    37713  05-14-2018 09:34   
---------                     -------
    37713                     1 file

so there seems to be an unnamed entry that confuses some zip programs.

 

This research led me to reread the documentation of filename zip (following Maxim 1), and I came up with this solution:

filename _dataout zip "$HOME/sascommunity/cars.csv.zip" member="cars.csv";

proc export data=sashelp.cars
  outfile=_dataout
  dbms=csv replace;
run;

The unzip -l now shows

  Length      Date    Time    Name
---------  ---------- -----   ----
    37713  05-14-2018 09:50   cars.csv
---------                     -------
    37713                     1 file

and UNIX unzip works. Similarly, Windows Explorer now shows the file, and I guess WinZip will also work.

 

BTW filename zip with the gzip option, as suggested by @ChrisHemedinger, works perfectly without any additional options.

 

PS all my tests run on SAS 9.4M5 and AIX, Windows tests on Windows 7.

ChrisNZ
Tourmaline | Level 20

@Kurt_Bremser Colour me impressed. Well done! I wonder whether this behaviour is expected.

ChrisNZ
Tourmaline | Level 20

@Kurt_Bremser Late enough that I was home but not so late that I was asleep. 🙂

 

Otherwise: After doing some (belated) reading, it seems a member name is required. Either in the form you mentioned or like this:

 

 

filename FOO zip 'U:\directory1\testzip.zip';
data _null_;
  file FOO(shoes);

 

There we go. My learning for the day. 🙂

 

@ChrisHemedinger even details how to send a SAS data set in a ZIP file here though the new fcopy() function makes this easier.

filename IN      "&path\test.sas7bdat" recfm=n;
filename OUT zip "&path\test.zip" member='test.sas7bdat' ;
%let rc=%sysfunc(fcopy(IN,OUT)); %put &=rc;

 

 

ChrisHemedinger
Community Manager

@ChrisNZ and @Kurt_Bremser - expected or not, I think it's good practice (maybe essential) to explicitly name the "member" files for the ZIP method, since there can be multiple files in the archive.  For GZIP, that's not necessary (or supported) since it's just a single compressed file, not an archive of several files.

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 16 replies
  • 18370 views
  • 10 likes
  • 4 in conversation