<?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 RETAIN statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-statement/m-p/373370#M89320</link>
    <description>&lt;P&gt;Hello there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me try this one more time:)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I couldn't come up with a relatively simple counting logic and was hoping somebody can help me:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the sample table created by the code below (which includes what I want to achive as "Desired_result") I would like to create a &lt;U&gt;counter&lt;/U&gt; variable that starts to count teams within each "event". The only trick is, the code should start counting from 1 when a team within the event is a "special team".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For instance in the event number 6 (i.e. row 7 to 10), the "Desired_result" gets a value of 1 starting at "row_number" 9 because within that particular "event", team C (in row_number 7) and team A (row_number &lt;span class="lia-unicode-emoji" title=":smiling_face_with_sunglasses:"&gt;😎&lt;/span&gt; are &lt;U&gt;not&lt;/U&gt; special teams thus the counter does not start to count from 1 until "row_number" 9. Then because it starts to count at "row_number" 9 it keeps counting after even though the team C at "row_number" 10 is not a special_team. After row_number 10 the counter starts from 0 again because a new "event" starts at "row_number" 11.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;input Row_number Event Team $ Special_team $ Desired_result;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 A No 0&lt;BR /&gt;2 2 B Yes 1&lt;BR /&gt;3 3 C No 0&lt;BR /&gt;4 4 B Yes 1&lt;BR /&gt;5 4 A No 2&lt;BR /&gt;6 5 A No 0&lt;BR /&gt;7 6 C No 0&lt;BR /&gt;8 6 A No 0&lt;BR /&gt;9 6 D Yes 1&lt;BR /&gt;10 6 C No 2&lt;/P&gt;&lt;P&gt;11 7 A No 0&lt;/P&gt;&lt;P&gt;12 7 C No 0&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm pretty sure I need to use a combination of &lt;STRONG&gt;first.event&lt;/STRONG&gt; and &lt;STRONG&gt;RETAIN&amp;nbsp;&lt;/STRONG&gt;statements together but I couldn't find a solution yet. The code below did not work for me:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data test1;&lt;BR /&gt;set test;&lt;BR /&gt;by event;&lt;/P&gt;&lt;P&gt;retain counter;&lt;BR /&gt;if first.event then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if special_team='Yes' then counter=1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;else if special_team='No' then counter=0;&lt;BR /&gt;else counter+1;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;</description>
    <pubDate>Wed, 05 Jul 2017 17:37:01 GMT</pubDate>
    <dc:creator>Recep</dc:creator>
    <dc:date>2017-07-05T17:37:01Z</dc:date>
    <item>
      <title>RETAIN statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-statement/m-p/373370#M89320</link>
      <description>&lt;P&gt;Hello there,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let me try this one more time:)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I couldn't come up with a relatively simple counting logic and was hoping somebody can help me:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the sample table created by the code below (which includes what I want to achive as "Desired_result") I would like to create a &lt;U&gt;counter&lt;/U&gt; variable that starts to count teams within each "event". The only trick is, the code should start counting from 1 when a team within the event is a "special team".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For instance in the event number 6 (i.e. row 7 to 10), the "Desired_result" gets a value of 1 starting at "row_number" 9 because within that particular "event", team C (in row_number 7) and team A (row_number &lt;span class="lia-unicode-emoji" title=":smiling_face_with_sunglasses:"&gt;😎&lt;/span&gt; are &lt;U&gt;not&lt;/U&gt; special teams thus the counter does not start to count from 1 until "row_number" 9. Then because it starts to count at "row_number" 9 it keeps counting after even though the team C at "row_number" 10 is not a special_team. After row_number 10 the counter starts from 0 again because a new "event" starts at "row_number" 11.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;input Row_number Event Team $ Special_team $ Desired_result;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 A No 0&lt;BR /&gt;2 2 B Yes 1&lt;BR /&gt;3 3 C No 0&lt;BR /&gt;4 4 B Yes 1&lt;BR /&gt;5 4 A No 2&lt;BR /&gt;6 5 A No 0&lt;BR /&gt;7 6 C No 0&lt;BR /&gt;8 6 A No 0&lt;BR /&gt;9 6 D Yes 1&lt;BR /&gt;10 6 C No 2&lt;/P&gt;&lt;P&gt;11 7 A No 0&lt;/P&gt;&lt;P&gt;12 7 C No 0&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm pretty sure I need to use a combination of &lt;STRONG&gt;first.event&lt;/STRONG&gt; and &lt;STRONG&gt;RETAIN&amp;nbsp;&lt;/STRONG&gt;statements together but I couldn't find a solution yet. The code below did not work for me:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data test1;&lt;BR /&gt;set test;&lt;BR /&gt;by event;&lt;/P&gt;&lt;P&gt;retain counter;&lt;BR /&gt;if first.event then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;if special_team='Yes' then counter=1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;else if special_team='No' then counter=0;&lt;BR /&gt;else counter+1;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot in advance!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 17:37:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-statement/m-p/373370#M89320</guid>
      <dc:creator>Recep</dc:creator>
      <dc:date>2017-07-05T17:37:01Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-statement/m-p/373384#M89327</link>
      <description>&lt;P&gt;This might do it.&amp;nbsp; The hard part is figuring out the logic.&amp;nbsp; If I have it correctly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test1;&lt;/P&gt;
&lt;P&gt;set test;&lt;/P&gt;
&lt;P&gt;by event;&lt;/P&gt;
&lt;P&gt;if first.event then counter=0;&lt;/P&gt;
&lt;P&gt;if special_team='Yes' or counter &amp;gt; 0 then counter + 1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that you don't need to retain counter explicitly.&amp;nbsp; The statement COUNTER + 1 will automatically do that for you.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 17:58:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-statement/m-p/373384#M89327</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-07-05T17:58:05Z</dc:date>
    </item>
    <item>
      <title>Re: RETAIN statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RETAIN-statement/m-p/373392#M89332</link>
      <description>&lt;P&gt;Thanks a lot! It worked for the sample dataset. I'll apply it to my original data to see if I have to expand the example to consider a few other scenarios...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Recep&lt;/P&gt;</description>
      <pubDate>Wed, 05 Jul 2017 18:09:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RETAIN-statement/m-p/373392#M89332</guid>
      <dc:creator>Recep</dc:creator>
      <dc:date>2017-07-05T18:09:26Z</dc:date>
    </item>
  </channel>
</rss>

