I have 2 datasets, data1 is repeated and data2 is not. I would like to add a flag=1 in data1 if ID is in data2 and flag=0 if not. I have the following code but it is marking all as flag=0, can you please help me troubleshoot?
data dataflagged;
if _N_ = 1 then do;
declare hash CHK(dataset: "data2", multidata:'y');
CHK.definekey("ID");
CHK.definedone();
end;
set data1;
flag = (CHK.check()=1);
run;
quit;
If found, CHK.check()=0.
data dataflagged;
if _N_ = 1 then do;
declare hash CHK(dataset: "data2");
CHK.definekey("ID");
CHK.definedone();
end;
set data1;
flag = CHK.check() = 0;
run;
Are they really equal in both datasets, or just equal once formatted?
It *should* work.
data data1;
do id = 1,1,2,7,9; output; end;
format id 8.;
run;
data data2;
do id = 1,7; output; end;
format id 8.;
run;
data dataflagged;
if _N_ = 1 then do;
declare hash CHK(dataset: "data2");
CHK.definekey("ID");
CHK.definedone();
end;
set data1;
flag = CHK.check() = 0;
run;
proc print noobs; run;
id flag 1 1 1 1 2 0 7 1 9 0
Post the actual code you are using.
This works:
data data1;
do id = "1","1","2","7","9"; output; end;
run;
data data2;
do id = "1","7"; output; end;
run;
data dataflagged;
if _N_ = 1 then do;
declare hash CHK(dataset: "data2");
CHK.definekey("ID");
CHK.definedone();
end;
set data1;
flag = CHK.check() = 0;
run;
proc print noobs; run;
id flag 1 1 1 1 2 0 7 1 9 0
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.