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

hi  i have two datasets A and B which could have common variable ID. I want to add a flag "Y" to the dataset A if the same ID existis in dataset B

 

example

 

A         B

 

ID        ID

1          2

2  Y      6      

3          4

4  Y     10

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data A;
input id;
cards;    
1
2
3
4
;
run;

data B;
input id;
cards;
2
6
4
10
;

proc sql;
create table want as
select a.*,ifc(a.id=b.id,'Y',' ') as Flag
from a a left join b b
on a.id=b.id;
quit;

View solution in original post

4 REPLIES 4
yabwon
Onyx | Level 15

Hi,

 

try something like this:

 

data A;
input id;
cards;    
1
2
3
4
;
run;

data B;
input id;
cards;
2
6
4
10
;
run;

data A;
  if 0 then set A;
  declare hash H(dataset:"B(keep=ID)");
  H.DefineKey("ID");
  H.DefineDone();

  do until(eof);
    set A end = eof;
    if H.check()=0 then flag = "Y";
                   else flag = " ";
    output;
  end;
  stop;
run;

All the best

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



Kurt_Bremser
Super User

Another solution: use a format:

data a;
input id $;
cards;
1
2
3
4
;
run;

data b;
input id $;
cards;
2
6
4
10
;
run;

data cntlin;
set b (rename=(id=start)) end=eof;
fmtname = 'lookup';
type = 'C';
label = 'Y';
output;
if eof
then do;
  hlo = 'O';
  start = 'OTHER';
  label = ' ';
  output;
end;
run;

proc format cntlin=cntlin;
run;

data want;
set a;
flag = put(id,$lookup.);
run;
Ksharp
Super User
data A;
input id;
cards;    
1
2
3
4
;
run;

data B;
input id;
cards;
2
6
4
10
;
run;
proc sql;
create table want as
select *,exists(select * from b where id=a.id) as flag
 from a;
quit;
novinosrin
Tourmaline | Level 20
data A;
input id;
cards;    
1
2
3
4
;
run;

data B;
input id;
cards;
2
6
4
10
;

proc sql;
create table want as
select a.*,ifc(a.id=b.id,'Y',' ') as Flag
from a a left join b b
on a.id=b.id;
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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 3874 views
  • 4 likes
  • 5 in conversation