BookmarkSubscribeRSS Feed
jdchang
Fluorite | Level 6

Hi all,

 

I've got data that looks like this:

data one;
input id key value;
datalines;
1 534 2905
2 . 6304
3 872 1108
;
run;

data two;
input id key;
datalines;
. 534
2 298
3 872
;
run;

 

I would like for it to be merged by both id and key such that I get the following results:

data end;
input id key value;
datalines;
1 534 2905
2 298 6304
3 872 1108
;
run;

At the moment I've only be merging by id am not sure of if the merge function can be used conditionally on if id is missing, then merge on key. I'm also not extremely familiar with proc sql, but am willing to use whatever works for the code. Many thanks!

4 REPLIES 4
UdayGuntupalli
Quartz | Level 8

@jdchang ,
   You could join the data set by using the common variables like so: 

 

data merged;
   set one two;
   by key;
run;

  Once you have a merged data set you can filter the data like so: 

data want;

     set merged;

where key ne . ;
Tom
Super User Tom
Super User

How do you know to treat the first observation of the second dataset as if the value of ID as 1?

Tom
Super User Tom
Super User

You might need to post a more complete example of the issues you have.

Your current example is just a merge by ID and collapse values of KEY and ignore values from second dataset that don't match.

data want;
  merge one (in=in1) two (rename=(key=key2));
  by id;
  if in1;
  key=coalesce(key,key2);
  drop key2;
run;
 
Astounding
PROC Star

You probably need to provide more information.  Are these data sets really representative of the problem?  Or do they contain more variables than you are showing?

 

If a variable has a missing value in TWO, you might as well throw out the entire observation.  Even if you could match on the other variable, an observation with a missing value cannot contribute any new information.

 

Could there be more than one observation for a KEY?

 

Could there be more than one observation for an ID?

 

Do the data sets always match up the way you pictured:  first observation with first observation, second observation with second observation, etc.

 

There are ways to handle most of the questions, but more direction is needed.  Otherwise, we are just guessing.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 586 views
  • 0 likes
  • 4 in conversation