Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
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 */
17proc sql;
18create 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
29data _null_;
30 set work.test1;
31 call symputx('test1',pk_fields);
32run;
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 */
41proc sql;
42create 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
54data _null_;
55 set work.test2;
56 call symputx('test2',pk_fields);
57run;
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 */
65proc sql;
66create 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
77data _null_;
78 set work.test3;
79 call symputx('test3',pk_fields);
80run;
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 */
94proc sql;
95create 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);
101proc datasets lib=work noprint;
102 modify example5;
103 index create tx_from /nomiss unique;
104quit;
105%mp_getpk(work,ds=example5,outds=test5)
106data _null_;
107 set work.test5;
108 call symputx('test5',pk_fields);
109run;
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 */
117proc sql;
118create 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);
124proc datasets lib=work noprint;
125 modify example6;
126 index create pk_6=(tx_from dd_type) /nomiss unique;
127quit;
128%mp_getpk(work,ds=example6,outds=test6)
129data _null_;
130 set work.test6;
131 call symputx('test6',pk_fields);
132run;
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)