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, surround it with double quotation marks. For example, to specify the character value Tom's as a constant, enter

 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.

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 quoted values. 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 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:

 '534153'x 

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; 
Caution  

Trailing blanks or leading blanks cause an error. 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:

 part/all*100 

Numeric constants can be expressed in standard notation in the following ways:

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

  • 0c1x

  • 9x

You can use numeric hex 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.

Any trailing blanks or leading blanks included within the quotation marks will not affect the processing of the date constant , time constant , or datetime constant .

Use the following patterns to create date and time constants:

' ddmmm < yy > yy 'D or " ddmmm < yy > yy "D represents a SAS date value:

  • date='1jan2006'd;

  • date='01jan04'd;

' hh:mm < :ss.s >'T or " hh:mm < :ss.s >"T represents a SAS time value:

  • time='9:25't;

  • time='9:25:19pm't;

' ddmmm < yy > yy : hh:mm < :ss.s >'DT or " ddmmm < yy > yy : hh:mm < :ss.s >"DT represents a SAS datetime value:

  • if begin='01may04:9:30:00'dt then end='31dec90:5:00:00'dt;

  • dtime='18jan2003:9:27:05am'dt;

For more information on SAS dates, refer to Chapter 8, "Dates, Times, and Intervals," on page 127.

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:

expression

can be any valid SAS expression. Both character and numeric variables can be bit tested . When SAS tests a character value, it aligns the left-most bit of the mask with the left-most bit of the string; the test proceeds through the corresponding bits, moving to the right. When SAS tests a numeric value, the value is truncated from a floating-point number to a 32-bit integer. The right-most bit of the mask is aligned with the right-most bit of the number, and the test proceeds through the corresponding bits, moving to the left.

comparison-operator

compares an expression with the bit mask. Refer to "Comparison Operators" on page 118 for a discussion of these operators.

bit-mask

is a string of 0s, 1s, and periods in quotation marks that is immediately followed by a B. Zeros test whether the bit is off; ones test whether the bit is on; and periods ignore the bit. Commas and blanks can be inserted in the bit mask for readability without affecting its meaning.

Caution  

Truncation can occur when SAS uses a bit mask. If the expression is longer than the bit mask, SAS truncates the expression before it compares it with the bit mask. A false comparison may result. An expression's length (in bits) must be less than or equal to the length of the bit mask. If the bit mask is longer than a character expression, SAS prints a warning in the log, stating that the bit mask is truncated on the left, and continues processing.

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 $BINARY w . and BINARY w . formats and the $BINARY w ., BINARY w . d , and BITS w . 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.

Avoiding a Common Error With Constants

When you use a string in quotation marks followed by a variable name, always put a blank space between the closing quotation mark and the variable name. Otherwise, SAS might interpret a character constant followed by a variable name as a special SAS constant as illustrated in this table.

Table 7.2: Characters That Cause Misinterpretation When Following a Character Constant

Characters that follow a character constant

Possible interpretation

Examples

b

bit testing constant

'00100000'b

d

date constant

'01jan04'd

dt

datetime constant

'18jan2005:9:27:05am'dt

n

name literal

'My Table'n

t

time constant

'9:25:19pm't

x

hexadecimal notation

'534153'x

In the following example, '821't is evaluated as a time constant. For more information about SAS time constants, see "Date, Time, and Datetime Constants" on page 113.

 data work.europe;      set ia.europe;      if flight='821'then         flight='230';   run; 

The program writes the following lines to the SAS log:

Output 7.1: Log Results from an Error Caused by a Time Literal Misinterpretation
start example
 ERROR: Invalid date/time/datetime constant '821't. ERROR 77--185: Invalid number conversion on '821't. ERROR 388--185: Expecting an arithmetic operator. 
end example
 

Inserting a blank space between the ending quotation mark and the succeeding character in the IF statement eliminates this misinterpretation. No error message is generated and all observations with a FLIGHT value of 821 are replaced with a value of 230.

 if flight='821' then      flight='230'; 



SAS 9.1.3 Language Reference. Concepts
SAS 9.1.3 Language Reference: Concepts, Third Edition, Volumes 1 and 2
ISBN: 1590478401
EAN: 2147483647
Year: 2004
Pages: 258

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net