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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 832 views
  • 2 likes
  • 3 in conversation