<?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: How to account for order in a do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-account-for-order-in-a-do-loop/m-p/743278#M232679</link>
    <description>&lt;P&gt;thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976"&gt;@SASKiwi&lt;/a&gt;&amp;nbsp;very much! That resolved my problem! I appreciate your help!&lt;/P&gt;&lt;DIV class="author-20-content"&gt;&lt;DIV class="sas-author-rank"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Mon, 24 May 2021 00:22:17 GMT</pubDate>
    <dc:creator>sdaniels429</dc:creator>
    <dc:date>2021-05-24T00:22:17Z</dc:date>
    <item>
      <title>How to account for order in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-account-for-order-in-a-do-loop/m-p/743261#M232665</link>
      <description>&lt;P&gt;Dear SAS users,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to capture change as well as the order of change in a do loop. But I can't get the code to work. Below is my hypothetical data and code.&amp;nbsp; I want to create a "censor" variable for the observations to flag those who got category=2 and never got category=4 with the rest of the days. So for example, id=1 should get censor=1, but id=2 or id=3 shouldn't&amp;nbsp; because it got category=4 afterwards.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With my current code, I'm only able to code id=1 and id=3 correctly. I realize I shouldn't use do until but I can't get do while to work. I also don't have to stick to the do loop if that's not the appropriate approach. Any suggestion is appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID days category;
datalines;

1 89 4
1 79 3
1 60 2
1 2 1
2 88 3
2 72 2
2 70 1
2 50 4
3 88 2
3 35 4
;
run;

proc sort data=have ;
by ID descending days;
run;

data have2;
do until(last.ID);
set have;
by ID category notsorted;
if lag(category)^=4 and category=2 then censor=1;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 23 May 2021 20:42:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-account-for-order-in-a-do-loop/m-p/743261#M232665</guid>
      <dc:creator>sdaniels429</dc:creator>
      <dc:date>2021-05-23T20:42:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to account for order in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-account-for-order-in-a-do-loop/m-p/743263#M232667</link>
      <description>&lt;P&gt;How about this? Don't think you need a DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID days category;
datalines;
1 89 4
1 79 3
1 60 2
1 2 1
2 88 3
2 72 2
2 70 1
2 50 4
3 88 2
3 35 4
;
run;

proc sort data=have ;
by ID descending days;
run;

data have2;
  set have;
  by ID;
  if first.id then censor = 0;
  retain censor;
  if category = 2 then censor = 1;
  if censor = 1 and category = 4 then censor = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 23 May 2021 21:15:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-account-for-order-in-a-do-loop/m-p/743263#M232667</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2021-05-23T21:15:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to account for order in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-account-for-order-in-a-do-loop/m-p/743278#M232679</link>
      <description>&lt;P&gt;thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13976"&gt;@SASKiwi&lt;/a&gt;&amp;nbsp;very much! That resolved my problem! I appreciate your help!&lt;/P&gt;&lt;DIV class="author-20-content"&gt;&lt;DIV class="sas-author-rank"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 24 May 2021 00:22:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-account-for-order-in-a-do-loop/m-p/743278#M232679</guid>
      <dc:creator>sdaniels429</dc:creator>
      <dc:date>2021-05-24T00:22:17Z</dc:date>
    </item>
  </channel>
</rss>

