DATA Step, Macro, Functions and more

concatenate two variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

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.

 

Thaks in advance


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

Re: concatenate two variables

Posted in reply to gowtham112

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!

View solution in original post


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

Re: concatenate two variables

Posted in reply to gowtham112

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: 252

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: 11,343

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

 

PROC Star
Posts: 768

Re: concatenate two variables

Posted in reply to gowtham112

Like this? Smiley Happy

 

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

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

Re: concatenate two variables

Posted in reply to gowtham112

Same solution as @LaurieF


Cheers from India!

Manjeet
Super Contributor
Posts: 252

Re: concatenate two variables

I beat @draycut by seconds, i think! Smiley Tongue

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 1281 views
  • 4 likes
  • 5 in conversation