Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mp_zip.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Creates a zip file
4  @details For DIRECTORY usage, will ignore subfolders. For DATASET usage,
5  provide a column that contains the full file path to each file to be zipped.
6 
7  %mp_zip(in=myzips,type=directory,outname=myDir)
8  %mp_zip(in=/my/file/path.txt,type=FILE,outname=myFile)
9  %mp_zip(in=SOMEDS,incol=FPATH,type=DATASET,outname=myFile)
10 
11  If you are sending zipped output to the _webout destination as part of an STP
12  be sure that _debug is not set (else the SPWA will send non zipped content
13  as well).
14 
15  <h4> Dependencies </h4>
16  @li mp_dirlist.sas
17 
18  @param in= unquoted filepath, dataset of files or directory to zip
19  @param type= FILE, DATASET, DIRECTORY. (FILE / DATASET not ready yet)
20  @param outname= output file to create, without .zip extension
21  @param outpath= location for output zip file
22  @param incol= if DATASET input, say which column contains the filepath
23 
24  @version 9.2
25  @author Allan Bowe
26  @source https://github.com/sasjs/core
27 
28 **/
29 
30 %macro mp_zip(
31  in=
32  ,type=FILE
33  ,outname=FILE
34  ,outpath=%sysfunc(pathname(WORK))
35  ,incol=
36  ,debug=NO
37 )/*/STORE SOURCE*/;
38 
39 %let type=%upcase(&type);
40 %local ds;
41 
42 ods package open nopf;
43 
44 %if &type=FILE %then %do;
45  ods package add file="&in" mimetype="application/x-compress";
46 %end;
47 %else %if &type=DIRECTORY %then %do;
48  %mp_dirlist(path=&in,outds=_data_)
49  %let ds=&syslast;
50  data _null_;
51  set &ds;
52  length __command $4000;
53  if file_or_folder='file';
54  command=cats('ods package add file="',filepath
55  ,'" mimetype="application/x-compress";');
56  call execute(command);
57  run;
58  /* tidy up */
59  %if &debug=NO %then %do;
60  proc sql; drop table &ds;quit;
61  %end;
62 %end;
63 %else %if &type=DATASET %then %do;
64  data _null_;
65  set &in;
66  length __command $4000;
67  command=cats('ods package add file="',&incol
68  ,'" mimetype="application/x-compress";');
69  call execute(command);
70  run;
71  ods package add file="&in" mimetype="application/x-compress";
72 %end;
73 
74 
75 ods package publish archive properties
76  (archive_name="&outname..zip" archive_path="&outpath");
77 ods package close;
78 
79 %mend;