Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_retainedkey.test.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Testing mp_retainedkey macro
4
5 <h4> SAS Macros </h4>
6 @li mf_nobs.sas
7 @li mp_assert.sas
8 @li mp_assertcolvals.sas
9 @li mp_assertscope.sas
10 @li mp_retainedkey.sas
11
12**/
13
14/**
15 * Setup base tables
16 */
17proc sql;
18create table work.maxkeytable(
19 keytable varchar(41) label='Base table in libref.dataset format',
20 keycolumn char(32) format=$32.
21 label='The Retained key field containing the key values.',
22 max_key num label=
23 'Integer representing current max RK or SK value in the KEYTABLE',
24 processed_dttm num format=E8601DT26.6
25 label='Datetime this value was last updated',
26 constraint pk_mpe_maxkeyvalues
27 primary key(keytable));
28
29create table work.locktable(
30 lock_lib char(8),
31 lock_ds char(32),
32 lock_status_cd char(10) not null,
33 lock_user_nm char(100) not null ,
34 lock_ref char(200),
35 lock_pid char(10),
36 lock_start_dttm num format=E8601DT26.6,
37 lock_end_dttm num format=E8601DT26.6,
38 constraint pk_mp_lockanytable primary key(lock_lib,lock_ds));
39
40data work.targetds;
41 rk_col=_n_;
42 set sashelp.class;
43run;
44
45data work.appendtable;
46 set sashelp.class;
47 if mod(_n_,2)=0 then name=cats('New',_n_);
48 if _n_<7;
49run;
50
51libname x (work);
52
53/** Test 1 - base case **/
54%mp_retainedkey(
55 base_lib=X
56 ,base_dsn=targetds
57 ,append_lib=X
58 ,append_dsn=APPENDTABLE
59 ,retained_key=rk_col
60 ,business_key= name
61 ,check_uniqueness=NO
62 ,maxkeytable=0
63 ,locktable=0
64 ,outds=work.APPEND
65 ,filter_str=
66)
67%mp_assert(
68 iftrue=(&syscc=0),
69 desc=Checking errors in test 1,
70 outds=work.test_results
71)
72
73data work.check;
74 do val=1,3,5,20,21,22;
75 output;
76 end;
77run;
78%mp_assertcolvals(work.append.rk_col,
79 checkvals=work.check.val,
80 desc=All values have a match,
81 test=ALLVALS
82)
83
84/** Test 2 - all new records, with metadata logging and unique check **/
85data work.targetds2;
86 rk_col=_n_;
87 set sashelp.class;
88run;
89
90data work.appendtable2;
91 set sashelp.class;
92 do x=1 to 21;
93 name=cats('New',x);
94 output;
95 end;
96 stop;
97run;
98
99%mp_retainedkey(base_dsn=targetds2
100 ,append_dsn=APPENDTABLE2
101 ,retained_key=rk_col
102 ,business_key= name
103 ,check_uniqueness=YES
104 ,maxkeytable=x.maxkeytable
105 ,locktable=work.locktable
106 ,outds=WORK.APPEND2
107 ,filter_str=
108)
109%mp_assert(
110 iftrue=(&syscc=0),
111 desc=Checking errors in test 2,
112 outds=work.test_results
113)
114%mp_assert(
115 iftrue=(%mf_nobs(work.append2)=21),
116 desc=Checking append records created,
117 outds=work.test_results
118)
119
120/** Test 3 - large numeric RK values (avoid exponential notation issue) **/
121data work.targetds3;
122 set sashelp.class;
123 rk_col=_n_+209100000000;
124run;
125
126data work.appendtable3;
127 set sashelp.class;
128 if mod(_n_,2)=0 then name=cats('New',_n_);
129 if _n_<7;
130run;
131
132%mp_assertscope(SNAPSHOT)
133
134%mp_retainedkey(
135 base_lib=X
136 ,base_dsn=targetds3
137 ,append_lib=X
138 ,append_dsn=APPENDTABLE3
139 ,retained_key=rk_col
140 ,business_key= name
141 ,check_uniqueness=NO
142 ,maxkeytable=0
143 ,locktable=0
144 ,outds=work.APPEND3
145 ,filter_str=
146)
147
148%mp_assertscope(COMPARE,
149 desc=Checking macro scope leakage in mp_retainedkey (test 3)
150)
151
152%mp_assert(
153 iftrue=(&syscc=0),
154 desc=Checking errors in test 3 (large numeric RK),
155 outds=work.test_results
156)
157
158data work.check3;
159 do val=209100000001,209100000003,209100000005,
160 209100000020,209100000021,209100000022;
161 output;
162 end;
163run;
164%mp_assertcolvals(work.append3.rk_col,
165 checkvals=work.check3.val,
166 desc=All large numeric values have a match,
167 test=ALLVALS
168)