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,
|
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
When the DATA step is executed, you notice that the values of
|
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 |
![]() ![]() ![]() ![]() ![]() ![]() |
|
![]() |
![]() |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.