<?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 SAS conditionally updating previous row's observation values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-conditionally-updating-previous-row-s-observation-values/m-p/711241#M219063</link>
    <description>&lt;P&gt;Greetings,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a rather peculiar data situation that I am stuck on.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a list of sample id's that undergoes 2 tests during each session: a RED &amp;amp; BLUE test. The test result values are either 0 (negative) or 1 (positive).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To make a diagnosis based on either tests, 2 consecutive positive tests results are required. However, there are sessions where the test results are missing (denoted by "."). It is OK for these "missing" sessions to be in between 2 consecutive positive results.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data looks like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MigMaster12_0-1610561188960.png" style="width: 621px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/53475i1934C422A0379CDA/image-dimensions/621x308?v=v2" width="621" height="308" role="button" title="MigMaster12_0-1610561188960.png" alt="MigMaster12_0-1610561188960.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note that the test diagnosis is currently made at the 2nd consecutive positive result session. Furthermore, after a diagnosis is made, it is kept through the sample's following sessions, until a diagnosis based on a different test is made by the consecutive rule (see ID 2).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;I would like to update the diagnosis column to where the test diagnosis is made at the 1st of the 2-consecutive-positive-result sessions, as indicated in the last column "desired diagnosis output".&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How would I go about updating the previous session rows?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help!&lt;/P&gt;</description>
    <pubDate>Wed, 13 Jan 2021 18:07:34 GMT</pubDate>
    <dc:creator>MigMaster12</dc:creator>
    <dc:date>2021-01-13T18:07:34Z</dc:date>
    <item>
      <title>SAS conditionally updating previous row's observation values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-conditionally-updating-previous-row-s-observation-values/m-p/711241#M219063</link>
      <description>&lt;P&gt;Greetings,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a rather peculiar data situation that I am stuck on.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a list of sample id's that undergoes 2 tests during each session: a RED &amp;amp; BLUE test. The test result values are either 0 (negative) or 1 (positive).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To make a diagnosis based on either tests, 2 consecutive positive tests results are required. However, there are sessions where the test results are missing (denoted by "."). It is OK for these "missing" sessions to be in between 2 consecutive positive results.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data looks like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MigMaster12_0-1610561188960.png" style="width: 621px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/53475i1934C422A0379CDA/image-dimensions/621x308?v=v2" width="621" height="308" role="button" title="MigMaster12_0-1610561188960.png" alt="MigMaster12_0-1610561188960.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note that the test diagnosis is currently made at the 2nd consecutive positive result session. Furthermore, after a diagnosis is made, it is kept through the sample's following sessions, until a diagnosis based on a different test is made by the consecutive rule (see ID 2).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;I would like to update the diagnosis column to where the test diagnosis is made at the 1st of the 2-consecutive-positive-result sessions, as indicated in the last column "desired diagnosis output".&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How would I go about updating the previous session rows?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your help!&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jan 2021 18:07:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-conditionally-updating-previous-row-s-observation-values/m-p/711241#M219063</guid>
      <dc:creator>MigMaster12</dc:creator>
      <dc:date>2021-01-13T18:07:34Z</dc:date>
    </item>
    <item>
      <title>Re: SAS conditionally updating previous row's observation values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-conditionally-updating-previous-row-s-observation-values/m-p/711385#M219143</link>
      <description>&lt;P&gt;How about this? I assume that "Blue" Trumps "Red" and that only those two are considered.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Feel free to ask &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id session red_test blue_test;
datalines;
1 1 0 0
1 3 1 0
1 4 1 0
1 5 1 1
1 6 1 0
2 1 1 0
2 2 1 1
2 3 0 0
2 5 0 0
2 7 0 1
2 8 0 1
2 9 0 0
3 1 1 0
3 2 . .
3 3 1 0
3 4 0 1
;

data want;
   
   set have curobs = c;
   by id;

   if first.id then diagnosis = '    ';

   if diagnosis = '    ' then do p = (c + 1) by 1;
      if p &amp;gt; n then leave;
      set have (keep = id red_test rename = (id = i red_test = r)) point = p nobs = n;
      if id ne i then leave;
      if red_test = 1 and r = 0 then leave;
      if red_test = 1 and r = 1 then do;
         diagnosis = 'RED ';
         leave;
      end;
   end;

   else if diagnosis in ('    ', 'RED ') then do p = (c + 1) by 1;
      if p &amp;gt; n then leave;
      set have (keep = id blue_test rename = (id = i blue_test = b)) point = p nobs = n;
      if id ne i then leave;
      if blue_test = 1 and b = 0 then leave;
      if blue_test = 1 and b = 1 then do;
         diagnosis = 'BLUE';
         leave;
      end;
   end;
   
   retain diagnosis;
   drop i r b;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;id session red_test blue_test diagnosis 
1  1       0        0           
1  3       1        0         RED 
1  4       1        0         RED 
1  5       1        1         RED 
1  6       1        0         RED 
2  1       1        0         RED 
2  2       1        1         RED 
2  3       0        0         RED 
2  5       0        0         RED 
2  7       0        1         BLUE 
2  8       0        1         BLUE 
2  9       0        0         BLUE 
3  1       1        0         RED 
3  2       .        .         RED 
3  3       1        0         RED 
3  4       0        1         RED &lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Jan 2021 09:45:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-conditionally-updating-previous-row-s-observation-values/m-p/711385#M219143</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-14T09:45:23Z</dc:date>
    </item>
  </channel>
</rss>

