Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_createconstraints.sas
Go to the documentation of this file.
1/**
2 @file mp_createconstraints.sas
3 @brief Creates constraints
4 @details Takes the output from mp_getconstraints.sas as input
5
6 proc sql;
7 create table work.example(
8 TX_FROM float format=datetime19.,
9 DD_TYPE char(16),
10 DD_SOURCE char(2048),
11 DD_SHORTDESC char(256),
12 constraint pk primary key(tx_from, dd_type,dd_source),
13 constraint unq unique(tx_from, dd_type),
14 constraint nnn not null(DD_SHORTDESC)
15 );
16
17 %mp_getconstraints(lib=work,ds=example,outds=work.constraints)
18 %mp_deleteconstraints(inds=work.constraints,outds=dropped,execute=YES)
19 %mp_createconstraints(inds=work.constraints,outds=created,execute=YES)
20
21 @param [in] inds= (work.mp_getconstraints) The input table containing the
22 constraint info
23 @param [out] outds= (work.mp_createconstraints) A table containing the create
24 statements (create_statement column)
25 @param [in] execute= (NO) To actually create, use YES.
26
27 <h4> Related Files </h4>
28 @li mp_getconstraints.sas
29
30 @version 9.2
31 @author Allan Bowe
32
33**/
34
35%macro mp_createconstraints(inds=mp_getconstraints
36 ,outds=work.mp_createconstraints
37 ,execute=NO
38)/*/STORE SOURCE*/;
39
40proc sort data=&inds out=&outds;
41 by libref table_name constraint_name;
42run;
43
44data &outds;
45 set &outds;
46 by libref table_name constraint_name;
47 length create_statement $500;
48 if _n_=1 and "&execute"="YES" then call execute('proc sql;');
49 if first.constraint_name then do;
50 if constraint_type='PRIMARY' then type='PRIMARY KEY';
51 else type=constraint_type;
52 create_statement=catx(" ","alter table",libref,".",table_name
53 ,"add constraint",constraint_name,type,"(");
54 if last.constraint_name then
55 create_statement=cats(create_statement,column_name,");");
56 else create_statement=cats(create_statement,column_name,",");
57 if "&execute"="YES" then call execute(create_statement);
58 end;
59 else if last.constraint_name then do;
60 create_statement=cats(column_name,");");
61 if "&execute"="YES" then call execute(create_statement);
62 end;
63 else do;
64 create_statement=cats(column_name,",");
65 if "&execute"="YES" then call execute(create_statement);
66 end;
67 output;
68run;
69
70%mend mp_createconstraints;