<?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: numbering observations with do loop in SAS Studio</title>
    <link>https://communities.sas.com/t5/SAS-Studio/numbering-observations-with-do-loop/m-p/498680#M6271</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
retain _round 0;
prevstat = lag(status);
if status = 'empty' then do;
  day = .;
  round = .;
end;
else do;
  if prevstat in ('empty','') then do;
    _round + 1;
    day = 0;
  end;
  round = _round;
  day + 1;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 25 Sep 2018 09:22:58 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-09-25T09:22:58Z</dc:date>
    <item>
      <title>numbering observations with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Studio/numbering-observations-with-do-loop/m-p/498673#M6269</link>
      <description>&lt;P&gt;Good morning all! I am new to this forum and I hope that someone can help me.&lt;/P&gt;&lt;P&gt;I have a dataset made in this way&lt;/P&gt;&lt;P&gt;date animals status&lt;/P&gt;&lt;P&gt;1/03/2015 30000 full&lt;/P&gt;&lt;P&gt;1/04/2015 29950&amp;nbsp;full&lt;/P&gt;&lt;P&gt;1/04/2015 &lt;SPAN&gt;29905&lt;/SPAN&gt;&amp;nbsp;full&lt;/P&gt;&lt;P&gt;1/05/2015 28880&amp;nbsp;full&lt;/P&gt;&lt;P&gt;1/06/2015 28870 full&lt;/P&gt;&lt;P&gt;. . .&amp;nbsp;&lt;/P&gt;&lt;P&gt;. . .&amp;nbsp;&lt;/P&gt;&lt;P&gt;. . .&lt;/P&gt;&lt;P&gt;2/20/2015 0 empty&lt;/P&gt;&lt;P&gt;2/21/2015 0 empty&lt;/P&gt;&lt;P&gt;2/22/2015 0 empty&lt;/P&gt;&lt;P&gt;. . .&lt;/P&gt;&lt;P&gt;. . .&amp;nbsp;&lt;/P&gt;&lt;P&gt;2/28/2015 30000 full&lt;/P&gt;&lt;P&gt;&amp;nbsp;ecc..&lt;/P&gt;&lt;P&gt;Let me explain my problem, I have to number the day each time the shed is filled with animals (and I need to stop the count when the shed is empty), but I really do not know how to do this. the file is made of several rounds (made in the same way)&lt;/P&gt;&lt;P&gt;I've tried some do loop including the "if first." condition, but did not work properly. I &amp;nbsp;tried also some macros, but I'm not so skilled.&lt;/P&gt;&lt;P&gt;the final dataset should look like this:&lt;/P&gt;&lt;P&gt;date animals status day round&lt;/P&gt;&lt;P&gt;1/03/2015 30000 full 1 1&lt;/P&gt;&lt;P&gt;1/04/2015 29950&amp;nbsp;full 2 1&lt;/P&gt;&lt;P&gt;1/04/2015 &lt;SPAN&gt;29905&lt;/SPAN&gt;&amp;nbsp;full 3 1&lt;/P&gt;&lt;P&gt;1/05/2015 28880&amp;nbsp;full 4 1&lt;/P&gt;&lt;P&gt;1/06/2015 28870 full 5 1&lt;/P&gt;&lt;P&gt;. . .&amp;nbsp;&lt;/P&gt;&lt;P&gt;. . .&amp;nbsp;&lt;/P&gt;&lt;P&gt;. . .&lt;/P&gt;&lt;P&gt;2/20/2015 0 empty . .&lt;/P&gt;&lt;P&gt;2/21/2015 0 empty . .&lt;/P&gt;&lt;P&gt;2/22/2015 0 empty . .&lt;/P&gt;&lt;P&gt;. . .&lt;/P&gt;&lt;P&gt;. . .&amp;nbsp;&lt;/P&gt;&lt;P&gt;2/28/2015 30000 full 1 2&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3/01/2015 29999&amp;nbsp;full 2&amp;nbsp;2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3/02/2015 29999&amp;nbsp;full 3&amp;nbsp;2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;ecc..&lt;/P&gt;</description>
      <pubDate>Tue, 25 Sep 2018 09:03:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/numbering-observations-with-do-loop/m-p/498673#M6269</guid>
      <dc:creator>tullina82</dc:creator>
      <dc:date>2018-09-25T09:03:55Z</dc:date>
    </item>
    <item>
      <title>Re: numbering observations with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Studio/numbering-observations-with-do-loop/m-p/498679#M6270</link>
      <description>&lt;P&gt;Arrays make processing variables in observation easier, macros are for code generation and seldom a must-have to solve a problem. First/Last seem to be the appropriate tools to solve the issue, but you need to explain the logic in more detail, e.g. why is day increased when date has not changed? And posting data as data-step using datalines-statement makes it easier to work on your problem.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Sep 2018 09:14:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/numbering-observations-with-do-loop/m-p/498679#M6270</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-09-25T09:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: numbering observations with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Studio/numbering-observations-with-do-loop/m-p/498680#M6271</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
retain _round 0;
prevstat = lag(status);
if status = 'empty' then do;
  day = .;
  round = .;
end;
else do;
  if prevstat in ('empty','') then do;
    _round + 1;
    day = 0;
  end;
  round = _round;
  day + 1;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Sep 2018 09:22:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/numbering-observations-with-do-loop/m-p/498680#M6271</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-09-25T09:22:58Z</dc:date>
    </item>
    <item>
      <title>Re: numbering observations with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Studio/numbering-observations-with-do-loop/m-p/498681#M6272</link>
      <description>&lt;P&gt;Sorry, the data did not change because It was a mistake.&lt;/P&gt;&lt;P&gt;I included a part of the file to be more clear.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Sep 2018 09:24:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/numbering-observations-with-do-loop/m-p/498681#M6272</guid>
      <dc:creator>tullina82</dc:creator>
      <dc:date>2018-09-25T09:24:57Z</dc:date>
    </item>
    <item>
      <title>Re: numbering observations with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Studio/numbering-observations-with-do-loop/m-p/498682#M6273</link>
      <description>&lt;P&gt;It worked!&lt;/P&gt;&lt;P&gt;thank you very much!!&lt;/P&gt;</description>
      <pubDate>Tue, 25 Sep 2018 09:27:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/numbering-observations-with-do-loop/m-p/498682#M6273</guid>
      <dc:creator>tullina82</dc:creator>
      <dc:date>2018-09-25T09:27:10Z</dc:date>
    </item>
    <item>
      <title>Re: numbering observations with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Studio/numbering-observations-with-do-loop/m-p/498708#M6274</link>
      <description>&lt;P&gt;To me using a BY statement with NOSTORTED options is a bit easier.&amp;nbsp; Same difference I reckon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by status notsorted;
   if first.status then call missing(d);
   if status ne: 'em' then do;
      d + 1;
      if first.status then r+1;
      day = d;
      round = r;
      end;
   drop d r;
   run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Sep 2018 12:25:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/numbering-observations-with-do-loop/m-p/498708#M6274</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2018-09-25T12:25:57Z</dc:date>
    </item>
  </channel>
</rss>

