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
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
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".
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;
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
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.
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.