## concatenate two variables

Solved
Occasional Contributor
Posts: 18

# concatenate two variables

I am trying to concatenate two variables(var1 and var2) which are separated by an identifier. Can anyone suggest me how this can be made easy.

var1                                   var2                                                   var3

1. DM / 2. SV                    1. SUBJID / 2. SVDAT                        DM.SUBJID ; SV.SVDAT

1. AE                                 1. SUBJID                                          AE.SUBJID

1. DM / 2. AE / 3. LB          1. SUBJID / 2. DY / 3. TEST              DM.SUBJID ; AE.DY ; LB.TEST

Accepted Solutions
Solution
‎12-21-2015 09:53 AM
Super User
Posts: 8,127

## Re: concatenate two variables

Are the numbers actually in the values?

``````data have ;
infile cards dsd dlm='|' ;
length var1-var2 \$100;
input var1-var2;
cards;
1. DM / 2. SV         |1. SUBJID / 2. SVDAT
1. AE                 |1. SUBJID
1. DM / 2. AE / 3. LB |1. SUBJID / 2. DY / 3. TEST
;

data want ;
set have ;
length var3 \$100;
do i=1 to countw(var1,'/');
var3=catx(';',var3,catx('.',scan(scan(var1,i,'/'),2,'.'),scan(scan(var2,i,'/'),2,'.')));
end;
drop i;
run;``````

All Replies
Solution
‎12-21-2015 09:53 AM
Super User
Posts: 8,127

## Re: concatenate two variables

Are the numbers actually in the values?

``````data have ;
infile cards dsd dlm='|' ;
length var1-var2 \$100;
input var1-var2;
cards;
1. DM / 2. SV         |1. SUBJID / 2. SVDAT
1. AE                 |1. SUBJID
1. DM / 2. AE / 3. LB |1. SUBJID / 2. DY / 3. TEST
;

data want ;
set have ;
length var3 \$100;
do i=1 to countw(var1,'/');
var3=catx(';',var3,catx('.',scan(scan(var1,i,'/'),2,'.'),scan(scan(var2,i,'/'),2,'.')));
end;
drop i;
run;``````
Occasional Contributor
Posts: 18

## Re: concatenate two variables

Yes Tom, The numbers are actually in the values var1 = "1. DM / 2. SV"

Posts: 1,259

## Re: concatenate two variables

Or you could try this:

``````data have;
length var1 var2 \$100;
input var1 1-24
var2 25-60;
cards;
1. DM / 2. SV           1. SUBJID / 2. SVDAT
1. AE                   1. SUBJID
1. DM / 2. AE / 3. LB   1. SUBJID / 2. DY / 3. TEST
;

data  want;
set have;
length c1 c2 \$33
var3 \$200;
do i=2 by 2 until(c1=' ');
c1=scan(var1, i, './');
c2=scan(var2, i, './');
var3=catx(' ; ', var3, catx('.', c1, c2));
end;
drop c: i;
run;

proc print data=want;
run;``````

Please make sure that the lengths of the character variables are sufficient for your data.

That said, I would rather try to change the process which produces character values like "1. DM / 2. AE / 3. LB" and question the usefulness of character values such as "DM.SUBJID ; AE.DY ; LB.TEST".

Super User
Posts: 9,599

## Re: concatenate two variables

Could I ask why?  That looks like SDTM data you are dealing with, how have you managed to get into this situation.  Personally I would want to resolve issues like this far before getting to the stage of this var1/var2 approach.

Occasional Contributor
Posts: 18

## Re: concatenate two variables

That is true. Actually I have a lab data in similar format as Var 1 and Var2, but provided the same format with SDTM variables replacing the lab data.

Super User
Posts: 9,599

## Re: concatenate two variables

Not really following am afraid.  Labs is based off the BDS structure, so long format.  Your example wouldn't fit that type of structure.  So all I can thinkg of is that your doing something metadata orientated, mapping maybe?  If so you may want to look at how metadata is stored.

🔒 This topic is solved and locked.

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

Discussion stats
• 6 replies
• 669 views
• 1 like
• 4 in conversation