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

Hello,

 

I have two tables, I would like to complete the first with a new variable. If "Name" is present in the second table then Pres = 1 otherwise 0.

 

Table 1  Table 2  
      
NameFonction NameAgenceDesk
WilliamA WilliamerA2
MathiasB FabienqsA3
NelsonY LuisedfA4
   RoseghB25
   MathiasteB26
   SebastienjkC15
   JonathanlmC16
   ArthurnbB29
   JulienazB30
      
      
I want to get to that:    
      
      
NameFonctionPres   
WilliamA1   
MathiasB1   
NelsonY0   

 

 

Thanks for your help

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Straightforward with SQL:

proc sql;
create table want1 as
select
  a.name,
  a.function,
  case
    when name in (select b.name from table_2 b)
    then 1
    else 0
  end as pres
from table_1 a;
quit;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20
data Table1;
input Name :$20. Fonction $1.;
datalines;
William A
Mathias B
Nelson Y
;
   
data Table2;
input Name :$20. Agence $ Desk $;
datalines;
William er A2
Fabien qs A3
Luise df A4
Rose gh B25
Mathias te B26
Sebastien jk C15
Jonathan lm C16
Arthur nb B29
Julien az B30
;

data want;
    if _N_=1 then do;
        declare hash h(dataset:'Table2');
        h.definekey('Name');
        h.definedone();
    end;

    set Table1;

    Pres=ifn(h.check(), 0, 1);
run;
Kurt_Bremser
Super User

Straightforward with SQL:

proc sql;
create table want1 as
select
  a.name,
  a.function,
  case
    when name in (select b.name from table_2 b)
    then 1
    else 0
  end as pres
from table_1 a;
quit;
novinosrin
Tourmaline | Level 20

Hello @WilliamB  Another way, is to use boolean expressions when want in 1,0s  a method heavily used , taught and emphasized by @SASKiwi  in many of his briiliant posts

 

For example,

 


data Table1;
input Name :$20. Fonction $1.;
datalines;
William A
Mathias B
Nelson Y
;
   
data Table2;
input Name :$20. Agence $ Desk $;
datalines;
William er A2
Fabien qs A3
Luise df A4
Rose gh B25
Mathias te B26
Sebastien jk C15
Jonathan lm C16
Arthur nb B29
Julien az B30
;

proc sql;
create table want as
select a.*,a.name=b.name as Pres
from table1 a left join table2 b
on a.name=b.name;
quit;

Since it appears your name values are unique in both tables, m*n product combo in a join is not a worry and makes it rather robust and easy 

SwissC
Obsidian | Level 7
PROC SQL;
  CREATE TABLE want AS SELECT
  a.*, 
CASE
WHEN b.pres1=1 THEN 1
ELSE 0
END as pres FROM have1 a LEFT JOIN (select distinct name, 1 as pres1 from have2) b ON a.name=b.name; QUIT;
Jagadishkatam
Amethyst | Level 16

Alternatively with merge statement

 

 

data Table1;
input Name :$20. Fonction $1.;
datalines;
William A
Mathias B
Nelson Y
;
   
data Table2;
input Name :$20. Agence $ Desk $;
datalines;
William er A2
Fabien qs A3
Luise df A4
Rose gh B25
Mathias te B26
Sebastien jk C15
Jonathan lm C16
Arthur nb B29
Julien az B30
;

proc sort data=table1;
by name;
run;

proc sort data=table2;
by name;
run;

data want;
merge Table1(in=a) Table2(in=b keep=name);
by name ;
if a;
if a and b then pres=1;
else pres=0;
run;
Thanks,
Jag

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 5 replies
  • 1386 views
  • 1 like
  • 6 in conversation