## concatenate two variables

Solved
Occasional Contributor
Posts: 13

# concatenate two variables

Hi guys,

I am trying to concatenate two variables(var1 and var2) . Can anyone suggest me how this can be done

var1  var2           Var3

Q1     Q3            Q1,Q3

The final output should be like var3.

Accepted Solutions
Solution
‎01-08-2017 11:34 PM
Super Contributor
Posts: 268

## Re: concatenate two variables

Fortunately it's the simplest thing in the world. Well, not the simplest thing. But still fairly simple.

``````data _null_;
var1 = 'Q1';
var2 = 'Q3';
length var3 \$ 5;
var3 = catx(',', var1, var2);
put _all_;
run;``````

catx takes as its first parameter the separator you want, followed by the variables (and there can be many more) you want to concatenate.

It's magic!

All Replies
Solution
‎01-08-2017 11:34 PM
Super Contributor
Posts: 268

## Re: concatenate two variables

Fortunately it's the simplest thing in the world. Well, not the simplest thing. But still fairly simple.

``````data _null_;
var1 = 'Q1';
var2 = 'Q3';
length var3 \$ 5;
var3 = catx(',', var1, var2);
put _all_;
run;``````

catx takes as its first parameter the separator you want, followed by the variables (and there can be many more) you want to concatenate.

It's magic!

Super Contributor
Posts: 268

## Re: concatenate two variables

I'd just like to add the the variables that are being concatenated are stripped (leading/trailing spaces taken off). In this case it doesn't make any difference, of course - just so that you know.
Super User
Posts: 13,542

## Re: concatenate two variables

As a minor clarification to @LaurieF's fine solution, the LENGTH of the new variable is important and needs to be set to the sum of the lengths of the longest values of Var1 and Var2 plus 1 for the comma. Otherwise you may experience truncation of your values. Note that CATX may tell that you need a longer variable with a message.

```data _null_;
var1 = 'Q123';
var2 = 'Q345';
length var3 \$ 8;
var3 = catx(',', var1, var2);
put _all_;
run;

```

Will generate:

```WARNING: In a call to the CATX function, the buffer allocated for the result was not long enough
to contain the concatenation of all the arguments. The correct result would contain 9
characters, but the actual result may either be truncated to 8 character(s) or be
completely blank, depending on the calling environment. The following note indicates the
left-most argument that caused truncation.
```

which tells the minimum length you should set for VAR3

New Contributor
Posts: 4

## Re: concatenate two variables

Perfect! Thank you LaurieF
PROC Star
Posts: 1,283

## Re: concatenate two variables

Like this?

``````data have;
input var1 \$ var2 \$;
datalines;
Q1 Q3
;

data want;
set have;
var3 = catx(',', var1, var2);
run; ``````
Frequent Contributor
Posts: 79

## Re: concatenate two variables

Same solution as @LaurieF

Cheers from India!

Manjeet
Super Contributor
Posts: 268

## Re: concatenate two variables

I beat @draycut by seconds, i think!

☑ This topic is solved.