user352188
user352188

Reputation: 53

SAS: retrieve value matching id from another data set

I have one data set with repeated ID's as follows:

## DATASET 1 ##
 ID  val1 val2
 1    2     10
 1    3     9
 ..
 1    4    8
 2    5    3
 ...

I have second one like this:

  ## DATASET 2 ##
  ID  num
  1    48
  2    30
  ...

And I want to be able to look at the ID in dataset 2, take the num from the ID, and repeat it alongside the same ID in dataset 1 like this:

  ## DATASET 1 (new) ##
  ID  val1 val2  num
  1    2     10   48
  1    3     9    48
  ..
  1    4     8    48 
  2    5     3    30
  ...  

What is the easiest way to do this? Thanks!

Upvotes: 0

Views: 405

Answers (1)

Kermit
Kermit

Reputation: 3117

You can join the two tables with a left join

proc sql;
create table want as
select t1.id, t1.val1, t1.val2, t2.num
from one t1 left join two t2
on t1.id = t2.id
;
quit;

or using a data step merge

data want;
merge one two;
by id;
run;

Upvotes: 1

Related Questions