BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
mshr
Calcite | Level 5

Hi,

I'm trying to join 2 tables based on a concatenated field (var1) :

Thank you

Table A:

IDvar1
1a, b, d
2c
3d, a

  Table B:

var1var2
aJohn
bSarah
cTralala
dMinnie

Result: 

IDvar 3
1John, Sarah, Minnie
2Tralala
3Minnie,  John
1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Amethyst | Level 16

"The join you want can't be done in one step." ?!?! Hold my beer...

data Result_in_one_step;
  if 0 then set Table_B;
  declare hash H(dataset: "Table_B");
  H.defineKey("code");
  H.defineData("data");
  H.defineDone();

  do until (EOF);
    set Table_A end=EOF;

    length code $ 1 var3 $ 128;
    call missing(var3);
    do i = 1 to countw(var1,", ");
      code=scan(var1,i,", ");
      if 0=H.find() then var3 = catx(", ", var3, data);
    end;

    output; 
  end;

  stop;
  keep ID var3;
run;

title "Result_in_one_step";
proc print data=Result_in_one_step;
run;
title;

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

3 REPLIES 3
yabwon
Amethyst | Level 16

This is a step by step solution:

title "Table_A";
data Table_A;
input ID	var1 $20.;
cards;
1	a, b, d
2	c
3	d, a
;
run;
proc print;
run;

title "Table_B";
data Table_B;
input code $1. data $20.; /* variables names changed */
cards;
a	John
b	Sarah
c	Tralala
d	Minnie
;
run;
proc print;
run;


/* step 1 */
data splited;
  set Table_A;

  length code $ 1;
  do i = 1 to countw(var1,", ");
    code=scan(var1,i,", ");
    output;
  end;
drop var1;
run;

/* step 2 */
proc sql;
  create table combined as
  select s.ID, b.data
  from splited as s
  left join
  Table_B as b
  on s.code=b.code
  order by s.ID
  ;
quit;

/* step 3 */
proc transpose data=combined out=transposed;
  by ID;
  var data;
run;

/* step 4 */
title "Result";
data Result;
  set transposed;
  array C[*] col:;

  length var3 $ 128;
  var3 = catx(", ", of C[*]);
  keep ID var3;
run;
proc print data=Result;
run;
title;

The join you want can't be done in one step.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Amethyst | Level 16

"The join you want can't be done in one step." ?!?! Hold my beer...

data Result_in_one_step;
  if 0 then set Table_B;
  declare hash H(dataset: "Table_B");
  H.defineKey("code");
  H.defineData("data");
  H.defineDone();

  do until (EOF);
    set Table_A end=EOF;

    length code $ 1 var3 $ 128;
    call missing(var3);
    do i = 1 to countw(var1,", ");
      code=scan(var1,i,", ");
      if 0=H.find() then var3 = catx(", ", var3, data);
    end;

    output; 
  end;

  stop;
  keep ID var3;
run;

title "Result_in_one_step";
proc print data=Result_in_one_step;
run;
title;

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



mshr
Calcite | Level 5

your solution works great! 

many thanks

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 828 views
  • 0 likes
  • 2 in conversation