SAS Constants in Expressions
Definition
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:
character
numeric
date, time, and datetime
bit testing
Character 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 with Character Constants
In the following SAS statement, Tom is a character constant:
if name='Tom' then do;
If a character constant includes a single quotation mark, enclose it in double quotation
marks. For example, to specify the character value Tom's as a constant, enter the
following:
name="Tom's"
Another way to write the same string is to enclose the string in single quotation marks
and to express the apostrophe as two consecutive quotation marks. SAS treats the two
consecutive quotation marks as one quotation mark:
name='Tom''s'
The same principle holds true for double quotation marks:
name="Tom""s"
CAUTION:
Matching quotation marks correctly is important. Missing or extraneous quotation
marks cause SAS to misread both the erroneous statement and the statements that
follow it. For example, in name='O'Brien';, O is the character value of Name,
Brien is extraneous, and '; begins another quoted string.
SAS Constants in Expressions 93
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:
x='abc';
if name='Smith' then do;
The following statements use character variables:
x=abc;
if name=Smith then do;
In the second set of examples, SAS searches for variables named ABC and SMITH,
instead of constants.
Note: SAS distinguishes between uppercase and lowercase when comparing character
expressions. For example, the character values 'Smith' and 'SMITH' are not
equivalent.
Character Constants Expressed in Hexadecimal Notation
SAS character constants can be expressed in hexadecimal notation. A character
hexadecimal constant is a string of an even number of hexadecimal characters enclosed
in single or double quotation marks, followed immediately by an X, as in this example:
'534153'x
A comma can be used to make the string more readable, but it is not part of and does not
alter the hexadecimal value. If the string contains a comma, the comma must separate an
even number of hexadecimal characters within the string, as in this example:
if value='3132,3334'x then do;
Note: Any trailing blanks or leading blanks within the quotation marks cause an error
message to be written to the log.
Numeric Constants
A numeric constant is a number that appears in a SAS statement. Numeric constants can
be presented in many forms, including
standard notation
scientific (E) notation
hexadecimal notation
Numeric Constants Expressed in Standard Notation
Most numeric constants are written just as numeric data values are. The numeric
constant in the following expression is 100:
94 Chapter 6 Expressions
part/all*100
Numeric constants can be expressed in standard notation in the following ways:
Table 6.1 Standard Notation for Numeric Constants
Numeric Constant Description
1 is an unsigned integer
–5 contains a minus sign
+49 contains a plus sign
1.23 contains decimal places
01 contains a leading zero, which is not
significant
Numeric Constants Expressed in Scientific Notation
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 2x10
4
or 20,000.
For numeric constants larger than (10
32
)−1, you must use scientific notation. Additional
examples follow:
1.2e23
0.5e-10
Numeric Constants Expressed in Hexadecimal Notation
A numeric constant that is expressed as a hexadecimal value starts with a numeric digit
(usually 0), can be followed by more hexadecimal characters, and ends with the letter X.
The constant can contain up to 16 valid hexadecimal characters (0 to 9, A to F). The
following are numeric hexadecimal constants:
0c1x
9x
You can use numeric hexadecimal constants in a DATA step, as follows:
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.
SAS Constants in Expressions 95

Get SAS 9.4 Language Reference, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.