Search code examples
sastrimstring-length

SAS trim function/ Length statement


Why does this code not need two trim statements, one for first and one for last name? Does the length statement remove blanks?


data work.maillist; set cert.maillist;
length FullName $ 40;
fullname=trim(firstname)||' '||lastname; 
run;

Solution

  • length is a declarative statement and introduces a variable to the Program Data Vector (PDV) with the specific length you specify. When an undeclared variable is used in a formula SAS will assign it a default length depending on the formula or usage context.

    Character variables in SAS have a fixed length and are padded with spaces on the right. That is why the trim(firstname) is needed when || lastname concatenation occurs. If it wasn't, the right padding of firstname would be part of the value in the concatenation operations, and might likely exceed the length of the variable receiving the result.

    There are concatenation functions that can simplify string operations

    • CAT same as using <var>|| operator
    • CATT same as using trim(<var>)||
    • CATS same as using trim(left(<var>))||
    • CATX same as using CATS with a delimiter.
    • STRIP same as trim(left(<var>))

    Your expression could be re-coded as:

    fullname = catx(' ', firstname, lastname);