BookmarkSubscribeRSS Feed
lulu3
Obsidian | Level 7

I want to select all the information from data one based on the data two id.

data one;
input id n;
datalines;
101 a
101 b
101 c
101 d
102 a
102 c
103 f
104 f
105 f
105 u
;
run;

data two;
input id;
datalines;
101
104
105
;
run;

 

want:

101 a

101 b

101 c

104 f

105 f

105 u

 

Thanks!

4 REPLIES 4
Patrick
Opal | Level 21

What have you tried already?

Either use a SQL inner join or a data step merge with the IN keyword.  

 

merge A (in=ina) B;

by variable;

if ina;

...

ballardw
Super User

And is the

101 d

missing from your Want data set a typo? If not a type then you will need to describe a rule for identifying that the record should be excluded.

novinosrin
Tourmaline | Level 20
data one;
input id n $;
datalines;
101 a
101 b
101 c
101 d
102 a
102 c
103 f
104 f
105 f
105 u
;
run;

data two;
input id;
datalines;
101
104
105
;
run;

proc sql;
create table want as
select *
from one
where id in (select id from two);
quit;
s_lassen
Meteorite | Level 14

You have two good solutions suggested: the datastep by @Patrick, and the SQL by @novinosrin. What you should chose depends:

  1. If your data TWO table only contains unique values of ID, the datastep is the fastest. This solution also assumes that both tables are sorted.
  2. If there are multiple rows with the same ID in TWO, the SQL solution immediately gives the right solution, and may be simpler to implement. And the data does not need to be sorted. If data ONE is very large and not sorted, the SQL solution may actually be faster than the datastep (which also needs time for sorting the large table).

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!

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