Loading...
Searching...
No Matches
restore.sas
Go to the documentation of this file.
1/**
2 @file restore.sas
3 @brief Restores a data version
4 @details Only applies if the history is stored in the audit table
5
6 <h4> SAS Macros </h4>
7 @li dc_assignlib.sas
8 @li mcf_getfmttype.sas
9 @li mf_nobs.sas
10 @li mp_abort.sas
11 @li mp_applyformats.sas
12 @li mp_ds2csv.sas
13 @li mp_getcols.sas
14 @li mp_stripdiffs.sas
15 @li mpeinit.sas
16 @li mpe_checkrestore.sas
17 @li mpe_loader.sas
18
19 <h4> Service Inputs </h4>
20 <h5> restore_in </h5>
21
22 |LOAD_REF:$32|
23 |---|
24 |DCXXXXXX|
25
26 @version 9.2
27 @author 4GL Apps Ltd
28 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
29 and may not be re-distributed or re-sold without the express permission of
30 4GL Apps Ltd.
31
32**/
33
34%mpeinit()
35
36%let loadref=;
37data _null_;
38 set work.restore_in;
39 call symputx('loadref',load_ref);
40run;
41
42/**
43 * Check if user has basic access permission to RESTORE the table
44 */
45%put checking access;
46%global allow_restore reason;
47%mpe_checkrestore(&loadref,outresult=ALLOW_RESTORE,outreason=REASON)
48
49%mp_abort(iftrue= (&ALLOW_RESTORE ne YES)
50 ,mac=&_program..sas
51 ,msg=%str(Cannot restore because: &reason)
52)
53
54/* grab the base DS */
55proc sql noprint;
56select cats(base_lib,'.',base_ds) into: tgtds
57 from &mpelib..mpe_submit
58 where TABLE_ID="&loadref";
59
60/* find the audit table */
61select coalescec(audit_libds,"&mpelib..MPE_AUDIT"), loadtype, var_txto
62 into: difftable, :loadtype, :txto
63 from &mpelib..MPE_TABLES
64 where libref="%scan(&tgtds,1,.)"
65 & dsn="%scan(&tgtds,2,.)"
66 & &dc_dttmtfmt<tx_to;
67%mp_abort(iftrue= ("&difftable"="0")
68 ,mac=&_program..sas
69 ,msg=%str(No audit table configured for &tgtds)
70)
71
72/* assign the library */
73%dc_assignlib(READ,%scan(&tgtds,1,.))
74
75/* if the target is an SCD2 table, create a view to get current snapshot */
76%let fltr=%sysfunc(ifc(&loadtype=TXTEMPORAL,&dc_dttmtfmt<&txto,1=1));
77
78/* extract the differences to be applied */
79%mp_stripdiffs(&tgtds,&loadref,&difftable
80 ,outds=work.mp_stripdiffs
81 ,filtervar=fltr
82 ,mdebug=&sasjs_mdebug
83)
84
85/* abort if any issues */
86%mp_abort(iftrue= (&syscc>0)
87 ,mac=&_program..sas
88 ,msg=%str(syscc=&syscc after stripdiffs)
89)
90%mp_abort(iftrue= (%mf_nobs(work.mp_stripdiffs)=0)
91 ,mac=&_program..sas
92 ,msg=%str(THERE ARE NO DIFFERENCES TO APPLY)
93)
94
95/* create a new load ref */
96%let mperef=DC%left(%sysfunc(datetime(),B8601DT19.3))_%substr(
97 %sysfunc(ranuni(0)),3,6)_%substr(%str(&sysjobid ),1,4);
98
99/* Create package folder */
100%let dir=&mpelocapprovals/&mperef;
101%mf_mkdir(&dir)
102options notes mprint;
103libname approve "&dir";
104
105/* take copy of macvars */
106data _null_;
107 file "&dir/macvars.sas";
108 set sashelp.vmacro;
109 where scope='GLOBAL';
110 put '%let ' name '=' value ';';
111run;
112
113/* copy the diffs dataset */
114data approve.jsdset;
115 length _____DELETE__THIS__RECORD_____ $3;
116 if 0 then call missing(_____DELETE__THIS__RECORD_____);
117 set work.mp_stripdiffs;
118 format _all_;
119run;
120
121/* find all of the date / datetime / time vars */
122%mcf_getfmttype(wrap=YES)
123%mp_getcols(&tgtds,outds=work.cols)
124
125data work.applydtfmts;
126 set work.cols;
127 lib="APPROVE";
128 ds="JSDSET";
129 var=name;
130 fmt=coalescec(format,'0');
131 fmttype=mcf_getfmttype(fmt);
132 if fmttype in ('DATE','DATETIME','TIME');
133 keep lib ds var fmt;
134run;
135%mp_applyformats(work.applydtfmts)
136
137
138/* export to csv */
139%mp_ds2csv(approve.jsdset
140 ,dlm=COMMA
141 ,outfile="&dir/%trim(&tgtds).csv"
142 ,outencoding="UTF-8"
143 ,headerformat=NAME
144 ,termstr=CRLF
145)
146
147%mp_abort(iftrue= (&syscc ne 0)
148 ,mac=&_program
149 ,msg=%str(syscc=&syscc when writing the CSV)
150)
151
152%mpe_loader(mperef=&mperef
153 ,submitted_reason_txt=Restoring &loadref
154 ,dc_dttmtfmt=&dc_dttmtfmt
155)
156%mp_abort(mode=INCLUDE)
157
158%mp_abort(
159 iftrue=(%sysfunc(fileexist(%sysfunc(pathname(work))/mf_abort.error))=1)
160 ,mac=&_program..sas
161 ,msg=%str(mf_abort.error=1)
162)
163
164%mp_abort(iftrue= (&syscc ne 0)
165 ,mac=&_program..sas
166 ,msg=%str(syscc=&syscc)
167)
168
169/* send relevant SUCCESS values */
170data work.restore_out;
171 loadref="&mperef";
172run;
173
174
175%webout(OPEN)
176%webout(OBJ,restore_out)
177%webout(CLOSE)
178