hyg17
hyg17

Reputation: 237

SAS Some question regarding the concatenation operator ||

I have a question regarding the following program in SAS.

 data staff;
      jobA='FA';
      jobB='1';
      jobA=jobA||jobB;
 run;

 proc print;

 run;

why does the output of jobA still FA?

I am thinking that it should be updated, but for some reason it stays the same... Does it have to do anything with jobA's original length when it was first defined?

Upvotes: 2

Views: 146

Answers (1)

Richard
Richard

Reputation: 27508

DATA Step character variables are different than string variables in other coding languages. Every DATA Step variable has a fixed length. Variable lengths can be:

  • explicitly declared (before first use) with a LENGTH or ATTRIBUTE statement,
  • 'inherited' from a contributing data set via SET, MERGE, UPDATE or MODIFY statement,
  • or are inferred contextually from their first use in the data step program.

As Chris J correctly answered in his comment, jobA will be length $2 because it's first use is contextual as 'FA' which has two characters. Likewise, jobB will be length $1.

For your specific question, the concatenation result is 3 characters long and being stored in a 2 character variable. A silent (no log messages) truncation occurs when the result is stored in the variable.

All character variables are implicitly tail filled with spaces out to their length, which is an important consideration when concatenating -- sometimes when concatenating you will scratch your head and ask, where is the rest of the result?, or why are there so many spaces before the subsequent parts?

The concatenation operator (||) does not trim trailing the spaces when combining values. This can cause confusion when a target variable is not long enough to contain the lengths of all variables being combined -- the concatenation will appear to have 'not happened'. In the grand old days the coding pattern trim(left(variable-1))||trim(left(variable-2))||..||trim(left(variable-n)) was used to avoid that situation.

Todays SAS has a family of concatenation functions (cat, cats, catt, catx, catq) which make combining character expressions a bit easier.

Consider a read of SAS documentation Step-by-Step Programming with Base SAS, Combining Character Values: Using Concatenation

Upvotes: 4

Related Questions