A SAS constant is a number or a character string that indicates a fixed value. Constants can be used as expressions in many SAS statements, including variable assignment and IF-THEN statements. They can also be used as values for certain options. Constants are also called literals.
The following are types of SAS constants:
A character constant consists of 1 to 32,767 characters and must be enclosed in quotation marks. Character constants can also be represented in hexadecimal form.
|Using Quotation Marks|
In the following SAS
Tom is a character constant:
if name='Tom' then do;If a character constant includes a single quotation mark, surround it with double quotation marks. For example, to specify the character value
Tom'sas a constant, enter
name="Tom's"You can also write a single quotation mark as two consecutive single quotation marks and SAS treats it as one. You can then surround the character constant with single quotation marks:
name='Tom''s'The same principle holds true for double quotation marks:
Ois the character value of NAME,
Brienis extraneous, and
';begins another quoted string.
|Comparing Character Constants and Character Variables|
It is important to remember that character constants are enclosed in quotation marks, but names of character variables are not. This distinction applies wherever you can use a character constant, such as in titles, footnotes, labels, and other descriptive strings; in option values; and in operating environment-specific strings, such as file specifications and commands.
The following statements use character constants:
if name='Smith' then do;
The following statements use character variables:
if name=Smith then do;
Note: SAS distinguishes between
uppercase and lowercase when comparing quoted values. For example, the character
'SMITH' are not equivalent.
SAS character constants can be expressed in hexadecimal notation. A character hex constant is a string of an even number of hex characters enclosed in single or double quotation marks, followed immediately by an X, as in this example:
A comma can be used to make the string more readable, but it is not part of and does not alter the hex value. If the string contains a comma, the comma must separate an even number of hex characters within the string, as in this example:
if value='3132,3334'x then do;
A numeric constant is a number that appears in a SAS statement. Numeric constants can be presented in many forms, including
Most numeric constants are written just as numeric data values are. The numeric constant in the following expression is 100:
part/all*100Numeric constants expressed in standard notation can be integers, can be specified with or without a plus or minus sign, and can include decimal places, as in these examples:
In scientific notation, the number before the E is multiplied by the power of ten that is indicated by the number after the E. For example, 2E4 is the same as 2x104 or 20,000. For numeric constants larger than (1032 )-1, you must use scientific notation. Additional examples follow:
A numeric constant that is expressed as a hexadecimal value starts with a numeric digit (usually 0), can be followed by more hexadecimal digits, and ends with the letter X. The constant can contain up to 16 valid hexadecimal digits (0 to 9, A to F). The following are numeric hex constants:
data test; input abend pib2.; if abend=0c1x or abend=0b0ax then do; ... more SAS statements ... run;
|Date, Time, and Datetime Constants|
You can create a date constant, time constant, or datetime constant by specifying the date or time in single or double quotation marks, followed by a D (date), T (time), or DT (datetime) to indicate the type of value. Use the following patterns to create date and time constants:
if begin='01may04:9:30:00'dt then end='31dec90:5:00:00'dt;
For more information on SAS dates, refer to Dates, Times, and Intervals.
|Bit Testing Constants|
Bit masks are used in bit testing to compare internal bits in a value's representation. You can perform bit testing on both character and numeric variables. The general form of the operation is:
|expression comparison-operator bit-mask|
The following are the components of the bit-testing operation:
The following example tests a character variable:
if a='..1.0000'b then do;
If the third bit of A (counting from the left) is on, and the fifth through eighth bits are off, the comparison is true and the expression result is 1. Otherwise, the comparison is false and the expression result is 0. The following is a more detailed example:
data test; input @88 bits $char1.; if bits='10000000'b then category='a'; else if bits='01000000'b then category='b'; else if bits='00100000'b then category='c'; run;
Note: Bit masks cannot be used as bit literals in assignment statements. For example, the following statement is not valid:
x='0101'b; /* incorrect */
The $BINARYw. and BINARYw. formats and the $BINARYw., BINARYw.d, and BITSw.d informats can be useful for bit testing. You can use them to convert character and numeric values to their binary values, and vice versa, and to extract specified bits from input data. See SAS Language Reference: Dictionary for complete descriptions of these formats and informats.
Top of Page
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.