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. ISLIB, ISNAME, LIBDS and FORMAT
29 match the entire value after trimming trailing SAS blanks; a trailing
30 newline is rejected. Current rules:
31 @li ISINT - checks if the variable is an integer
32 @li ISLIB - checks if the value is a valid libref (NOT whether it exists)
33 @li ISNAME - checks if the value is a valid SAS NAME (V7 rules - up to 32
34 chars, starting with a letter or underscore, followed by letters,
35 numbers or underscores)
36 @li ISNUM - checks if the variable is numeric
37 @li LIBDS - checks LIBREF.DATASET syntax
38 @li FORMAT - checks if the provided format is syntactically valid
39 @li ISPATH - checks that the value is an absolute path this platform would
40 accept. Shape only - whether the path exists is a different question
41 (mf_isdir / fileexist)
42 @param [out] outcol The variable to create, with the results of the match
43
44 <h4> SAS Macros </h4>
45 @li mf_getuniquename.sas
46
47 <h4> Related Macros </h4>
48 @li mp_validatecol.test.sas
49
50 @version 9.3
51**/
52
53%macro mp_validatecol(incol,rule,outcol);
54
55/* tempcol is given a unique name with every invocation */
56%local tempcol;
57%let tempcol=%mf_getuniquename();
58
59%if &rule=ISINT %then %do;
60 &outcol=0;
61 if not missing(&incol) then do;
62 &tempcol=input(&incol,?? best32.);
63 if not missing(&tempcol) then if mod(&tempcol,1)=0 then &outcol=1;
64 end;
65 drop &tempcol;
66%end;
67%else %if &rule=ISNUM %then %do;
68 /*
69 credit SOREN LASSEN
70 https://sasmacro.blogspot.com/2009/06/welcome-isnum-macro.html
71 */
72 &tempcol=input(&incol,?? best32.);
73 if missing(&tempcol) then &outcol=0;
74 else &outcol=1;
75 drop &tempcol;
76%end;
77%else %if &rule=ISLIB %then %do;
78 if _n_=1 then do;
79 retain &tempcol;
80 &tempcol=prxparse('/^[_a-z]\w{0,7}\z/i');
81 if missing(&tempcol) then do;
82 putlog 'ERR' +(-1) "OR: Invalid expression for ISLIB";
83 stop;
84 end;
85 drop &tempcol;
86 end;
87 if prxmatch(&tempcol, trim(&incol)) then &outcol=1;
88 else &outcol=0;
89%end;
90%else %if &rule=ISNAME %then %do;
91 /* match a valid SAS name (V7 rules) */
92 if _n_=1 then do;
93 retain &tempcol;
94 &tempcol=prxparse('/^[_a-z]\w{0,31}\z/i');
95 if missing(&tempcol) then do;
96 putlog 'ERR' +(-1) "OR: Invalid expression for ISNAME";
97 stop;
98 end;
99 drop &tempcol;
100 end;
101 if prxmatch(&tempcol, trim(&incol)) then &outcol=1;
102 else &outcol=0;
103%end;
104%else %if &rule=LIBDS %then %do;
105 /* match libref.dataset */
106 if _n_=1 then do;
107 retain &tempcol;
108 &tempcol=prxparse('/^[_a-z]\w{0,7}\.[_a-z]\w{0,31}\z/i');
109 if missing(&tempcol) then do;
110 putlog 'ERR' +(-1) "OR: Invalid expression for LIBDS";
111 stop;
112 end;
113 drop &tempcol;
114 end;
115 if prxmatch(&tempcol, trim(&incol)) then &outcol=1;
116 else &outcol=0;
117%end;
118%else %if &rule=ISPATH %then %do;
119 /*
120 An absolute path on the current platform. This is a shape check: a path
121 that passes may still not exist, and one that fails would be rejected by
122 the operating system rather than read as a folder.
123 */
124 &outcol=0;
125 if length(trim(&incol))>0 and anycntrl(&incol)=0 then do;
126 %if "%upcase(&sysscp)"="WIN" or "%upcase(&sysscp)"="DNTHOST" %then %do;
127 /* a drive root (C:/folder) */
128 if substr(&incol,2,1)=':' and substr(&incol,3,1) in ('\','/') then
129 &outcol=1;
130 /* a UNC share needs both a host and a share - \\host\ is not openable */
131 if substr(&incol,1,2)='\\' and scan(&incol,2,'\') ne '' then
132 &outcol=1;
133 /* characters windows does not accept in a name, and a colon
134 anywhere but the drive separator */
135 if &outcol=1 then do;
136 if index(substr(&incol,3),':')>0 then &outcol=0;
137 if index(&incol,byte(34))>0 then &outcol=0;
138 if index(&incol,'<')>0 then &outcol=0;
139 if index(&incol,'>')>0 then &outcol=0;
140 if index(&incol,'|')>0 then &outcol=0;
141 if index(&incol,'?')>0 then &outcol=0;
142 if index(&incol,'*')>0 then &outcol=0;
143 end;
144 %end;
145 %else %do;
146 if substr(&incol,1,1)='/' then &outcol=1;
147 %end;
148 end;
149%end;
150%else %if &rule=FORMAT %then %do;
151 /* match valid SAS format*/
152 if _n_=1 then do;
153 retain &tempcol;
154 &tempcol=prxparse('/^(?:[_a-z\$]\w{0,31}|[0-9]+)\.[0-9]*\z/i');
155 if missing(&tempcol) then do;
156 putlog 'ERR' +(-1) "OR: Invalid expression for FORMAT";
157 stop;
158 end;
159 drop &tempcol;
160 end;
161 if prxmatch(&tempcol, trim(&incol)) then &outcol=1;
162 else &outcol=0;
163%end;
164
165%mend mp_validatecol;