Hi: I am processing baseball dataset. I want to count the winning streak of a team winning. I created a variable called win, if team A wins it is 1, else it is 0. I want to create a variable called winstreak, if team A wins 1 time, it is 1, if team A wins 2 in a row, it is 2, Once team A loses it goes back to 0 again. So far I have tried:
data phi2002;
set phi2002;
if (VisitingTeam = "PHI" and VisitorRunsScored > HomeRunsScore)
then PHIWIN = 1;
else if (HomeTeam = "PHI" and HomeRunsScore > VisitorRunsScored)
then PHIWIN = 1;
else PHIWIN = 0;
retain winstreak;
if PHIWIN = 1 then winstreak =+1;
else winstreak = 0;
run;
The code doesnt work, since winstreak only shows the same value as PHIWIN. Please help!
Please post sample data and expected output.
Your problem is with the line
if PHIWIN = 1 then winstreak =+1;
What you're doing here is setting the value of winstreak to 1 instead of incrementing it. if you make it
if PHIWIN = 1 then winstreak+1;
I think you'll find you get the correct answer.
Winning and loosing streaks in 2010:
data bb0;
infile "&sasforum\datasets\GL2010.txt" dsd;
input date :$8. gameNumber :$1. day :$3.
teamV :$3. leagueV :$2. gameV
teamH :$3. leagueH :$2. gameH
scoreV scoreH;
run;
data bb;
set bb0;
length game $9 team $7;
game = cats(date, gameNumber);
team = catx("-", teamV, leagueV);
location = "V";
teamScore = scoreV; opponentScore = scoreH;
win = teamScore > opponentScore;
output;
team = catx("-", teamH, leagueH);
location = "H";
teamScore = scoreH; opponentScore = scoreV;
win = teamScore > opponentScore;
output;
keep game team location win teamScore opponentScore;
run;
proc sort data=bb; by team game; run;
data streaks;
streak = 0;
do until (last.win);
set bb; by team win notsorted;
streak + 1;
end;
keep team win streak;
run;
proc format;
value win
0 = "Loosing"
1 = "Winning"
;
run;
proc sql;
create table teamStreaks as
select team, win format=win., max(streak) as longestStreak
from streaks
group by team, win;
quit;
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!
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.