<?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 Conditionally extract a single obs in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-extract-a-single-obs/m-p/420692#M280676</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a data like below:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1.png" style="width: 183px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/17178i842E63487263CAD8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="1.png" alt="1.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to have a data with a single obs based on the last value of column C.&lt;/P&gt;&lt;P&gt;Specifically, if the last value is negative, then I want the fifth row, which is AXX&amp;nbsp; 5 N or P;&lt;/P&gt;&lt;P&gt;and if it is positive, then I want the first row within positive value of C, which is AXX&amp;nbsp; 2&amp;nbsp; 1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my code&amp;nbsp;could only extract the last row in the case of negative value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data data1; set data1&amp;nbsp; end = last;&lt;BR /&gt;by&amp;nbsp;name;&lt;BR /&gt;if last then last_C&amp;nbsp;= XXXXXX;&lt;BR /&gt;if last_c&amp;lt; 0 then do;&lt;BR /&gt;output;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any helps are very appreciated!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 13 Dec 2017 08:44:08 GMT</pubDate>
    <dc:creator>cczzzl</dc:creator>
    <dc:date>2017-12-13T08:44:08Z</dc:date>
    <item>
      <title>Conditionally extract a single obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-extract-a-single-obs/m-p/420692#M280676</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a data like below:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1.png" style="width: 183px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/17178i842E63487263CAD8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="1.png" alt="1.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to have a data with a single obs based on the last value of column C.&lt;/P&gt;&lt;P&gt;Specifically, if the last value is negative, then I want the fifth row, which is AXX&amp;nbsp; 5 N or P;&lt;/P&gt;&lt;P&gt;and if it is positive, then I want the first row within positive value of C, which is AXX&amp;nbsp; 2&amp;nbsp; 1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my code&amp;nbsp;could only extract the last row in the case of negative value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data data1; set data1&amp;nbsp; end = last;&lt;BR /&gt;by&amp;nbsp;name;&lt;BR /&gt;if last then last_C&amp;nbsp;= XXXXXX;&lt;BR /&gt;if last_c&amp;lt; 0 then do;&lt;BR /&gt;output;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any helps are very appreciated!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 08:44:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-extract-a-single-obs/m-p/420692#M280676</guid>
      <dc:creator>cczzzl</dc:creator>
      <dc:date>2017-12-13T08:44:08Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally extract a single obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-extract-a-single-obs/m-p/420701#M280677</link>
      <description>&lt;P&gt;Post test data in the form of a datastep, and use the code window (its the {i} above post area) to post the code.&amp;nbsp; As such this is untested:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  by name;
  if last.name then do;
    if value &amp;lt; 0 then result=c;
    else do;
      if lag4(c) &amp;gt; 0 then result=lag4(c);
      else if lag3(c) &amp;gt; 0 then result=lag3(c);
      else if lag2(c) &amp;gt; 0 then result=lag2(c);
    end;
    output;
  end;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Dec 2017 09:16:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-extract-a-single-obs/m-p/420701#M280677</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-12-13T09:16:24Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally extract a single obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-extract-a-single-obs/m-p/420702#M280678</link>
      <description>&lt;P&gt;Something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input NAME$ B C;
datalines;
AXX 1 -1
AXX 2  1
AXX 3  2
AXX 4  3
AXX 5  4
;

proc sort data=have;
	by NAME descending C;
run;

data want;
	set have(where=(C&amp;gt;=0)) end=eof;
	by NAME;
	if first.NAME and C&amp;lt;0 then output;
	else if first.NAME and C&amp;gt;=0 then outflag=1;
	if eof and outflag=1 then output;
	retain outflag;drop outflag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Dec 2017 09:18:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-extract-a-single-obs/m-p/420702#M280678</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-12-13T09:18:17Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally extract a single obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-extract-a-single-obs/m-p/420753#M280679</link>
      <description>&lt;P&gt;Here's a way to retrieve the last value of C before you starting moving through all the observations:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;if _n_=1 then set have (keep=C rename=(C=last_C)) nobs=_nobs_ point=_nobs_;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;if last_C &amp;gt; 0 and C &amp;gt; 0 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;last_C=.;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;else if last_C &amp;gt; . and&amp;nbsp;_n_=_nobs_ then output;&lt;/P&gt;
&lt;P&gt;drop last_C;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's untested code, for now.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Dec 2017 12:00:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-extract-a-single-obs/m-p/420753#M280679</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-12-13T12:00:43Z</dc:date>
    </item>
  </channel>
</rss>

