Validating Data Values - SAS Programming

The VERIFY function,as its name implies,can be used to verify that a character variable contains only valid characters.It takes the following form:

VERIFY (char_var, verify_string);

If all the characters in char_var are present in verify_string, the VERIFY function returns a 0.Otherwise, it returns the position of the first character in char_var that is not in verify_string. Sound confusing? Yes, we think so too.Here is an example to help clarify it: Suppose you want to verify that only the characters A, B, C, D, E are present in the variable ANSWER. (Assume that ANSWER has been defined with a length of 1 in data set OLD.) Using the VERIFY function, you have:

Example

DATA _NULL_;
SET OLD;
RETAIN CHECK 'ABCDE';
IF VERIFY (ANSWER,CHECK) NE 0 THEN PUT
'ERROR IN RECORD ' _N_ ANSWER=;
RUN;

There's actually much going on in this small program that deserves explanation.To begin with, there is that funny data set name _NULL_.This is a SAS System reserved name.It instructs the system to go through all the motions of creating a data set called _NULL_, but not to actually create it.Since the only purpose of the program is to check for errors and report on them, there's no need to create a real data set.

The _NULL_ data set name is often used in conjunction with the PUT statement, as it is here. Data values are read in with an INPUT or a SET statement, and they are written (in this case to the SAS log) with the PUT statement. Notice that the PUT statement here first writes the literal' ERROR IN RECORD '(including the trailing blank),then it writes the observation number of the offending record and, finally,it writes the value of the variable ANSWER prefixed with the string ANSWER=.The use of DATA _NULL_ is highly recommended as an efficiency measure. No need to take up resources (CPU and storage) to write a data set that you don't really need.

The next feature needing explanation is the RETAIN statement. You want to verify that the 1-byte value of ANSWER is contained in the string CHECK. You could have written the statement CHECK='ABCDE'; in the code,and that would have created the identical value for CHECK for each observation, but you used an alternate method instead.You told the system to initialize a variable called CHECK with the value ABCDE and to retain it for each succeeding observation.

Now for the heart of the example, the VERIFY statement. It's really very simple.For each observation, if the value of NSWER is not contained in one of the characters making up the string contained in variable CHECK (remember, the string is ABCDE for each observation), the value returned by VERIFY is not equal to 0 (0 is good!). The IF statement then produces a report of the "bad" observation.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics