SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Search||next

Transforming Data with SAS Functions
Modifying Character Values with Functions


The TRIM function enables you to remove trailing blanks from character values.


General form, TRIM function:
TRIM(argument)

where argument can be any character expression, such as

  • a character variable: trim(address)
  • another character function: trim(left(id))


To remove the blanks from the variable NewAddress, include the TRIM function in your assignment statement to trim the values of Address and City.
     data hrd.newtemp(drop=address city state zip);
        set hrd.temp;
        NewAddress=trim(address)||', '||trim(city)||', '||zip;
     run;

The revised DATA step creates the values that you expect for NewAddress.


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


Points to Remember

Keep in mind that the TRIM function does not affect the way a variable is stored. Suppose you trim the values of a variable and then assign these values to a new variable. The trimmed values are padded with trailing blanks again if the values are shorter than the length of the new variable.

Here's an example. In the DATA step below, the new variable Street is assigned the trimmed value of Address. When the trimmed value is assigned to Street, trailing blanks are added to the value to match the length of 20.

     data temp;
        set hrd.temp;
        length Street $ 20;
        Street=trim(address);
     run;

Address
length=32
Street
length=20
65 ELM DRIVE···················· 65 ELM DRIVE········
11 SUN DRIVE···················· 11 SUN DRIVE········
712 HARTWICK STREET············· 712 HARTWICK STREET·



|next


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

Terms of Use & Legal Information | Privacy Statement