SAS dataset count winning streak on baseball

Occasional Contributor
Posts: 6

SAS dataset count winning streak on baseball

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;

The code doesnt work, since winstreak only shows the same value as PHIWIN. Please help!

Grand Advisor
Posts: 17,360

Re: SAS dataset count winning streak on baseball

Please post sample data and expected output. 

Frequent Contributor
Posts: 124

Re: SAS dataset count winning streak on baseball

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.

Respected Advisor
Posts: 4,608

Re: SAS dataset count winning streak on baseball

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;

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;
team = catx("-", teamH, leagueH);
location = "H";
teamScore = scoreH; opponentScore = scoreV;
win = teamScore > opponentScore;
keep game team location win teamScore opponentScore;

proc sort data=bb; by team game; run;

data streaks;
streak = 0;
do until (;
    set bb; by team win notsorted;
    streak + 1;
keep team win streak;

proc format;
value win
0 = "Loosing"
1 = "Winning"

proc sql;
create table teamStreaks as
select team, win format=win., max(streak) as longestStreak
from streaks
group by team, win;

Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation