Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mp_validatecol.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Used to validate variables in a dataset
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 @param [in] incol The column to be validated
24 @param [in] rule The rule to apply. Current rules:
25 @li ISNUM - checks if the variable is numeric
26 @li LIBDS - matches LIBREF.DATASET format
27 @param [out] outcol The variable to create, with the results of the match
28
29 <h4> SAS Macros </h4>
30 @li mf_getuniquename.sas
31
32 @version 9.3
33**/
34
35%macro mp_validatecol(incol,rule,outcol);
36
37/* tempcol is given a unique name with every invocation */
38%local tempcol;
39%let tempcol=%mf_getuniquename();
40
41%if &rule=ISNUM %then %do;
42 /*
43 credit S├śREN LASSEN
44 https://sasmacro.blogspot.com/2009/06/welcome-isnum-macro.html
45 */
46 &tempcol=input(&incol,?? best32.);
47 if missing(&tempcol) then &outcol=0;
48 else &outcol=1;
49 drop &tempcol;
50%end;
51%else %if &rule=LIBDS %then %do;
52 /* match libref.dataset */
53 if _n_=1 then do;
54 retain &tempcol;
55 &tempcol=prxparse('/^[_a-z]\w{0,7}\.[_a-z]\w{0,31}$/i');
56 if missing(&tempcol) then do;
57 putlog "%str(ERR)OR: Invalid expression for LIBDS";
58 stop;
59 end;
60 drop &tempcol;
61 end;
62 if prxmatch(&tempcol, trim(&incol)) then &outcol=1;
63 else &outcol=0;
64%end;
65
66%mend mp_validatecol;