## 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;
run;``````

Super User
Posts: 23,724

## Re: SAS dataset count winning streak on baseball

Please post sample data and expected output.

Valued Guide
Posts: 593

## 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.

Posts: 5,529

## 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;
run;

data bb;
set bb0;
length game \$9 team \$7;
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;

``````
PG
Discussion stats
• 3 replies
• 357 views
• 0 likes
• 4 in conversation