BookmarkSubscribeRSS Feed
shlomiohana
Obsidian | Level 7

Hello,

 

I have 2 tables:

 

Table A:

Branch_Area
0
1
2
3
220

 

Table B:

Branch_IdArea
1171
2173
3174
4175

 

I want the final table to be a combination of the 2 tables.

The Branch_Area field from table A will appear as it is in the final table, the Area field from table B will be added to the final table according to the following logic:

 

Each record from table A should be searched in the Branch_Id field in table B, If there is a match the value from the Area field should be taken to the final table.

 

If there is no match, enter the same value that we searched, for example, the value 0 or 220.

 

Final table:

AreaBranch_Area
00
1711
1732
1743
220220

 

Thanks a lot.

2 REPLIES 2
ChrisNZ
Tourmaline | Level 20

Like this?

data C;
  merge A(in=A)
        B(in=B rename=(BRANCH_ID=BRANCH_AREA));
  by BRANCH_AREA;
  if A;
  if ^B then AREA=BRANCH_AREA;
run;

Obs Branch_Area Area
1 0 0
2 1 171
3 2 173
4 3 174
5 220 220

 

smantha
Lapis Lazuli | Level 10
data table_a;
input Branch_area;
cards;
0
1
2
3
220
;;;
run;

data table_b;
input Branch_id area;
cards;
1	171
2	173
3	174
4	175
;;;
run;

proc sql;
create table c
as select Branch_Area, coalesce(Area,Branch_area) as Area
from Table_a b
left join table_b a
on a.branch_id = b.branch_area;
quit;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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