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