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