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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 479 views
  • 7 likes
  • 5 in conversation