Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_validatecol.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Used to validate values in a data step
4 @details Useful when sanitising inputs, to ensure that they arrive with a
5 certain pattern.
6 Usage:
7
8 data test;
9 infile datalines4 dsd;
10 input;
11 libds=_infile_;
12 %mp_validatecol(libds,LIBDS,is_libds)
13 datalines4;
14 some.libname
15 !lib.blah
16 %abort
17 definite.ok
18 not.ok!
19 nineletrs._
20 ;;;;
21 run;
22
23 For more examples, see mp_validatecol.test.sas
24
25 Tip - when contributing, use https://regex101.com to test the regex validity!
26
27 @param [in] incol The column to be validated
28 @param [in] rule The rule to apply. Current rules:
29 @li ISINT - checks if the variable is an integer
30 @li ISLIB - checks if the value is a valid libref (NOT whether it exists)
31 @li ISNAME - checks if the value is a valid SAS NAME (V7 rules - up to 32
32 chars, starting with a letter or underscore, followed by letters,
33 numbers or underscores)
34 @li ISNUM - checks if the variable is numeric
35 @li LIBDS - matches LIBREF.DATASET format
36 @li FORMAT - checks if the provided format is syntactically valid
37 @param [out] outcol The variable to create, with the results of the match
38
39 <h4> SAS Macros </h4>
40 @li mf_getuniquename.sas
41
42 <h4> Related Macros </h4>
43 @li mp_validatecol.test.sas
44
45 @version 9.3
46**/
47
48%macro mp_validatecol(incol,rule,outcol);
49
50/* tempcol is given a unique name with every invocation */
51%local tempcol;
52%let tempcol=%mf_getuniquename();
53
54%if &rule=ISINT %then %do;
55 &outcol=0;
56 if not missing(&incol) then do;
57 &tempcol=input(&incol,?? best32.);
58 if not missing(&tempcol) then if mod(&tempcol,1)=0 then &outcol=1;
59 end;
60 drop &tempcol;
61%end;
62%else %if &rule=ISNUM %then %do;
63 /*
64 credit SOREN LASSEN
65 https://sasmacro.blogspot.com/2009/06/welcome-isnum-macro.html
66 */
67 &tempcol=input(&incol,?? best32.);
68 if missing(&tempcol) then &outcol=0;
69 else &outcol=1;
70 drop &tempcol;
71%end;
72%else %if &rule=ISLIB %then %do;
73 if _n_=1 then do;
74 retain &tempcol;
75 &tempcol=prxparse('/^[_a-z]\w{0,7}$/i');
76 if missing(&tempcol) then do;
77 putlog 'ERR' +(-1) "OR: Invalid expression for ISLIB";
78 stop;
79 end;
80 drop &tempcol;
81 end;
82 if prxmatch(&tempcol, trim(&incol)) then &outcol=1;
83 else &outcol=0;
84%end;
85%else %if &rule=ISNAME %then %do;
86 /* match a valid SAS name (V7 rules) */
87 if _n_=1 then do;
88 retain &tempcol;
89 &tempcol=prxparse('/^[_a-z]\w{0,31}$/i');
90 if missing(&tempcol) then do;
91 putlog 'ERR' +(-1) "OR: Invalid expression for ISNAME";
92 stop;
93 end;
94 drop &tempcol;
95 end;
96 if prxmatch(&tempcol, trim(&incol)) then &outcol=1;
97 else &outcol=0;
98%end;
99%else %if &rule=LIBDS %then %do;
100 /* match libref.dataset */
101 if _n_=1 then do;
102 retain &tempcol;
103 &tempcol=prxparse('/^[_a-z]\w{0,7}\.[_a-z]\w{0,31}$/i');
104 if missing(&tempcol) then do;
105 putlog 'ERR' +(-1) "OR: Invalid expression for LIBDS";
106 stop;
107 end;
108 drop &tempcol;
109 end;
110 if prxmatch(&tempcol, trim(&incol)) then &outcol=1;
111 else &outcol=0;
112%end;
113%else %if &rule=FORMAT %then %do;
114 /* match valid SAS format*/
115 if _n_=1 then do;
116 retain &tempcol;
117 &tempcol=prxparse('/^(?:[_a-z\$]\w{0,31}|[0-9]+)\.[0-9]*$/i');
118 if missing(&tempcol) then do;
119 putlog 'ERR' +(-1) "OR: Invalid expression for FORMAT";
120 stop;
121 end;
122 drop &tempcol;
123 end;
124 if prxmatch(&tempcol, trim(&incol)) then &outcol=1;
125 else &outcol=0;
126%end;
127
128%mend mp_validatecol;