Reputation: 237
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
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:
LENGTH
or ATTRIBUTE
statement,SET
, MERGE
, UPDATE
or MODIFY
statement,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