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

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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