/*
NOTE: writing zip files with ODS - the output archives do no support being read by the SAS filename zip engine due to missing file signature info
However, the method described here works: https://blogs.sas.com/content/sasdummy/2017/10/10/reading-writing-gzip-files-sas/
I have extended the example to include both GZ and ZIP examples.
*/

%let path = C:\projects;
%let project = customer;
%let projectpath = &path./&project;

options dlcreatedir;
libname _proj "&path";
libname cust "&projectpath";
options nodlcreatedir;

/* get a data set into a library */

data cust.cars_i;
set sashelp.cars;
run;

filename sas_in "%sysfunc(pathname(cust))/cars_i.sas7bdat";
filename sas_outg "%sysfunc(pathname(cust))/cars_o_gz.sas7bdat";
filename sas_outz "%sysfunc(pathname(cust))/cars_o_zp.sas7bdat";
filename gz_arch ZIP "%sysfunc(pathname(cust))/cars.sas7bdat.gz" GZIP;
filename zp_arch ZIP "%sysfunc(pathname(cust))/cars.sas7bdat.zip" ZIP;

/* Compress the data set into a GZ file */
data _null_;
infile sas_in
lrecl=256 recfm=F length=length eof=eof unbuf;
file gz_arch lrecl=256 recfm=N;
input;
put _infile_ $varying256. length;
return;
eof:
stop;
run;

/* Compress the data set into a Zip file */
data _null_;
infile sas_in
lrecl=256 recfm=F length=length eof=eof unbuf;
file zp_arch lrecl=256 recfm=N;
input;
put _infile_ $varying256. length;
return;
eof:
stop;
run;

/* Delete the original uncompressed file */
/* NOTE: changed code here to use a different compress file, for comparison purposes - so no need to delete original */
/*
data _null_;
rc=fdelete('sas_ds');
run;
*/

/* put the file back in the directory, expanded */
data _null_;
infile gz_arch
lrecl=256 recfm=F length=length eof=eof unbuf;
file sas_outg lrecl=256 recfm=N;
input;
put _infile_ $varying256. length;
return;
eof:
stop;
run;

/* put the file back in the directory, expanded */
data _null_;
infile zp_arch
lrecl=256 recfm=F length=length eof=eof unbuf;
file sas_outz lrecl=256 recfm=N;
input;
put _infile_ $varying256. length;
return;
eof:
stop;
run;