kstats9pt3
kstats9pt3

Reputation: 873

SAS unzip a single sas7bdat file

I've scoured the internet and cannot find exactly what I need. At the end of a SAS program, I zipped the final dataset (20GB) using the ods package method. My dataset now sits in a zipped folder as wanted. Now, I want to unzip and read in that .sas7bdat file and I'm not quite sure about the method.

Below is an example that creates a permanent SAS dataset and zips it up. I am able to look within that zip file and "see" the dataset I need, but I have no idea how to unzip it and read it in:

** assign your path **;
libname output "H:\SAS Example Code";
%let path = H:\SAS Example Code;

** test data **;
data output.class; set sashelp.class; run;

** zip the permanent SAS dataset **;
ods package(zip) open nopf;
ods package(zip) 
    add file="&path./class.sas7bdat";
ods package(zip) 
    publish archive        
    properties(
        archive_name= "sashelp.class.zip"                  
        archive_path="&path."
        );
ods package(zip) close;

/* BELOW THIS LINE NEEDS WORK -- HOW DO I READ IN THIS DATASET? */

** assign filename and point to zip file **;
filename inzip zip "&path./sashelp.class.zip";

** view the .sas7bdat file within the zip file **;
data contents(keep=memname);
    length memname $200;
    fid=dopen("inzip");
        if fid=0 then stop;
    memcount=dnum(fid);
        do i=1 to memcount;
            memname=dread(fid,i);
        output;
        end;
    rc=dclose(fid);
run;

Upvotes: 2

Views: 3557

Answers (1)

Tom
Tom

Reputation: 51566

You need to copy the file out of the ZIP file and write to a physical file. You can use the FCOPY() function to do this. To force it to copy the file as BINARY you need to set RECFM=F and a LRECL value, 512 is a natural record size to use, when defining the filerefs.

So here is complete program to create a dataset, zip it, unzip it to a new name and compare the two versions.

* Get path of current WORK directory ;
%let work=%sysfunc(pathname(work));

* Make a new work dataset ;
data work.class; set sashelp.class; run;

** Make the ZIP file **;
ods package(zip) open nopf;
ods package(zip) add file="&work./class.sas7bdat";
ods package(zip) publish archive
    properties(
        archive_name= "sashelp.class.zip"
        archive_path="&work."
        )
;
ods package(zip) close;

* Create filerefs pointing to the source and target ;
filename zipin zip "&work/sashelp.class.zip" member="class.sas7bdat"
  recfm=f lrecl=512
;
filename ssdout "&work/class2.sas7bdat" recfm=f lrecl=512;

* Use FCOPY() function to copy the file. ;
%let rc=%sysfunc(fcopy(zipin,ssdout));

* Check if it worked ;
proc compare data=class compare=class2; run;

If you cannot get FCOPY() to work try using the utility that Chris Hemedinger posted on his SAS Dummy blog. http://blogs.sas.com/content/sasdummy/2013/09/17/copy-file-macro/

Upvotes: 3

Related Questions