SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Search||next

Transforming Data with SAS Functions
Modifying Character Values with Functions


TRIM Function

The TRIM function enables you to remove trailing blanks from character values. To learn about the TRIM function, let's complete a modification to the data set Hrd.Temp that requires this function.

The data set Hrd.Temp contains four address variables, Address, City, State, and Zip.


SAS Data Set Hrd.Temp
Agency ID Name Address City State Zip Phone BeginDate EndDate PayRate Days Hours
Administrative Support, Inc. F274 CICHOCK, ELIZABETH MARIE 65 ELM DR CARY NC 27513 6224549 14567 14621 10 11 88
Administrative Support, Inc. F101 BENINCASA, HANNAH LEE 11 SUN DR CARY NC 27513 6223251 14524 14565 8 25 200


You need to create one address variable that contains the values of the three variables Address, City, and Zip. (Because all temporary employees are hired locally, the value of State does not need to be included in the new variable.)


SAS Data Set Hrd.Newtemp
Agency ID Name NewAddress Phone BeginDate EndDate PayRate Days Hours
Administrative Support, Inc. F274 CICHOCK, ELIZABETH MARIE 65 ELM DR, CARY, 27513 6224549 14567 14621 10 11 88
Administrative Support, Inc. F101 BENINCASA, HANNAH LEE 11 SUN DR, CARY, 27513 6223251 14524 14565 8 25 200


Writing the DATA step to create this new variable is easy. You include an assignment statement that contains the concatenation operator, ||, as shown below.
     data hrd.newtemp(drop=address city state zip);
        set hrd.temp;
        NewAddress=address||', '||city||', '||zip;
     run;

The concatenation operator (| |) enables you to concatenate character values. In this assignment statement, the character values of Address, City, and Zip are concatenated with two character constants consisting of a comma and a blank. The commas and blanks are needed to separate the street, city, and ZIP code values. The length of NewAddress is the sum of the length of each variable and constant that is used to create the new variable. Notice that this DATA step drops the original address variables from the new data set.

When the DATA step is executed, you notice that the values of NewAddress do not appear as expected. The values of the new variable contain embedded blanks.


SAS Data Set Hrd.Newtemp
NewAddress
65 ELM DRIVE                    , CARY           , 27513
11 SUN DRIVE                    , CARY           , 27513
712 HARDWICK STREET             , CHAPEL HILL    , 27514
5372 WHITEBUD ROAD              , RALEIGH        , 27612


These blanks appear in the values of NewAddress because the values of the original address variables contained trailing blanks. Whenever the value of a character variable does not match the length of the variable, SAS software pads the value with trailing blanks.


Address
length=32
City
length=15
Zip
length=5
65 ELM DRIVE···················· RALEIGH········ 27612
11 SUN DRIVE···················· DURHAM········· 27612
712 HARTWICK STREET············· CHAPEL HILL···· 27514


Notice that the variable Zip does not contain trailing blanks. So, when the original address values are concatenated to create NewAddress, the trailing blanks in the original values are included in the values of the new variable.


NewAddress
length=60
65 ELM DRIVE····················, RALEIGH········, 27612
11 SUN DRIVE····················, DURHAM·········, 27612
712 HARTWICK STREET·············, CHAPEL HILL····, 27514



|next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement