Concatenating a numeric character

data test; name = 'abcdefgh'; age = 30; res = name || age; run; 

When I run the above code, the res variable is saved -> abcdefgh 30

Why is a numeric variable age filled with spaces and then concatenated with a character variable?

+4
source share
4 answers

When a number and a character are concatenated, the number is first converted to a character variable, then the two character variables are combined together. The default format for converting a numeric variable to a character variable is BEST12. (although this may vary depending on the format of your numeric variable). put(30,BEST12.) will give ' 30' , which will then be concatenated with the character variable.

To avoid this, use the strip as Aaron's notes or make your concatenation using CATS ( res=cats(name,age); ), which automatically breaks all the variables or places the digital variable itself (and using PUT you can force it to the left, if you want with the -l option).

+8
source

Try the code below to combine variables without spaces.

 data test; name = 'abcdefgh'; age = 30; res = name || strip(age); run; 

SAS can be fancy. My best guess is that the β€œwhy” is that the SAS is trying to make the numbers look properly valid for the text output.

+2
source

You can also use this:

 res=name || put(age,3.); 

Do not use the Strip, Trim, etc. for numeric variables. In the log window you will receive a NOTE:

NOTE. Numerical values ​​have been converted to the values ​​of characters in the places specified by (Line) :( Column).

0
source

You asked why he does it. If you force the SAS not to define it the way it wants, using the other methods mentioned here, you may run into sorting problems when one variable is defined as a string and the other is numerical. 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 by VAR3 , you get what you probably want .... If you sort by VAR4 , you get DOG1 , DOG11 , DOG2 ...

Just my observation. Hope this helps.

0
source

All Articles