Macros for SAS Application Developers
https://github.com/sasjs/core
mp_getpk.test.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Testing mp_getpk.sas macro
4 
5  <h4> SAS Macros </h4>
6  @li mf_nobs.sas
7  @li mp_getpk.sas
8  @li mp_assert.sas
9  @li mp_assertdsobs.sas
10 
11  <h4> Related Macros </h4>
12  @li mp_getpk.sas
13 
14 **/
15 
16 /* ensure PK arrives in corrrect order */
17 proc sql;
18 create table work.example1(
19  TX_FROM float format=datetime19.,
20  DD_TYPE char(16),
21  DD_SOURCE char(2048),
22  DD_SHORTDESC char(256),
23  constraint pk primary key(tx_from, dd_type,dd_source),
24  constraint unq unique(tx_from, dd_type),
25  constraint nnn not null(DD_SHORTDESC)
26 );
27 %mp_getpk(work,ds=example1,outds=test1)
28 
29 data _null_;
30  set work.test1;
31  call symputx('test1',pk_fields);
32 run;
33 
34 %mp_assert(
35  iftrue=("&test1"="TX_FROM DD_TYPE DD_SOURCE"),
36  desc=mp_getpk gets regular PK values in correct order,
37  outds=work.test_results
38 )
39 
40 /* unique key with NOT NULL captured */
41 proc sql;
42 create table work.example2(
43  TX_FROM float format=datetime19.,
44  DD_TYPE char(16),
45  DD_SOURCE char(2048),
46  DD_SHORTDESC char(256),
47  constraint unq1 unique(tx_from, dd_type),
48  constraint unq2 unique(tx_from, dd_type, dd_source),
49  constraint nnn not null(tx_from),
50  constraint nnnn not null(dd_type)
51 );
52 %mp_getpk(work,ds=example2,outds=test2)
53 
54 data _null_;
55  set work.test2;
56  call symputx('test2',pk_fields);
57 run;
58 
59 %mp_assert(
60  iftrue=("&test2"="TX_FROM DD_TYPE"),
61  desc=mp_getpk gets unique constraint with NOT NULL in correct order
62 )
63 
64 /* unique key without NOT NULL NOT captured */
65 proc sql;
66 create table work.example3(
67  TX_FROM float format=datetime19.,
68  DD_TYPE char(16),
69  DD_SOURCE char(2048),
70  DD_SHORTDESC char(256),
71  constraint unq1 unique(tx_from, dd_type),
72  constraint unq2 unique(tx_from, dd_type, dd_source),
73  constraint nnn not null(tx_from)
74 );
75 %mp_getpk(work,ds=example3,outds=test3)
76 
77 data _null_;
78  set work.test3;
79  call symputx('test3',pk_fields);
80 run;
81 
82 %mp_assert(
83  iftrue=("&test3 "=" "),
84  desc=mp_getpk does not capture unique constraint without NOT NULL,
85  outds=work.test_results
86 )
87 
88 /* constraint capture at library level is functional - uses first 2 tests */
89 %mp_getpk(work,outds=test4)
90 
91 %mp_assertdsobs(work.test4,test=ATLEAST 2)
92 
93 /* unique & not null INDEX captured */
94 proc sql;
95 create table work.example5(
96  TX_FROM float format=datetime19.,
97  DD_TYPE char(16),
98  DD_SOURCE char(2048),
99  DD_SHORTDESC char(256)
100 );
101 proc datasets lib=work noprint;
102  modify example5;
103  index create tx_from /nomiss unique;
104 quit;
105 %mp_getpk(work,ds=example5,outds=test5)
106 data _null_;
107  set work.test5;
108  call symputx('test5',pk_fields);
109 run;
110 %mp_assert(
111  iftrue=("&test5"="TX_FROM"),
112  desc=mp_getpk captures single column not null unique index,
113  outds=work.test_results
114 )
115 
116 /* unique & not null COMPOSITE INDEX captured */
117 proc sql;
118 create table work.example6(
119  TX_FROM float format=datetime19.,
120  DD_TYPE char(16),
121  DD_SOURCE char(2048),
122  DD_SHORTDESC char(256)
123 );
124 proc datasets lib=work noprint;
125  modify example6;
126  index create pk_6=(tx_from dd_type) /nomiss unique;
127 quit;
128 %mp_getpk(work,ds=example6,outds=test6)
129 data _null_;
130  set work.test6;
131  call symputx('test6',pk_fields);
132 run;
133 %mp_assert(
134  iftrue=("&test6"="TX_FROM DD_TYPE"),
135  desc=mp_getpk captures multiple column not null unique index,
136  outds=work.test_results
137 )