BookmarkSubscribeRSS Feed
StephenLaRoche
SAS Employee

I have simple one-to-many merge code like this...

data baseball.playershighestsalary;

  merge baseball.players (in=InMaster) baseball.salaries (in=InSalaries);

  by playerID;

  if InMaster and InSalaries;

  keep playerID birthYear birthMonth birthCountry salary;

run;

Salaries dataset contains a row for each player for each year, so there are multiple rows for most players.

I simply want my merged dataset to include one row per player, including the max salary that player ever received (from the Salaries dataset).

How to do it?

I'm a newbie working on a final project in a SAS programming class, so I appreciate any solution or quick tip.  First time trying to get help on this community!

Thanks,

Steve

4 REPLIES 4
Reeza
Super User

1. Limit dataset before merge to include the max salary or only one record per player

2. I don't think a data step merge can handle a many2many merge so you may want to use SQL

Patrick
Opal | Level 21

Have your Salaries data set sorted by "playerID Salary".

Amend your IF-condition to:   if inMaster and inSalaries and last.playerID;

I wouldn't use "and inSalaries" in your condition as this means you would remove players from your result for which there is no record in "Salaries".

stat_sas
Ammonite | Level 13

proc sql;

create table baseball.playershighestsalary as

select a.playerid,birthYear, birthMonth, birthCountry, b.salary

from baseball.players a left join (select playerid,max(salary) as salary from baseball.salaries group by playerid) b

on a.playerid=b.playerid;

quit;

Ksharp
Super User

If there was a tie for max salary ?

data players;
input id ;
cards;
1 
2
3
;
run;
data salaries;
input id salaries;
cards;
1 23
1 43
2 43
2 21
3 45
4 34
;
run;
proc sort data=players;by id;run;
proc sort data=salaries;by id descending salaries;run;
data playershighestsalary;
  InMaster=0;
  merge players (in=InMaster) salaries ;
  by id;
  if InMaster ;
run;

Xia Keshan

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1162 views
  • 1 like
  • 5 in conversation