<?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: If first. then group by; how to restart count in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-first-then-group-by-how-to-restart-count/m-p/648622#M194339</link>
    <description>&lt;P&gt;You have to include the variables in the BY statement if you want SAS to set values for FIRST. and LAST. variables for them.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;by DAY MTH;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You have to tell SAS not to reset the new variable COUNT to missing when it starts the next iteration.&amp;nbsp; You can use a RETAIN statement for that.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain count;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you could modify your code to use a SUM statement, which will automatically retain the variable that you are using as the accumulator in the SUM statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.MTH then count=1;
else count+1;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 18 May 2020 16:49:34 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-05-18T16:49:34Z</dc:date>
    <item>
      <title>If first. then group by; how to restart count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-first-then-group-by-how-to-restart-count/m-p/648621#M194338</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to assign a value (incremental by 1) to each row of a dataset, resetting to 1 when it's a new group.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(in the example I provide, column count should show 1,2,3 for the first three rows, then restarting from 1 on row 4 etc..)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it seems so simple but I can't get it work!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data WORKING_DAYS;
     input DAY MTH;
     datalines;
20190102 201901
20190103 201901
20190104 201901
20190201 201902
20190204 201902
20190205 201902
20190301 201903
20190304 201903
20190305 201903
20190401 201904
20190402 201904
20190403 201904
;
run;
 
data WORK.WORKING_DAYS;
     set WORK.WORKING_DAYS;
     by DAY;
 
     if first.MTH then
           count=1;
else count = count+1;
 
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 May 2020 16:44:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-first-then-group-by-how-to-restart-count/m-p/648621#M194338</guid>
      <dc:creator>MART1</dc:creator>
      <dc:date>2020-05-18T16:44:06Z</dc:date>
    </item>
    <item>
      <title>Re: If first. then group by; how to restart count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-first-then-group-by-how-to-restart-count/m-p/648622#M194339</link>
      <description>&lt;P&gt;You have to include the variables in the BY statement if you want SAS to set values for FIRST. and LAST. variables for them.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;by DAY MTH;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You have to tell SAS not to reset the new variable COUNT to missing when it starts the next iteration.&amp;nbsp; You can use a RETAIN statement for that.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;retain count;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you could modify your code to use a SUM statement, which will automatically retain the variable that you are using as the accumulator in the SUM statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.MTH then count=1;
else count+1;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 May 2020 16:49:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-first-then-group-by-how-to-restart-count/m-p/648622#M194339</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-05-18T16:49:34Z</dc:date>
    </item>
    <item>
      <title>Re: If first. then group by; how to restart count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-first-then-group-by-how-to-restart-count/m-p/648628#M194342</link>
      <description>&lt;P&gt;Part of the answer will come if you read the log.&lt;/P&gt;
&lt;P&gt;This line in your code:&lt;/P&gt;
&lt;PRE&gt;if first.MTH then&lt;/PRE&gt;
&lt;P&gt;will cause a note in your log:&lt;/P&gt;
&lt;PRE&gt;NOTE: Variable first.MTH is uninitialized.
&lt;/PRE&gt;
&lt;P&gt;That should give you clue that you are expected to do something with MTH variable. And since First and Last only apply to variables on a BY statement ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 May 2020 17:04:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-first-then-group-by-how-to-restart-count/m-p/648628#M194342</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-05-18T17:04:22Z</dc:date>
    </item>
    <item>
      <title>Re: If first. then group by; how to restart count</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-first-then-group-by-how-to-restart-count/m-p/648812#M194435</link>
      <description>&lt;P&gt;thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've used the SUM option and it works perfectly&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(ps: the group by must be&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;by MTH DAY;&lt;/PRE&gt;&lt;P&gt;thanks&lt;/P&gt;</description>
      <pubDate>Tue, 19 May 2020 12:54:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-first-then-group-by-how-to-restart-count/m-p/648812#M194435</guid>
      <dc:creator>MART1</dc:creator>
      <dc:date>2020-05-19T12:54:19Z</dc:date>
    </item>
  </channel>
</rss>

