BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
thummala
Obsidian | Level 7

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

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

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;
thummala
Obsidian | Level 7

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

FreelanceReinh
Jade | Level 19

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".

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.  

thummala
Obsidian | Level 7

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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