Macros for SAS Application Developers
https://github.com/sasjs/core
mm_spkexport.sas
Go to the documentation of this file.
1 /**
2  @file mm_spkexport.sas
3  @brief Creates an batch spk export command
4  @details Creates a script that will export everything in a metadata folder to
5  a specified location.
6  If you have XCMD enabled, then you can use mmx_spkexport (which performs
7  the actual export)
8 
9  Note - the batch tools require a username and password. For security,
10  these are expected to have been provided in a protected directory.
11 
12  Usage:
13 
14  %* import the macros (or make them available some other way);
15  filename mc url
16  "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
17  %inc mc;
18 
19  %* create sample text file as input to the macro;
20  filename tmp temp;
21  data _null_;
22  file tmp;
23  put '%let mmxuser="sasdemo";';
24  put '%let mmxpass="Mars321";';
25  run;
26 
27  filename myref "%sysfunc(pathname(work))/mmxexport.sh"
28  permission='A::u::rwx,A::g::r-x,A::o::---';
29  %mm_spkexport(metaloc=%str(/my/meta/loc)
30  ,outref=myref
31  ,secureref=tmp
32  ,cmdoutloc=%str(/tmp)
33  )
34 
35  Alternatively, call without inputs to create a function style output
36 
37  filename myref "/tmp/mmscript.sh"
38  permission='A::u::rwx,A::g::r-x,A::o::---';
39  %mm_spkexport(metaloc=%str(/my/meta/loc)
40  outref=myref
41  ,cmdoutloc=%str(/tmp)
42  ,cmdoutname=mmx
43  )
44 
45  You can then navigate and execute as follows:
46 
47  cd /tmp
48  ./mmscript.sh "myuser" "mypass"
49 
50 
51  <h4> SAS Macros </h4>
52  @li mf_getuniquefileref.sas
53  @li mf_getuniquename.sas
54  @li mf_isblank.sas
55  @li mf_loc.sas
56  @li mm_tree.sas
57  @li mp_abort.sas
58 
59 
60  @param [in] metaloc= the metadata folder to export
61  @param [in] secureref= fileref containing the username / password (should
62  point to a file in a secure location). Leave blank to substitute $bash vars.
63  @param [in] excludevars= (0) A space seperated list of macro variable names,
64  each of which contains a value that should be used to filter the output
65  objects.
66  @param [out] outref= fileref to which to write the command
67  @param [out] cmdoutloc= (%sysfunc(pathname(work))) The directory to which the
68  command will write the SPK
69  @param [out] cmdoutname= (mmxport) The name of the spk / log files to create
70  (will be identical just with .spk or .log extension)
71 
72  @version 9.4
73  @author Allan Bowe
74 
75 **/
76 
77 %macro mm_spkexport(metaloc=
78  ,secureref=
79  ,excludevars=0
80  ,outref=
81  ,cmdoutloc=%sysfunc(pathname(work))
82  ,cmdoutname=mmxport
83 );
84 
85 %if &sysscp=WIN %then %do;
86  %put %str(WARN)ING: the script has been written assuming a unix system;
87  %put %str(WARN)ING- it will run anyway as should be easy to modify;
88 %end;
89 
90 /* set creds */
91 %local mmxuser mmxpath i var;
92 %let mmxuser=$1;
93 %let mmxpass=$2;
94 %if %mf_isblank(&secureref)=0 %then %do;
95  %inc &secureref/nosource;
96 %end;
97 
98 /* setup metadata connection options */
99 %local host port platform_object_path ds;
100 %let host=%sysfunc(getoption(metaserver));
101 %let port=%sysfunc(getoption(metaport));
102 %let platform_object_path=%mf_loc(POF);
103 %let ds=%mf_getuniquename(prefix=spkexportable);
104 
105 %mm_tree(root=%str(&metaloc),types=EXPORTABLE ,outds=&ds)
106 
107 %if %mf_isblank(&outref)=1 %then %let outref=%mf_getuniquefileref();
108 
109 data _null_;
110  set &ds end=last;
111  file &outref lrecl=32767;
112  length str $32767;
113  if _n_=1 then do;
114  put "# Script generated by &sysuserid on %sysfunc(datetime(),datetime19.)";
115  put "cd ""&platform_object_path"" \";
116  put "; ./ExportPackage -host &host -port &port -user &mmxuser \";
117  put " -disableX11 -password &mmxpass \";
118  put " -package ""&cmdoutloc/&cmdoutname..spk"" \";
119  end;
120 /* exclude particular patterns from the exported SPK */
121 %if "&excludevars" ne "0" %then %do;
122  %do i=1 %to %sysfunc(countw(&excludevars));
123  %let var=%scan(&excludevars,&i);
124  if _n_=1 then do;
125  length excludestr&i $1000;
126  retain excludestr&i;
127  excludestr&i=symget("&var");
128  putlog excludestr&i=;
129  putlog path=;
130  end;
131  if index(path,cats(excludestr&i))=0 and index(name,cats(excludestr&i))=0;
132  %end;
133  /* ignore top level folder else all subcontent will be exported regardless */
134  if _n_>1;
135 %end;
136  str=' -objects '!!cats('"',path,'/',name,"(",publictype,')" \');
137  put str;
138  if last then put " -log ""&cmdoutloc/&cmdoutname..log"" 2>&1 ";
139 run;
140 
141 %mp_abort(iftrue= (&syscc ne 0)
142  ,mac=mm_spkexport
143  ,msg=%str(syscc=&syscc)
144 )
145 
146 %mend mm_spkexport;