Macros for SAS Application Developers
https://github.com/sasjs/core
mp_unzip.sas
Go to the documentation of this file.
1/**
2 @file mp_unzip.sas
3 @brief Unzips a zip file
4 @details Opens the zip file and copies all the contents to another directory.
5 It is not possible to retain permissions / timestamps, also the BOF marker
6 is lost so it cannot extract binary files.
7
8 Usage:
9
10 filename mc url
11 "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
12 %inc mc;
13
14 %mp_unzip(ziploc="/some/file.zip",outdir=/some/folder)
15
16 More info: https://blogs.sas.com/content/sasdummy/2015/05/11/using-filename-zip-to-unzip-and-read-data-files-in-sas/
17
18 @param ziploc= Fileref or quoted full path to zip file ("/path/to/file.zip")
19 @param outdir= (%sysfunc(pathname(work))) Directory in which to write the
20 outputs (created if non existant)
21
22 <h4> SAS Macros </h4>
23 @li mf_mkdir.sas
24 @li mf_getuniquefileref.sas
25 @li mp_binarycopy.sas
26
27 @version 9.4
28 @author Allan Bowe
29 @source https://github.com/sasjs/core
30
31**/
32
33%macro mp_unzip(
34 ziploc=
35 ,outdir=%sysfunc(pathname(work))
36)/*/STORE SOURCE*/;
37
38%local f1 f2 ;
39%let f1=%mf_getuniquefileref();
40%let f2=%mf_getuniquefileref();
41
42/* Macro variable &datazip would be read from the file */
43filename &f1 ZIP &ziploc;
44
45/* create target folder */
46%mf_mkdir(&outdir)
47
48/* Read the "members" (files) from the ZIP file */
49data _data_(keep=memname isFolder);
50 length memname $200 isFolder 8;
51 fid=dopen("&f1");
52 if fid=0 then stop;
53 memcount=dnum(fid);
54 do i=1 to memcount;
55 memname=dread(fid,i);
56 /* check for trailing / in folder name */
57 isFolder = (first(reverse(trim(memname)))='/');
58 output;
59 end;
60 rc=dclose(fid);
61run;
62
63filename &f2 temp;
64
65/* loop through each entry and either create the subfolder or extract member */
66data _null_;
67 set &syslast;
68 file &f2;
69 if isFolder then call execute('%mf_mkdir(&outdir/'!!memname!!')');
70 else do;
71 qname=quote(cats("&outdir/",memname));
72 bname=cats('(',memname,')');
73 put '/* hat tip: "data _null_" on SAS-L */';
74 put 'data _null_;';
75 put ' infile &f1 ' bname ' lrecl=256 recfm=F length=length eof=eof unbuf;';
76 put ' file ' qname ' lrecl=256 recfm=N;';
77 put ' input;';
78 put ' put _infile_ $varying256. length;';
79 put ' return;';
80 put 'eof:';
81 put ' stop;';
82 put 'run;';
83 end;
84run;
85
86%inc &f2/source2;
87
88filename &f2 clear;
89
90%mend mp_unzip;