BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Lapis Lazuli | Level 10

Hi guys, 

suppose to have the following two datasets: 

 

data DB;
  input ID $20. Codes Index_1;
cards;
0001 2134  1
0001 87    0
0002 154   1 
0002 65    0
0003 88    1
0003 34    0
0003 78    0
run;


data DB1;
  input Codes Index_2;
cards;
2134  6
87    4
154   2 
65    6
88    1
34    3
78    5
run;

Is there a way to add Index_2 from DB1 to DB only when Index_1 from DB is  = 1 to get the following? 

 


data DB2;
  input ID $20. Codes Index_1 Index_2;
cards;
0001 2134  1    6
0001 87    0    0
0002 154   1    2
0002 65    0    0
0003 88    1    1
0003 34    0    0
0003 78    0    0
run;

Thank you in advance

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
data db2;
set db;
if _n_ = 1
then do;
  length index_2 8;
  declare hash db1 (dataset:"db1");
  db1.definekey("codes");
  db1.definedata("index_2");
  db1.definedone();
end;
if index_1
then rc = db1.find();
else index_2 = 0;
drop rc;
run;

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User
data db2;
set db;
if _n_ = 1
then do;
  length index_2 8;
  declare hash db1 (dataset:"db1");
  db1.definekey("codes");
  db1.definedata("index_2");
  db1.definedone();
end;
if index_1
then rc = db1.find();
else index_2 = 0;
drop rc;
run;