<?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: Keep Record Based on Value of a Prior or Proceeding Observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Keep-Record-Based-on-Value-of-a-Prior-or-Proceeding-Observation/m-p/344789#M79260</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input SUBJ $ VISIT LAT $ DISCREP $;
cards4;
101-101 1 Left yes
101-101 1 Right yes
101-101 2 Left no
101-101 2 Right no
101-101 3 Left yes
101-101 3 Right no
101-101 4 Left no
101-101 4 Right no
101-102 1 Left no
101-102 1 Right no
101-102 2 Left yes
101-102 2 Right no
101-102 3 Left no
101-102 3 Right no
;;;;
run;


data want;
  retain flag .; /* not necessary but I like it for clarity */
  do until (last.visit);
    set have;
    by visit notsorted;
    if discrep='yes' then flag=1;
  end;
  do until (last.visit);
    set have;
    by visit notsorted;
    if flag then output;
  end;
  flag=.;
run;quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 27 Mar 2017 20:39:26 GMT</pubDate>
    <dc:creator>rogerjdeangelis</dc:creator>
    <dc:date>2017-03-27T20:39:26Z</dc:date>
    <item>
      <title>Keep Record Based on Value of a Prior or Proceeding Observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-Record-Based-on-Value-of-a-Prior-or-Proceeding-Observation/m-p/344779#M79256</link>
      <description>&lt;P&gt;I have a reporting situation that I cannot figure out. &amp;nbsp;I have a vertical dataset, and there are typically 2 rows per visit (left and right laterality). &amp;nbsp;I am checking the data for discrepancies. &amp;nbsp;If a discrepancy exists for a given visit for either laterality, then I want to do output both rows regardless of whether or not the other laterality has a discrepancy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For simplicity, here is some sample data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
 input SUBJ $ VISIT LAT $ DISCREP $;
 cards;
101-101 1 Left yes
101-101 1 Right yes
101-101 2 Left no
101-101 2 Right no
101-101 3 Left yes
101-101 3 Right no
101-101 4 Left no
101-101 4 Right no
101-102 1 Left no
101-102 1 Right no
101-102 2 Left yes
101-102 2 Right no
101-102 3 Left no
101-102 3 Right no
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;This is what the final table should show:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;SUBJ&lt;/TD&gt;
&lt;TD&gt;VISIT&lt;/TD&gt;
&lt;TD&gt;LAT&lt;/TD&gt;
&lt;TD&gt;DISCREP&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;101-101&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;Left&lt;/TD&gt;
&lt;TD&gt;yes&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;101-101&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;Right&lt;/TD&gt;
&lt;TD&gt;&lt;SPAN&gt;yes&lt;/SPAN&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;101-101&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;Left&lt;/TD&gt;
&lt;TD&gt;&lt;SPAN&gt;yes&lt;/SPAN&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;101-101&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;Right&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;no&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;101-102&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;Left&lt;/TD&gt;
&lt;TD&gt;&lt;SPAN&gt;yes&lt;/SPAN&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;101-102&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;Right&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;no&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any ideas on how to do this? &amp;nbsp;I'm guessing something with a RETAIN statement. &amp;nbsp;I'm sure it will be extremely simple, but my brain is done for the day.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Mar 2017 20:27:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-Record-Based-on-Value-of-a-Prior-or-Proceeding-Observation/m-p/344779#M79256</guid>
      <dc:creator>djbateman</dc:creator>
      <dc:date>2017-03-27T20:27:57Z</dc:date>
    </item>
    <item>
      <title>Re: Keep Record Based on Value of a Prior or Proceeding Observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-Record-Based-on-Value-of-a-Prior-or-Proceeding-Observation/m-p/344785#M79258</link>
      <description>&lt;P&gt;A relatively simple end-of-day solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;wanted='N';&lt;/P&gt;
&lt;P&gt;do until (last.visit);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by subj visit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if discrep='yes' then wanted='Y';&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;do until (last.visit);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; by subj visit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if wanted='Y' then output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop wanted;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The top loop examines both observations, and the bottom loop outputs either zero or both observations.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Mar 2017 20:29:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-Record-Based-on-Value-of-a-Prior-or-Proceeding-Observation/m-p/344785#M79258</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-03-27T20:29:07Z</dc:date>
    </item>
    <item>
      <title>Re: Keep Record Based on Value of a Prior or Proceeding Observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-Record-Based-on-Value-of-a-Prior-or-Proceeding-Observation/m-p/344789#M79260</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input SUBJ $ VISIT LAT $ DISCREP $;
