Fix the NVALID function to properly validate a string as a potential name-literal
on 05-13-201401:01 PM - last edited on 10-10-201509:40 PM by LainieH
The NVALID function is supposed to validate a SAS name. The first parameter is the potential name. The second parameter, validvarname, can be one V7, ANY, and NLITERAL or it can be left blank. If blank, the parameter will use the setting of the VALIDVARNAME system option to decide whether the string is a valid name under the current settings.
The problem is that the function does not demand the name-literal-surrounds ('...'n or "..."n) even though any actual usage of the value WILL require this to surround the name. It does not validate the string as a name but rather its potential to be used as a name if first fed to the NLITERAL function. So NVALID will return true on both the following forms:
%put %sysfunc(NVALID(This Name Should Not Be Valid, NLITERAL)) ;
%put %sysfunc(NVALID('This Name Should Be Valid'n, NLITERAL)) ;
when it ought to return false on the first call since a macro variable with that string cannot be used as a SAS name. This limits its usefulness since the primary purpose of this function is to validate a potential name. It gives false positives. ANY fares no better...in fact, it is arguably worse because it doesn't properly evaluate the length if the name-literal surround is present.
Since macros may have been developed which rely on this behavior, the simplest method would be to add two additional options that behave as one would expect of a validation function. Perhaps NAMELIT (to require the name-literal surround and validate assuming ANY) and SYSNAMELIT (to require the name-literal surround and validate based on the system option).