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

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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
  • 2857 views
  • 0 likes
  • 5 in conversation