Rules for Words and Names in the SAS Language |
Definition of a SAS Name |
A SAS name is a name token that represents
component objects
There are two types of names in SAS:
Rules for User-Supplied SAS Names |
Note: The rules are more flexible for SAS variable names than for other language elements. See Rules for SAS Variable Names.
The length of a SAS name depends on the element it is assigned to. Many SAS names can be 32 characters long; others have a maximum length of 8.
The first character must be an English letter (A, B, C, . . ., Z) or underscore (_). Subsequent characters can be letters, numeric digits (0, 1, . . ., 9), or underscores.
Special characters, except for the underscore, are not allowed. In filerefs only, you can use the dollar sign ($), pound sign (#), and at sign (@).
SAS reserves a few names for automatic variables and variable lists, SAS data sets, and librefs.
When creating variables, do not use the names of special SAS automatic variables (for example, _N_ and _ERROR_) or special variable list names (for example, _CHARACTER_, _NUMERIC_, and _ALL_).
When associating a libref with a SAS library, do not use these:
When you create SAS data sets, do not use these names:
When assigning a fileref to an external file, do not use the filename SASCAT.
When you create a macro variable, do not use names that begin with SYS.
SAS Language Element | Maximum Length |
---|---|
Arrays | 32 |
CALL routines | 16 |
Catalog entries | 32 |
DATA step statement labels | 32 |
DATA step variable labels | 256 |
DATA step variables | 32 |
DATA step windows | 32 |
Engines | 8 |
Filerefs | 8 |
Formats, character | 31 |
Formats, numeric | 32 |
Functions | 16 |
Generation data sets | 28 |
Informats, character | 30 |
Informats, numeric | 31 |
Librefs | 8 |
Macro variables | 32 |
Macro windows | 32 |
Macros | 32 |
Members of SAS libraries (SAS data sets, SAS views, catalogs, indexes) except for generation data sets | 32 |
Passwords | 8 |
Procedure names (first eight characters must be unique, and cannot begin with "SAS") | 16 |
SCL variables | 32 |
The rules for SAS variable names have expanded to provide more functionality. The setting of the VALIDVARNAME= system option determines what rules apply to the variables that you can create and process in your SAS session as well as to variables that you want to read from existing data sets. The VALIDVARNAME= option has three settings (V7, UPCASE, and ANY), each with varying degrees of flexibility for variable names:
Variable name rules are as follows:
The first character must begin with an English letter or an underscore. Subsequent characters can be English letters, numeric digits, or underscores.
A variable name cannot contain any special characters other than the underscore.
A variable name can contain mixed case. Mixed case is remembered and used for presentation purposes only. (SAS stores the case used in the first reference to a variable.) When SAS processes variable names, however, it internally uppercases them. You cannot, therefore, use the same letters with different combinations of lowercase and uppercase to represent different variables. For example, cat, Cat, and CAT all represent the same variable.
You do not assign the names of special SAS automatic variables (such as _N_ and _ERROR_) or variable list names (such as _NUMERIC_, _CHARACTER_, and _ALL_) to variables.
is the same as V7, except that variable names are uppercased, as in earlier versions of SAS.
The name can contain special and multi-byte characters not to exceed 32 bytes.
The name cannot contain any null bytes.
A name can contain blanks. The leading blanks are preserved, but the trailing blanks are ignored.
A name must contain at least one character. A name with all blanks is not permitted.
The name can start with or contain any characters, including blanks.
Note: If you use any characters other than the ones that are valid when the VALIDVARNAME system option is set to V7 (letters of the Latin alphabet, numerals, or underscores), then you must express the variable name as a name literal and you must set VALIDVARNAME=ANY.
If you use either the percent sign (%) or the ampersand (&), then you must use single quotation marks in the name literal in order to avoid interaction with the SAS Macro Facility. See SAS Name Literals.
A variable name can contain mixed-case letters. Mixed-case letters are stored and used for presentation purposes only. (SAS stores the case that is used in the first reference to a variable.) When SAS processes variable names, however, it internally uppercases them. Therefore, you cannot use the same letters with different combinations of lowercase and uppercase to represent different variables. For example, cat, Cat, and CAT all represent the same variable.
Warning: | The intent of the VALIDVARNAME=ANY system option is to enable compatibility with other DBMS variable (column) naming conventions, such as allowing embedded blanks and national characters. Throughout SAS, using the name literal syntax with variable names that exceed the 32-byte limit or have excessive embedded quotation marks might cause unexpected results. |
SAS Name Literals |
A SAS name literal is a name token that is expressed as a string within quotation marks, followed by the upper- or lowercase letter n. Name literals enable you to use special characters (including blanks) that are not otherwise allowed in SAS names when you specify a SAS data set or a variable. Blanks between the closing quotation mark and the n are not valid when you specify a name literal. When the name literal of a data set or variable contains any characters that are not allowed when VALIDVARNAME=V7, then you must set the VALIDVARNAME= system option to ANY. Note that even if you set the system option to ANY, the V6 engine does not support names that have intervening blanks.
Name literals are especially useful for expressing DBMS column and table names that contain special characters.
The following is an example of a VAR statement and a name literal:
var 'a b'n;
The following is an example of a VAR statement with variables A and B:
var a b;
You can use a name literal only for variables, statement labels, and DBMS column and table names.
When the name literal of a variable or DBMS column contains any characters that are not allowed when VALIDVARNAME=V7, then you must set the system option VALIDVARNAME=ANY.
Note: For more details about the VALIDVARNAME=ANY system option, see SAS Language Reference: Dictionary.
If you use either the percent sign (%) or the ampersand (&), then you must use single quotation marks in the name literal in order to avoid interaction with the SAS Macro Facility.
When the name literal of a DBMS table or column contains any characters that are not valid for SAS rules, then you might need to specify a SAS/ACCESS LIBNAME statement option.
Note: For more details and examples about the SAS/ACCESS LIBNAME statement and about using DBMS table and column names that do not conform to SAS naming conventions, see SAS/ACCESS for Relational Databases: Reference.
In a quoted string, SAS preserves and uses leading blanks, but SAS ignores and trims trailing blanks.
For information on how to avoid creating name literals in error, see Avoiding a Common Error with Constants.
Examples of SAS name literals are
input 'Bob''s Asset Number'n;
input "Bob's Asset Number"n;
libname foo SAS/ACCESS-engine-name SAS/ACCESS-engine-connection-options; data foo.'My Table'n;
input 'Amount Budgeted'n 'Amount Spent'n 'Amount Difference'n;
Copyright © 2010 by SAS Institute Inc., Cary, NC, USA. All rights reserved.