cards4;
101-101 1 Left yes
101-101 1 Right yes
101-101 2 Left no
101-101 2 Right no
101-101 3 Left yes
101-101 3 Right no
101-101 4 Left no
101-101 4 Right no
101-102 1 Left no
101-102 1 Right no
101-102 2 Left yes
101-102 2 Right no
101-102 3 Left no
101-102 3 Right no
;;;;
run;


data want;
  retain flag .; /* not necessary but I like it for clarity */
  do until (last.visit);
    set have;
    by visit notsorted;
    if discrep='yes' then flag=1;
  end;
  do until (last.visit);
    set have;
    by visit notsorted;
    if flag then output;
  end;
  flag=.;
run;quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Mar 2017 20:39:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-Record-Based-on-Value-of-a-Prior-or-Proceeding-Observation/m-p/344789#M79260</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-03-27T20:39:26Z</dc:date>
    </item>
    <item>
      <title>Re: Keep Record Based on Value of a Prior or Proceeding Observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-Record-Based-on-Value-of-a-Prior-or-Proceeding-Observation/m-p/344873#M79299</link>
      <description>&lt;P&gt;Hi Astounding,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a small question about the workings of your code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the first part when you decide to make wanted='Y' when discrep = 'yes', does your code give the value 'Y' to all of the entries within the subject-visit block if the value 'yes' appears at least once, and then in the second part output all the observations where wanted = 'Y',&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or , you give the value 'Y' only to the observation that has 'yes', and in the second part output the subject-visit block if 'Y' appears at least once.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2017 04:17:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-Record-Based-on-Value-of-a-Prior-or-Proceeding-Observation/m-p/344873#M79299</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-03-28T04:17:50Z</dc:date>
    </item>
    <item>
      <title>Re: Keep Record Based on Value of a Prior or Proceeding Observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-Record-Based-on-Value-of-a-Prior-or-Proceeding-Observation/m-p/344885#M79306</link>
      <description>&lt;P&gt;I'm sorry to say, the answer is that it depends.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code changes WANTED to "Y" the first time it encounters "yes". &amp;nbsp;From that point forward (to the end of the BY group), WANTED remains "Y". &amp;nbsp;You can observe the behavior by adding this statement inside the first loop:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;put wanted=;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;More important, the top loop does not output any observations. &amp;nbsp;Its key function is to change WANTED in the PDV, where SAS is storing the current value of all variables. &amp;nbsp;Once WANTED becomes "Y", there is no code to change it to anything else (neither to "N" nor to a blank) until the second loop has completed.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2017 05:25:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-Record-Based-on-Value-of-a-Prior-or-Proceeding-Observation/m-p/344885#M79306</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-03-28T05:25:54Z</dc:date>
    </item>
    <item>
      <title>Re: Keep Record Based on Value of a Prior or Proceeding Observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-Record-Based-on-Value-of-a-Prior-or-Proceeding-Observation/m-p/345079#M79350</link>
      <description>&lt;P&gt;I tried to modify the original data in the following way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;101-101 3 Left no
101-101 3 Right yes&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;so for the 3rd visit of subject 101-101, Left is "no" and Right is "yes" - so the code will see the "no" and then see the"yes" - but nevertheless in the final want data all the info will be displayed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So even if SAS encounters the condition for the last observation within a specific subject-visit block, it applies it to the previous &amp;nbsp;observations even if they didn't have the condition?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2017 16:57:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-Record-Based-on-Value-of-a-Prior-or-Proceeding-Observation/m-p/345079#M79350</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-03-28T16:57:17Z</dc:date>
    </item>
    <item>
      <title>Re: Keep Record Based on Value of a Prior or Proceeding Observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-Record-Based-on-Value-of-a-Prior-or-Proceeding-Observation/m-p/345081#M79351</link>
      <description>&lt;P&gt;It doesn't really apply to the previous observations.&amp;nbsp; It just sets WANTED and leaves it that way.&amp;nbsp; Remember, the first DO loop doesn't output anything, it only reads some observations and calculates WANTED.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason you get all the observations (within the block) output is that the second DO loop reads exactly the same observations as the first DO loop.&amp;nbsp; The second DO loop looks at the previously-assigned value for WANTED, when determining which observations to output.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Mar 2017 17:05:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-Record-Based-on-Value-of-a-Prior-or-Proceeding-Observation/m-p/345081#M79351</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-03-28T17:05:10Z</dc:date>
    </item>
  </channel>
</rss>

