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> SAS Macros </h4>
16  @li mp_dirlist.sas
17 
18  @param [in] in= unquoted filepath, dataset of files or directory to zip
19  @param [in] type= (FILE) Valid values:
20  @li FILE - /full/path/and/filename.extension to a particular file
21  @li DATASET - a dataset containing a list of files to zip (see `incol`)
22  @li DIRECTORY - a directory to zip
23  @param [out] outname= (FILE) Output file to create, _without_ .zip extension
24  @param [out] outpath= (%sysfunc(pathname(WORK))) Parent folder for zip file
25  @param [in] incol= () If DATASET input, say which column contains the filepath
26 
27  <h4> Related Macros </h4>
28  @li mp_unzip.sas
29  @li mp_zip.test.sas
30 
31  @version 9.2
32  @author Allan Bowe
33  @source https://github.com/sasjs/core
34 
35 **/
36 
37 %macro mp_zip(
38  in=
39  ,type=FILE
40  ,outname=FILE
41  ,outpath=%sysfunc(pathname(WORK))
42  ,incol=
43  ,debug=NO
44 )/*/STORE SOURCE*/;
45 
46 %let type=%upcase(&type);
47 %local ds;
48 
49 ods package open nopf;
50 
51 %if &type=FILE %then %do;
52  ods package add file="&in" mimetype="application/x-compress";
53 %end;
54 %else %if &type=DIRECTORY %then %do;
55  %mp_dirlist(path=&in,outds=_data_)
56  %let ds=&syslast;
57  data _null_;
58  set &ds;
59  length __command $4000;
60  if file_or_folder='file';
61  __command=cats('ods package add file="',filepath
62  ,'" mimetype="application/x-compress";');
63  call execute(__command);
64  run;
65  /* tidy up */
66  %if &debug=NO %then %do;
67  proc sql; drop table &ds;quit;
68  %end;
69 %end;
70 %else %if &type=DATASET %then %do;
71  data _null_;
72  set &in;
73  length __command $4000;
74  __command=cats('ods package add file="',&incol
75  ,'" mimetype="application/x-compress";');
76  call execute(__command);
77  run;
78 %end;
79 
80 
81 ods package publish archive properties
82  (archive_name="&outname..zip" archive_path="&outpath");
83 ods package close;
84 
85 %mend mp_zip;