Reputation: 563
data test;
name = 'abcdefgh';
age = 30;
res = name || age;
run;
When I run the above code the variable res holds -> abcdefgh 30
Why the numeric variable age is padding with blanks and then concatenated with the character variable?
Upvotes: 4
Views: 52220
Reputation: 2250
You had asked "why" it is doing it. If you force SAS to not define it as it wants to with the nice methods others have mentioned here, you can run into sorting issues when one variable is defined as a string and another is numeric. Example:
VAR3 = VAR1 || VAR2;
VAR4 = VAR1 || PUT(VAR2,2.);
VAR1 VAR2 VAR3 VAR4
DOG 1 DOG 1 DOG1
DOG 2 DOG 2 DOG2
... ... ... ...
DOG 11 DOG11 DOG11
If you sort on VAR3
you get what you probably want....
If you sort on VAR4
you get DOG1
, DOG11
, DOG2
...
Just my observation. Hope that helps.
Upvotes: 0
Reputation: 1
You can use this as well:
res=name || put(age,3.);
Don't use Strip,Trim etc functions for numeric variables. You will get NOTE in your log window:
NOTE: Numeric values have been converted to character values at the places given by (Line):(Column).
Upvotes: 0
Reputation: 63434
When a number and a character are concatenated, the number is converted to a character variable first, then the two character variables are concatenated together. The default format for converting a numeric variable to a character variable is BEST12. (although that can vary based on the format of your numeric variable). put(30,BEST12.)
would yield ' 30'
which is then concatenated to the character variable.
To avoid this, either use strip as Aaron notes, or do your concatenation using CATS (res=cats(name,age);
) which automatically strips all variables, or put the numeric variable yourself (and with PUT, you can forcibly left-justify it if you want with the -l option).
Upvotes: 8
Reputation: 2036
Try the below code to concatenate the variables with no space.
data test;
name = 'abcdefgh';
age = 30;
res = name || strip(age);
run;
SAS can be quirky. My best guess as to "why" is that SAS is trying to make the numbers look right-justified for text output listings.
Upvotes: 2