<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Loop through each observation and count previous occurrences of values in dataset based on current observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138908#M28043</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks again for the reply all. I think this approach would only get me the last x home games and last x away games for each teams, however, because teams don't always play home, away, home I wouldn't be capturing the right data in some instances for example if a team plays 2 home games in a row. I will definitely go with this approach however, if there is no alternative.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 10 Mar 2014 20:35:15 GMT</pubDate>
    <dc:creator>cxkev182</dc:creator>
    <dc:date>2014-03-10T20:35:15Z</dc:date>
    <item>
      <title>Loop through each observation and count previous occurrences of values in dataset based on current observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138900#M28035</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Good Evening,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am required to do some analysis of Football (Soccer) teams. In a dataset of match history (sorted ascending by date), I have the following fields:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;HomeTeam&lt;/P&gt;&lt;P&gt;AwayTeam&lt;/P&gt;&lt;P&gt;FTR&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;FTR being the full time result and can take the value of "H" (Home Win) "D" (Draw) or "A" (Away Win)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For each match, I need to count the number of Wins/Draws/Losses for each teams last 36 games. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So for instance, say an observation in the dataset is Manchester United v Arsenal, I want to count number of Win/Draw/Losses for Manchester United in their previous 36 games (consisting of both their home &amp;amp; away games) and also the same for Arsenal.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So the output would look like the following (sample data used):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;HomeTeam&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AwayTeam&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FTR&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; H_Form_W&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; H_Form_D&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Home_Form_L&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A_Form_W&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A_Form_D&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A_Form_L&lt;/P&gt;&lt;P&gt;Man UTD&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Arsenal&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; H&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 25&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 19&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This would seemingly be easy if only home games were needed for the home team and away games for the away team, however, I suspect a more complex macro is needed to handle the fact that when querying the dataset, the object we are looking for can appear in both the home &amp;amp; away team field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Many Thanks for any help,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;cxkev182&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Mar 2014 18:20:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138900#M28035</guid>
      <dc:creator>cxkev182</dc:creator>
      <dc:date>2014-03-10T18:20:40Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through each observation and count previous occurrences of values in dataset based on current observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138901#M28036</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The 'brute force' method would be to use the 'point' option on the set statement to loop back through the file identifying whether the team in question was in the match, recording the result, and stopping once you have reached 36 matches or the start of the file. No macros required.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Mar 2014 18:50:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138901#M28036</guid>
      <dc:creator>BruceBrad</dc:creator>
      <dc:date>2014-03-10T18:50:08Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through each observation and count previous occurrences of values in dataset based on current observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138902#M28037</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Reasonably simple solutions should be possible.&amp;nbsp; Is there an additional variable in your data set that tells you the date of the match?&amp;nbsp; How do you know that the observations are in order?&amp;nbsp; Start there, and I'll be able to post something later this afternoon.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Mar 2014 19:23:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138902#M28037</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2014-03-10T19:23:33Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through each observation and count previous occurrences of values in dataset based on current observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138903#M28038</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Astounding, yes sorry there is a variable simply called 'Date', It's from this that I know and can keep the dataset in order. Look forward to any help you can give.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Mar 2014 19:44:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138903#M28038</guid>
      <dc:creator>cxkev182</dc:creator>
      <dc:date>2014-03-10T19:44:02Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through each observation and count previous occurrences of values in dataset based on current observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138904#M28039</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;BruceBrad, first of all, thanks very much for the reply. I am not familiar with the point option. I have googled to get some examples but can't seem to get my head round it. Could I ask for a sample piece of code how I could code this?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks again for your help, I really appreciate it.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Mar 2014 19:47:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138904#M28039</guid>
      <dc:creator>cxkev182</dc:creator>
      <dc:date>2014-03-10T19:47:34Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through each observation and count previous occurrences of values in dataset based on current observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138905#M28040</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Have you considered changing your data structure?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Team&amp;nbsp; Location FTR form_w form_d form_l&lt;/P&gt;&lt;P&gt;Manchester Away H 25 5 6&lt;/P&gt;&lt;P&gt;Arsenal Home AL 19 6 10&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think this may be more conducive to data analysis.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Mar 2014 19:53:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138905#M28040</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2014-03-10T19:53:29Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through each observation and count previous occurrences of values in dataset based on current observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138906#M28041</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Actually, I think Reeza's approach is the neatest way to go.&lt;/P&gt;&lt;P&gt;Step 1. Create two records from each of your records - one for each team as Reeza suggests&lt;/P&gt;&lt;P&gt;Step 2. Sort by date within team, and create a variable COUNT that counts the number of times the team has competed. That is, starting at 1 for the first occurence of the team etc.&lt;/P&gt;&lt;P&gt;Step 3. Create a dataset which only has the last record for each team.&lt;/P&gt;&lt;P&gt;Step 3. Merge this dataset with the step 2 dataset, selecting records where count is greater than the current value of count minus 16. &lt;/P&gt;&lt;P&gt;Step 4. Summarise this merged data, counting the number of wins etc. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Mar 2014 20:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138906#M28041</guid>
      <dc:creator>BruceBrad</dc:creator>
      <dc:date>2014-03-10T20:11:10Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through each observation and count previous occurrences of values in dataset based on current observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138907#M28042</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Bruce,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This sounds about right, and was along the lines of what I was thinking as well.&amp;nbsp; One suggestion:&amp;nbsp; If you sort within team by DESCENDING date, you can accomplish the rest in a single additional DATA step.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Mar 2014 20:32:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138907#M28042</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2014-03-10T20:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through each observation and count previous occurrences of values in dataset based on current observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138908#M28043</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks again for the reply all. I think this approach would only get me the last x home games and last x away games for each teams, however, because teams don't always play home, away, home I wouldn't be capturing the right data in some instances for example if a team plays 2 home games in a row. I will definitely go with this approach however, if there is no alternative.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Mar 2014 20:35:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138908#M28043</guid>
      <dc:creator>cxkev182</dc:creator>
      <dc:date>2014-03-10T20:35:15Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through each observation and count previous occurrences of values in dataset based on current observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138909#M28044</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Assuming you need the last 36 games, no matter whether they are home or away:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data split;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; team = HomeTeam;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if FTR='H' them result='W';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; else if FTR='A' then result='L';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; else result='D';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; team = AwayTeam;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if FTR='A' then result='W';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; else if FTR = 'H' then result='L';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; else result='D';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; output;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; keep team date result;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sort data=split;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; by team descending date;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set split;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; by team;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if first.team then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wins = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; losses = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; draws = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if wins + losses + draws &amp;lt; 36 then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if result='W' then wins + 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else if result='L' then losses + 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else draws + 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; if last.team;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does this meet all the requirements?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Mar 2014 20:52:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138909#M28044</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2014-03-10T20:52:00Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through each observation and count previous occurrences of values in dataset based on current observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138910#M28045</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks to everyone for the help as you have all contributed. I wish I could give you all the correct answer as Reeza had the initial idea of structuring the data in this way, Bruce elaborated further on it, but I have set the correct answer to Astounding for taking the ideas and putting them into code for me.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Many thanks to all.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Mar 2014 21:36:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-each-observation-and-count-previous-occurrences-of/m-p/138910#M28045</guid>
      <dc:creator>cxkev182</dc:creator>
      <dc:date>2014-03-10T21:36:42Z</dc:date>
    </item>
  </channel>
</rss>

