Creating a Character Expression

In this section:

A character expression uses alphanumeric constants, fields, concatenation operators, IF-THEN-ELSE logic, or functions to derive an alphanumeric value.

Both text and alphanumeric fields can be assigned values stored in text fields or alphanumeric expressions in TABLE COMPUTE, MODIFY COMPUTE, and DEFINE commands. If an alphanumeric field is assigned the value of a text field that is too long for the alphanumeric field, the value is truncated before being assigned to the alphanumeric field.

A character expression can consist of:

Note:

Embedding a Quotation Mark in a Quote-Delimited Literal String

Under certain conditions, you can use quote-delimited strings containing embedded quotation marks. Within the string, you can use either one single quotation mark or two contiguous single quotation marks to represent the single quotation mark. Both are interpreted as a single quotation mark.

You can use quote-delimited strings in the following instances:

Example: Specifying the Data Value O'BRIEN in a Quote-Delimited Literal String

The following example illustrates the use of quotation marks for the correct interpretation of the data value O'BRIEN:

TABLE FILE VIDEOTRK
PRINT LASTNAME
WHERE LASTNAME IS 'O'BRIEN'
END

Concatenating Character Strings

You can write an expression that concatenates two or more alphanumeric constants and/or fields into a single character string. This concatenation operator has two forms, as shown in the following table:

Symbol

Represents

Description

|

Weak concatenation

Preserves trailing blanks.

||

Strong concatenation

Moves trailing blanks to the end of a concatenated string.

Example: Concatenating Character Strings

The following example uses the EDIT function to extract the first initial from a first name. It then uses both strong and weak concatenation to produce the last name, followed by a comma, followed by the first initial, followed by a period:

DEFINE FILE EMPLOYEE
FIRST_INIT/A1 = EDIT(FIRST_NAME, '9$$$$$$$$$');
NAME/A19 = LAST_NAME ||(', '| FIRST_INIT |'.');
END
TABLE FILE EMPLOYEE
PRINT NAME WHERE LAST_NAME IS 'BANNING'
END

The output is:

NAME       
----       
BANNING, J.

The request evaluates the expressions as follows:

  1. The EDIT function extracts the initial J from FIRST_NAME.
  2. The expression in parentheses returns the value:
    , J.
  3. LAST_NAME is concatenated to the string derived in step 2 to produce:
    Banning, J.

    While LAST_NAME has the format A15 in the EMPLOYEE Master File, strong concatenation suppresses the trailing blanks. Regardless of the suppression or inclusion of blanks, the resulting field name, NAME, has a length of 19 characters (A19).

Example: Using IF-THEN-ELSE Logic in a Character Expression

The following request uses IF-THEN-ELSE logic to determine what characters to concatenate to MOVIECODE in order to compute NEWCODE.

TABLE FILE MOVIES
PRINT COPIES
MOVIECODE
COMPUTE
NEWCODE/A20 =  MOVIECODE |(IF MOVIECODE CONTAINS 'DIS' THEN  'NEY;' ELSE ';');
BY CATEGORY
WHERE CATEGORY EQ 'CHILDREN'
ON TABLE SET PAGE NOLEAD
ON TABLE SET STYLE *
GRID=OFF,$
ENDSTYLE
END

The output is shown in the following image. If MOVIECODE contains the characters 'DIS', NEWCODE is generated by concatenating the characters 'NEY;', otherwise NEWCODE is generated by concatenating the character ';'.