Names in the SAS Language

Definition of a SAS Name

A SAS name is a name token that represents one of the following:
  • variables
  • SAS data sets
  • formats or informats
  • SAS procedures
  • options
  • arrays
  • statement labels
  • SAS macros or macro variables
  • SAS catalog entries
  • librefs or filerefs
  • component objects
There are two types of names in SAS:
  • names of elements of the SAS language
  • names supplied by SAS users

Rules for User-Supplied SAS Names

Rules for Most SAS Names

The following list contains the rules that you use when you create most SAS name:
Note: The rules are more flexible for SAS variable names, data set names, view names, and item store names than for other language elements. See Rules for SAS Variable Names and Rules for SAS Data Set Names, View Names, and Item Store Names.
  • The length of a SAS name depends on which element it is assigned to. Many SAS names can be 32 characters long; others have a maximum length of 8 characters. For a list of SAS names and their maximum length, see Maximum Length in Characters of User-Supplied SAS Names.
  • 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.
  • You can use uppercase or lowercase letters.
  • Blanks cannot appear in SAS names.
  • Special characters, except for the underscore, are not allowed. In filerefs only, you can use the dollar sign ($), the pound sign (#), and the 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 libref names:
      • SASHELP
      • SASMSG
      • SASUSER
      • WORK
    • When you create SAS data sets, do not use these names:
      • _NULL_
      • _DATA_
      • _LAST_
  • 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.
Maximum Length in Characters of User-Supplied SAS Names
User-Supplied SAS Name
Maximum Length in Characters
Arrays
32
CALL routines
16
Catalog entries
32
Component objects
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

Rules for SAS Variable Names

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 create 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:
V7
is the default setting.
Variable name follows these rules:
  • The length of a SAS variable names 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 variable names, SAS internally converts it to uppercase. You cannot, therefore, use the same variable name with a different combination of upper 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.
Examples:season=’summer’;

percent_of_profit=percent;

UPCASE
is the same as V7, except that variable names are uppercased, as in earlier versions of SAS.
ANY
  • The name can begin with or contain any characters, including blanks, national characters, special characters, and multi-byte characters.
  • The name can be up to 32 bytes in length.
  • The name cannot contain any null bytes.
  • Leading blanks are preserved, but the trailing blanks are ignored.
  • The name must contain at least one character. A name with all blanks is not permitted.
  • The 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 uppercases. You cannot, therefore, 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.
Requirement: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 the name includes 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 and Avoiding Errors When Using Name Literals.
Examples:‘% of profit’n=percent;

‘items@warehouse’n=itemnum;

CAUTION:
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.
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.

Rules for SAS Data Set Names, View Names, and Item Store Names

Three types of SAS members, SAS data sets, data views, and item stores, are expanded to have more functionality. The setting of the VALIDMEMNAME= system option determines what rules apply to the names of these members in your SAS session. The VALIDMEMNAME= option has two settings (COMPATIBLE and EXTEND), each with varying degrees of flexibility for data set names, data view names, and item store names:
COMPATIBLE
specifies that a SAS data set name, a view name, or an item store name must follow these rules:
  • The length of the names can be up to 32 characters.
  • Names 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.
  • Names cannot contain blanks or special characters except for the underscore.
  • Names can contain mixed-case letters. SAS internally converts the member name to uppercase. You cannot, therefore, use the same member name with a different combination of uppercase and lowercase letters to represent different variables. For example, customer, Customer, and CUSTOMER all represent the same member name. How the name on the disk appears is determined by the operating environment.
Alias:COMPAT
EXTEND
specifies that a SAS data set name, a SAS view name, or an item store name must follow these rules:
  • Names can include national characters.
  • The name can include special characters, except for the / \ * ? " < > |: - characters.
    Note: The SPD engine does not allow ‘.’ (the period) anywhere in the member name.
  • The name must contain at least one character.
  • The length of a name can be up to 32 bytes.
  • Null bytes are not allowed.
  • Names cannot begin with a blank or a ‘.’ (the period).
    Note: The SPD engine does not allow ‘$’ as the first character of the member name.
  • Leading and trailing blanks are deleted when the member is created.
  • Names can contain mixed-case letters. SAS internally converts the member name to uppercase. You cannot, therefore, use the same member name with a different combination of uppercase and lowercase letters to represent different variables. For example, customer, Customer, and CUSTOMER all represent the same member name. How the name appears is determined by the operating environment.
Restriction:The windowing environment supports the extended rules in the Program, Log, and Output windows when VALIDMEMNAME=EXTEND is set. In most SAS windows, these extended rules are not supported. For example, these rules are not supported in SAS Explorer, the VIEWTABLE window, and windows that you open using the Solutions menu.
Requirement:If you use any characters other than the ones that are valid when the VALIDMEMNAME= system option is set to COMPAT (letters of the Latin alphabet, numerals, or underscores), then you must express the variable name as a name literal and you must set VALIDMEMNAME=EXTEND. 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. For more information, see SAS Name Literals.
Tip:The name is displayed in uppercase letters.
Examples:data “August Purchases”n;

data ‘Años de empleo’n.;

CAUTION:
Throughout SAS, using the name literal syntax with SAS member names that exceed the 32-byte limit or have excessive embedded quotation marks might cause unexpected results.
The intent of the VALIDMEMNAME=EXTEND system option is to enable compatibility with other DBMS member naming conventions, such as allowing embedded blanks and national characters.
CAUTION:
Using the special character # when VALIDMEMNAME=EXTEND could cause a SAS data set to be overwritten by a generation data set.
When VALIDMEMNAME= is set to EXTEND, it is possible to name a SAS data set name that uses the naming conventions for generation data sets, which append the special character # and a three-digit number to its member name. To avoid conflict, do not name SAS data sets similar to archived SAS data sets. For example, for a data set named A, generation data sets would automatically be named A#001, A#002, and so on. If you name a SAS data set A#003, the SAS data set could be deleted by SAS in the process of adding to a generation group.
Note: The VALIDMEMNAME= option is not valid for the following tape engines: V9TAPE, V8TAPE, V7TAPE, and V6TAPE.

How Many Characters Can I Use When I Measure SAS Name Lengths in Bytes?

When VALIDVARNAME=ANY or VALIDMEMNAME=EXTEND, the length of these SAS names must be measured in bytes:
System Option Setting
SAS Name Measured in Bytes
Maximum Length in Bytes
VALIDVARNAME=ANY
variable names
32
VALIDMEMNAME=EXTEND
SAS data set name
SAS view name
item store name
32
When these system option values are set, the maximum number of characters that you can use for a SAS variable name, data set name, view name, or item store name is determined by the number of bytes of storage that are used to store one character. This value is set by the SAS encoding value for your SAS session. VALIDVARNAME=ANY or VALIDMEMNAME=EXTEND must be set to allow the use of national language support (NLS) characters. Otherwise, only one-byte characters are allowed.
The SAS encodings for western languages use one byte of storage to store one character. Therefore, in western languages, you can use 32 characters for these SAS names. The SAS encoding for some Asian languages use one to two bytes of storage to store one character. The Unicode encoding, UTF-8, supports one to four bytes of storage for a single character. When the SAS encoding uses four bytes to store one character, the maximum length of one of these SAS names is eight characters.
All SAS encodings support the characters A–Z and a–z as one-byte characters.
Follow these instructions for finding the maximum number of characters that can be used for a SAS name:
  1. Find the SAS encoding in one of the following ways:
    • Find the ENCODING= system option in the SAS System Options window:
      1. Type options in the command bar.
      2. Right-click Options and select Find Option.
      3. Type encoding and click OK.
    • In an editor window, specify the ENCODING= system option in the OPTIONS procedure:
      proc options option=encoding;
      run;
  2. In the table “SBCS, DBCS, and Unicode Encoding Values Used to Transcode Data” , find the maximum characters per byte for the SAS encoding. This table is in SAS National Language Support (NLS): Reference Guide.
  3. Find the maximum number of bytes for a SAS name from Maximum Length in Characters of User-Supplied SAS Names. Divide this number by the characters per byte. The result is the maximum number of characters that you can use for the SAS name.

SAS Name Literals

Definition of 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. Most SAS names allow only the characters _, A–Z, and a–z. Name literals enable you to use characters (including blanks and national characters) that are not otherwise allowed.
You can use name literals in these types of SAS names:
  • DBMS column names
  • DBMS table
  • item store
  • SAS data set
  • SAS view
  • statement label
  • variable
To use characters in a name literal other than _, A–Z, or a–z, you must set either the VALIDVARNAME=ANY or VALIDMEMNAME=EXTEND system options. The following table specifies the options that you must set to use SAS name literals.
SAS Name Literal System Option Requirements
SAS Name Type
Name Literal Requirements
DBMS column
Set VALIDVARNAME=ANY.
DBMS table name
Set VALIDVARNAME=ANY.
item store
Set VALIDMEMNAME=EXTEND.
SAS data set name
Set VALIDMEMNAME=EXTEND.
SAS view
Set VALIDMEMNAME=EXTEND.
statement label
Set VALIDVARNAME=ANY.
variable
Set VALIDVARNAME=ANY.
Name literals are especially useful for expressing DBMS column and table names that contain special characters and for including national characters in SAS names.
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;

SAS Name Literal Examples

Here are some examples of SAS name literals:
  • libname foo SAS/ACCESS-engine-name 
                SAS/ACCESS-engine-connection-options;
    data foo.'My Table'n;
  • data ‘Años de empleo’n.;
  • data “August Purchases”n;
  • input 'Bob''s Asset Number'n;
  • input “Bob's Asset Number”n;
  • input 'Amount Budgeted'n 'Amount Spent'n 
          'Amount Difference'n;
  • ‘Statement Label 1’n:

Important Restrictions

  • You can use a name literal only for variables, statement labels, DBMS column and table names, SAS data sets, SAS view, and item stores.
  • When the name literal of a SAS data set name, a SAS view name, or an item store name contains any characters that are not allowed when VALIDMEMNAME=EXTEND, then you must set the system option VALIDMEMNAME=EXTEND. See VALIDMEMNAME= System Option in SAS System Options: Reference.
  • When the name literal of a variable, DBMS table, or DBMS column contains any characters that are not allowed when VALIDVARNAME=V7, then you must set the system option VALIDVARNAME=ANY. See VALIDVARNAME= System Option in SAS System Options: Reference.
  • 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, 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.
  • Blanks between the closing quotation mark and the n are not valid when you specify a name literal.
  • Note that even if you set VALIDVARNAME=ANY, the V6 engine does not support names that have intervening blanks.

Using Name Literals in BY Groups

When you designate a name literal as the BY variable in BY-group processing and you want to refer to the corresponding FIRST. or LAST. temporary variables, you must include the FIRST. or LAST. portion of the two-level variable name within quotation marks. Here is an example:
data sedanTypes; 
   set cars; 
   by 'Sedan Types'n; 								
   if 'first.Sedan Types'n then type=1;   
run;  													
For more information about BY-Group Processing and how SAS creates the temporary variables, FIRST and LAST, see How SAS Determines FIRST.variable and LAST.variable and How SAS Identifies the Beginning and End of a BY Group in SAS Statements: Reference..

Avoiding Errors When Using Name Literals

For information about how to avoid creating name literals in error, see Avoiding a Common Error with Constants.