BookmarkSubscribeRSS Feed
psychopedia
Calcite | Level 5

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!

3 REPLIES 3
Reeza
Super User

Please post sample data and expected output. 

ChrisBrooks
Ammonite | Level 13

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.

PGStats
Opal | Level 21

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;

PG

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1642 views
  • 0 likes
  • 4 in conversation