BookmarkSubscribeRSS Feed
mkSAS1
Calcite | Level 5

Why do we need VALIDVARNAME=V7?  If this option is not included, is there any issue?Thank you for your comment.

7 REPLIES 7
PaigeMiller
Diamond | Level 26

According to the SAS documentation

 

VALIDVARNAME=V7 | UPCASE | ANY

Syntax Description

V7

specifies that variable names must follow these rules:

  • The name can be up to 32 characters.
  • The first character must begin with a letter of the Latin alphabet (A - Z, a - z) or the underscore. Subsequent characters can be letters of the Latin alphabet, numerals, or underscores.
  • Trailing blanks are ignored. The variable name alignment is left-justified.
  • A variable name cannot contain blanks or special characters except for the underscore.
  • A variable name can contain mixed-case letters. SAS stores and writes the variable name in the same case that is used in the first reference to the variable. However, when SAS processes a variable name, SAS internally converts it to uppercase. Therefore, you cannot use the same variable name with a different combination of uppercase and lowercase letters to represent different variables. For example, cat, Cat, and CAT all represent the same variable.
  • Do not assign variables the names of special SAS automatic variables (such as _N_ and _ERROR_) or variable list names (such as _NUMERIC_, _CHARACTER_, and _ALL_) to variables.

And when you ask "Why do we need VALIDVARNAME=V7?" you may or may not need it, its an option you can choose, but you can also choose other options based on your current needs. Depending on what user interface you are accessing SAS through, some default to VALIDVARNAME=V7, while other interfaces use a different default, or you may want to change the default if that makes more sense in your situation.

--
Paige Miller
mkSAS1
Calcite | Level 5
Dear Paige,

Thank you so much for your explanation that educated me to have better
insight on the option.

Thank you again for your help.

Best,
Mike
WarrenKuhfeld
Ammonite | Level 13

If you have an old program that runs correctly with a default of VALIDVARNAME=V7, it might not run correctly anymore if you switch to an environment that has VALIDVARNAME=ANY as a default. So you can change your job or change the option.

mkSAS1
Calcite | Level 5
Thank you so much for your comment!
Ksharp
Super User
VALIDVARNAME=V7 could apply to all the module of SAS.
But VALIDVARNAME=ANY is only available to SAS/Base and SAS/Stat. If you are using other module like SAS/OR , SAS/IML that might generate some error info.
WarrenKuhfeld
Ammonite | Level 13

@Ksharp I was involved with discussions on VALIDVARNAME= (which we often referred to as VVN) at its various stages of development during my career at SAS. I wrote PROC TRANSREG, the first procedure to surface output data set coded variables for various model coding schemes. All those coded variables had to have names and labels created, hence my interest in VVN. I was always a bit dismayed that it was an option that kinda/sorta/mostly worked most of the time. I never thought that was up to SAS standards, but I never made the decisions, and it would have taken a lot of testing resources to get it up to the usual SAS standards.

 

A lot of time has passed since then, so I imagine most of the bugs have been worked out. I remember one time when the idea was being floated to make VVN=ANY the default, I argued it would break jobs because procedures like TRANSPOSE, TRANSREG, and others would create different names. I also argued that it wasn't fully tested and spent all of 15 minutes as I recall before I found a case where I could make it fail. I think they later did make VVN=ANY the default for Viya and kept it as VVN=V7 for the rest of SAS. I've been retired for 7 years, so things might have changed.

 

Part of my concern back in those days was that if it is going to be part of SAS, it needed to work for every crazy name that was syntactically valid. I argued that some of the names that were syntactically valid were nutty, and it would be better to impose different rules prohibiting them than to ensure that nutty names did not cause problems. With VVN=ANY, you could specify names as n-literals. The form was quote-text string-quote-'n'. Example: 'This is a name that has blanks'n. So how long can the longest n-literal be? You might think two quotes, one 'n', 32 characters, so the answer is 35. But you would be wrong. A single quote, followed by 32 pairs of double quotes (each resolving to a single double quote), followed by a single quote and an 'n' is a perfectly legitimate n-literal (at least it was when I was employed). Substitute single for double and double for single and you get another nutty but legitimate n-literal. I wanted a ceiling on n-literal length (like 40), but that argument never went anywhere.

 

It is extraordinarily difficult for a macro writer to handle n-literals if they have to extract variable names from a list. I requested SAS functions to make n-literal name and list processing easier, but that never got anywhere either. I imagine n-literals and VVN=ANY works well for most of SAS, but I imagine it is not hard to trip things up in macros that process variable lists.

Tom
Super User Tom
Super User

The fact that PROC output datasets generate different names for the variables is the most dangerous thing with using VALIDVARNAME=ANY.

 

The most annoying thing is having to type name literals into your code.

 

There is the NLITERAL() function that can help in code generation functions.

 

To your last point you can use the Q modifier on %SCAN() to help with name literals in variable lists.  The DEQUOTE() function also helps.

 73          %macro test(varlist,sep=%str( ));
 74          %local name i ;
 75          %do i=1 %to %sysfunc(countw(&varlist,&sep,q));
 76             %let name=%sysfunc(nliteral(%sysfunc(dequote(%qscan(&varlist,&i,%str( ),q)))));
 77             %put &=i &=name;
 78          %end;
 79          %mend;
 80         
 81          %test(a 'b c'n x)
 I=1 NAME=a
 I=2 NAME="b c"N
 I=3 NAME=x

 

SAS has the NVALID() function that can check if a string is a valid variable names.

You could also use this macro to check if a string can be used as valid for variable names or member names or pther types of names.

https://github.com/sasutils/macros/blob/c1f45370cbf72e7bd35806712fb2752a62f3e6cb/nvalid.sas

Here is part of the explanation of the type of names it can validate.

Meaning of TYPE value choice:
ANY - Any string from 1 to 32 characters long.
V7 - Only contains Letters, digits or _ and does not start with digit.
UPCASE - Same as V7 but lowercase letters not allowed.
V6 - Same as V7 but max length is 8 instead of 32.
NLITERAL - A name that is not V7 valid must be in NLITERAL form.
FILEREF - Same as V6 with additional test if FILEREF is defined
LIBREF  - Same as V6 with additional test if LIBREF is defined
FORMAT  - Same as V7 but allow $ prefix and exclude terminal digit
INFORMAT  - Same as V7 but allow $ prefix and exclude terminal digit
MEMNAME - Valid membername based on VALIDMEMNAME setting.
COMPAT - an alias for COMPATIBLE.
COMPATIBLE - Same as V7 but used for member name.
EXTEND - Same as NLITERAL except there are extra excluded characters.