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.
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
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
Thanks kenny, this works perfectly.
Subquery:
proc sql; select * from PRIMARY_DATA where ID in (select ID from SELECTION); quit;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.