Previous Page | Next Page

Working with Character Variables

Identifying Character Variables and Expressing Character Values

To store character values in a SAS data set, you need to create a character value. One way to create a character variable is to define it in an input statement. Simply place a dollar sign after the variable name in the INPUT statement, as shown in the DATA step that created AIR.DEPARTURES:

input Country $ 1-9 CitiesInTour 11-12 USGate $ 14-26 
      ArrivalDepartureGates $ 28-48;

You can also create a character variable and assign a value to it in an assignment statement. Simply enclose the value in quotation marks:

Schedule = '3-4 tours per season';

Either single quotation marks (apostrophes) or double quotation marks are acceptable. If the value itself contains a single quote, then surround the value with double quotation marks, as in

Remarks = "See last year's schedule";

Note:   Matching quotation marks properly is important. Missing or extraneous quotation marks cause SAS to misread both the erroneous statement and the statements following it.  [cautionend]

When you specify a character value in an expression, you must also enclose the value in quotation marks. For example, the following statement compares the value of USGate to San Francisco and, when a match occurs, assigns the airport code SFO to the variable Airport:

if USGate = 'San Francisco' then Airport = 'SFO';

In character values, SAS distinguishes uppercase letters from lowercase letters. For example, in the data set AIR.DEPARTURES, the value of USGate in the observation for Australia is Honolulu . The following IF condition is true; therefore, SAS assigns to Airport the value HNL:

else if USGate = 'Honolulu' then Airport = 'HNL';

However, the following condition is false:

if USGate = 'HONOLULU' then Airport = 'HNL';

SAS does not select that observation because the characters in Honolulu and HONOLULU are not equivalent.

The following program places these shaded statements in a DATA step:

options pagesize=60 linesize=80 pageno=1 nodate;

data charvars;
   set mylib.departures;
   Schedule = '3-4 tours per season';
   Remarks = "See last year's schedule";
   if USGate = 'San Francisco' then Airport = 'SFO';
    else if USGate = 'Honolulu' then Airport = 'HNL';
run;

proc print data=charvars noobs1 ;
   var Country Schedule Remarks USGate Airport;
   title 'Tours By City of Departure';
run;

[1]The NOOBS option in the PROC PRINT statement suppresses the display of observation numbers in the output.

The following output displays the character variables in the data set CHARVARS:

Examples of Character Variables

                           Tours By City of Departure                          1

 Country         Schedule               Remarks          USGate        Airport

 Japan     3-4 tours per season See last year's schedule San Francisco   SFO  
 Italy     3-4 tours per season See last year's schedule New York             
 Australia 3-4 tours per season See last year's schedule Honolulu        HNL  
 Venezuela 3-4 tours per season See last year's schedule Miami                
 Brazil    3-4 tours per season See last year's schedule                      

Previous Page | Next Page | Top of Page