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