intersecting one dataset based on second dataset in sas

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

intersecting one dataset based on second dataset in sas

I have a dataset called 'primary_data' which looks like this:

Obs  Id     Month   Name    Salary
1   10006   JAN1926 name1   3273
2   10006   FEB1926 name1   7143
3   10007   JAN1926 name2   4274
4   10008   JAN1926 name3   2591
5   10008   FEB1926 name3   2394
6   10009   JAN1926 name4   4416
.
.

And I have another dataset called 'selection' which looks like this:

Obs Id
1   10006
2   10008
.
.

Note: Ids are unique in 'selection' dataset

I need to take a subset of primary_data where the Ids are taken from 'selection' dataset. I tried merge but I am getting the whole primary_data as my output.

data merged_data;
    merge primary_data selection;
    by Id;
run;

In my example above, I would like merged_data to have entries corresponding to Ids: 10006 and 10008 but not from Ids 10007 and 10009. That is, merged_data should look like:

Obs  Id     Month   Name    Salary
1   10006   JAN1926 name1   3273
2   10006   FEB1926 name1   7143
3   10008   JAN1926 name3   2591
4   10008   FEB1926 name3   2394
.
.

I do not understand, what am I doing wrong.


Accepted Solutions
Solution
‎08-17-2016 10:20 AM
Occasional Contributor
Posts: 13

Re: intersecting one dataset based on second dataset in sas

Hi nikhil,

 

Try the following additions to your code:

data merged_data;
    merge primary_data (in=a) selection (in=b);
    by Id;
if b; run;

 

Hope this works!

Kenny

View solution in original post


All Replies
Solution
‎08-17-2016 10:20 AM
Occasional Contributor
Posts: 13

Re: intersecting one dataset based on second dataset in sas

Hi nikhil,

 

Try the following additions to your code:

data merged_data;
    merge primary_data (in=a) selection (in=b);
    by Id;
if b; run;

 

Hope this works!

Kenny

New Contributor
Posts: 3

Re: intersecting one dataset based on second dataset in sas

[ Edited ]

Thanks kenny, this works perfectly.

Esteemed Advisor
Esteemed Advisor
Posts: 7,211

Re: intersecting one dataset based on second dataset in sas

Subquery:

proc sql;
  select *
  from   PRIMARY_DATA
  where  ID in (select ID from SELECTION);
quit;
New Contributor
Posts: 3

Re: intersecting one dataset based on second dataset in sas

Thanks KW9, it gives the data as desired.
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 336 views
  • 2 likes
  • 3 in conversation