BookmarkSubscribeRSS Feed
KPCklebspn
Obsidian | Level 7

Hi all, I have 2 tables

 

tableA

ID   var1

1      a

2      b

3      c

 

tableB

ID     var1

1         b

4         c

 

I want to flag the ID in tableA if the ID is in tableB

 

want

ID   var1    flag

1      a          1

2      b         0

3      c         0

 

 

Is there  a way to do this with proc sql?

 

proc sql;
create table as want as
select * from tableA
**not sure what to put here***
quit;

4 REPLIES 4
novinosrin
Tourmaline | Level 20

data a;
input ID   var1	$;
cards;
1      a

2      b

3      c
;

data b;
input ID   var1	$;
cards;

1         b

4         c
;

proc sql;
create table want  as
select a.* ,a.id=b.id as flag
from a a left join b b
on a.id=b.id;
quit;
smantha
Lapis Lazuli | Level 10
Proc sql;
Create table x (drop = id2)
As select *, case when id=id2 then 1
Else .
End as flag
From
(Select a.*, b.id as id2 from a left join b
On a.id = b. Id)
Quit;
r_behata
Barite | Level 11
data tablea;
input id var1 $;
cards;
1 a
2 b
3 c
;
run;

data tableb;
input id var1 $;
cards;
1 b
4 c
;
run;

proc sql;
create table want as
select a.*,
	 case when b.id then 1
	 else 0
	 end as flag
from tablea a
left join tableb b
on a.id=b.id;
quit;
Ksharp
Super User
data a;
input ID   var1	$;
cards;
1      a
2      b
3      c
;

data b;
input ID   var1	$;
cards;
1         b
4         c
;

proc sql;
create table want  as
select a.* ,exists(select * from b where id=a.id) as flag
from a ;
quit;

sas-innovate-white.png

🚨 Early Bird Rate Extended!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Lock in the best rate now before the price increases on April 1.

Register now!

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
  • 4 replies
  • 2791 views
  • 0 likes
  • 5 in conversation