13

I have a table that has two numeric values called year and month. I would like to create a new table that has one value called ym which is just the concatenation of year and month. Here is an example:

proc sql;
create table test as 
select CONCAT(year, month) as ym from tbl;
run;

What is the CONCAT function that goes there?

Joe
  • 62,789
  • 6
  • 49
  • 67
Alex
  • 19,533
  • 37
  • 126
  • 195

2 Answers2

33

CAT, CATS, CATT, CATX all perform concatenation, as long as you're on 9.1.3 or later (and CATQ on 9.2 or later); CAT does basic concatenation, CATS concatenates and strips spaces, CATT trims, and CATX concatenates with a delimiter.

Typically CATS is the correct function to use for numbers, since by default numbers are put into a format with spaces (BEST12., so "3 " is 3).

proc sql;
create table test as 
select CATS(year, month) as ym from tbl;
run;
Joe
  • 62,789
  • 6
  • 49
  • 67
0

As another option, you can concatenate with the || operator like X1||X2 or even multiples at once like X1||X2||X3||X4

You can add trim or left like TRIM(LEFT(X1))||TRIM(LEFT(X2))

You can add delimeters like TRIM(X1)||','||TRIM(X2)

FullStack
  • 5,902
  • 4
  • 43
  • 77
  • I get this: "ERROR: Concatenation (||) requires character operands." TRIM also seems to want characters to begin with. – blakeoft Jan 18 '19 at 19:22