Skip to content

Instantly share code, notes, and snippets.

@cjdinger
Created March 4, 2016 20:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjdinger/14f038a79d63fbfd5b69 to your computer and use it in GitHub Desktop.
Save cjdinger/14f038a79d63fbfd5b69 to your computer and use it in GitHub Desktop.
Example of using FILENAME ZIP to create a ZIP file, add to it, and report on the contents
/* Your zip file folder location */
%let projectDir = c:\temp;
/* macro to assign a fileref to a SAS data set in a Base library */
%macro assignFilerefToDataset(_dataset_name);
%local outDsName;
ods output EngineHost=File;
proc contents data=&_dataset_name.;
run;
proc sql noprint;
select cValue1 into: outDsName
from work.file where Label1="Filename";
quit;
filename data_fn "&outDsName.";
%mend;
/* Assign the ZIP fileref */
filename projzip "&projectDir./project.zip";
/* Start with a clean slate - delete ZIP if it exists */
data _null_;
rc=fdelete('projzip');
run;
%assignFilerefToDataset(sashelp.class);
/* Use FILENAME ZIP to add a new member -- CLASS */
/* Put it in the data subfolder */
filename addfile zip "&projectDir./project.zip"
member='data/class.sas7bdat';
/* byte-by-byte copy */
/* "copies" the new file into the ZIP archive */
data _null_;
infile data_fn recfm=n;
file addfile recfm=n;
input byte $char1. @;
put byte $char1. @;
run;
filename addfile clear;
%assignFilerefToDataset(sashelp.cars);
/* Use FILENAME ZIP to add a new member -- CARS */
/* Put it in the data subfolder */
filename addfile zip "&projectDir./project.zip"
member='data/cars.sas7bdat';
/* byte-by-byte copy */
/* "copies" the new file into the ZIP archive */
data _null_;
infile data_fn recfm=n;
file addfile recfm=n;
input byte $char1. @;
put byte $char1. @;
run;
filename addfile clear;
/* OPTIONAL for reporting */
/* Report on the contents of the ZIP file */
/* Assign a fileref wth the ZIP method */
filename inzip zip "&projectDir./project.zip";
/* Read the "members" (files) from 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;
/* create a report of the ZIP contents */
title "Files in the ZIP file";
proc print data=contents noobs N;
run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment