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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
kewong
Obsidian | Level 7

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

4 REPLIES 4
kewong
Obsidian | Level 7

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

nikhil141088
Fluorite | Level 6

Thanks kenny, this works perfectly.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Subquery:

proc sql;
  select *
  from   PRIMARY_DATA
  where  ID in (select ID from SELECTION);
quit;
nikhil141088
Fluorite | Level 6
Thanks KW9, it gives the data as desired.

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
  • 4 replies
  • 1668 views
  • 2 likes
  • 3 in conversation