<?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: check the current value with the next record in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/check-the-current-value-with-the-next-record/m-p/230864#M41935</link>
    <description>&lt;P&gt;Hello Jag,&lt;/P&gt;
&lt;P&gt;Try the LAG() function as shown below:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt; set have;&lt;BR /&gt; if value = 2 and lag1(value) = 1 then output;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This writes 3 records to the output. &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;101 2&lt;/P&gt;
&lt;P&gt;102 2&lt;/P&gt;
&lt;P&gt;103 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is this what you are looking for ?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 21 Oct 2015 03:36:55 GMT</pubDate>
    <dc:creator>kannand</dc:creator>
    <dc:date>2015-10-21T03:36:55Z</dc:date>
    <item>
      <title>check the current value with the next record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-the-current-value-with-the-next-record/m-p/230856#M41931</link>
      <description>&lt;P&gt;Dear Experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a scenario where i need to check the current values with the next value, I need to identify those subject whose values has 1 followed by 2 , if the subject have value 2 followed by 1 then i should exclude them.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;please consider the sample data like below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input subjid value;
cards;
101 1
101 .
101 1 
101 2
101 1
102 1
102 2
102 .
102 1
103 2
103 1
103 .
103 1
;

proc sort data=have;
by subjid ;
run;

data want;
 set have;
 by subjid  ;
 retain first ;
 if first.subjid then do;first=.;end;
if value ne . then first=value;
run;

 data want2;
 obs=_n_+1;
 set want end=eof;
 by subjid;
 if nobs&amp;gt;=obs then set want(keep=first rename=(first=nextfirst)) point=obs nobs=nobs;
run;

data want2;
  set want2;
  by subjid;
  if not last.subjid and first=1 and  nextfirst=2 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I wrote the above code which gets me the expected output, however i am looking for a better approach.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate your responses.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jag&lt;/P&gt;</description>
      <pubDate>Wed, 21 Oct 2015 01:53:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-the-current-value-with-the-next-record/m-p/230856#M41931</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2015-10-21T01:53:48Z</dc:date>
    </item>
    <item>
      <title>Re: check the current value with the next record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-the-current-value-with-the-next-record/m-p/230864#M41935</link>
      <description>&lt;P&gt;Hello Jag,&lt;/P&gt;
&lt;P&gt;Try the LAG() function as shown below:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt; set have;&lt;BR /&gt; if value = 2 and lag1(value) = 1 then output;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This writes 3 records to the output. &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;101 2&lt;/P&gt;
&lt;P&gt;102 2&lt;/P&gt;
&lt;P&gt;103 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is this what you are looking for ?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Oct 2015 03:36:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-the-current-value-with-the-next-record/m-p/230864#M41935</guid>
      <dc:creator>kannand</dc:creator>
      <dc:date>2015-10-21T03:36:55Z</dc:date>
    </item>
    <item>
      <title>Re: check the current value with the next record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-the-current-value-with-the-next-record/m-p/230869#M41940</link>
      <description>&lt;P&gt;Yes, thank you so much for the thought.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works but here is the code which gives me the expected output. I need to get 101 and 102 subjects as these are the only subjects with value variable 1 followed by 2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by subjid ;
run;

data want;
 set have;
 by subjid  ;
 retain first ;
 if first.subjid then do;first=.;end;
if value ne . then first=value;
if last.subjid then first=.;
if first=2 and lag(first)=1;
run;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jag&lt;/P&gt;</description>
      <pubDate>Wed, 21 Oct 2015 04:32:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-the-current-value-with-the-next-record/m-p/230869#M41940</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2015-10-21T04:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: check the current value with the next record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-the-current-value-with-the-next-record/m-p/230884#M41944</link>
      <description>&lt;P&gt;What about:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table WANT as
  select  distinct SUBJID
  from    HAVE A
  where   exists (select SUBJID from HAVE where SUBJID=A.SUBJID and VALUE=1)
      and exists (select SUBJID from HAVE where SUBJID=A.SUBJID and VALUE=2);
quit;&lt;/PRE&gt;
&lt;P&gt;As there is no logical sequence in the data, I can only say there is a 1 and a 2. &amp;nbsp;If there is other data such as datetime, length you can easily pull that up as min(datetime) where &amp;gt; A.datetime, but I am just guessing here as the test data doesn't show it.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Oct 2015 09:30:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-the-current-value-with-the-next-record/m-p/230884#M41944</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-10-21T09:30:36Z</dc:date>
    </item>
    <item>
      <title>Re: check the current value with the next record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-the-current-value-with-the-next-record/m-p/230922#M41954</link>
      <description>&lt;P&gt;One thing that's not clear about your question ... if you are keeping a SUBJID, which observations do you want to keep?&amp;nbsp; I'll assume that you want to keep all of them.&amp;nbsp; Here's one approach:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; keepme='N';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; do until (last.subjid);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by subjid;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if value=1 then found_1='Y';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if value=2 and found_1='Y' then keepme='Y';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; do until (last.subjid);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by subjid;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if keepme='Y' then output;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; drop found_1 keepme;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Oct 2015 12:58:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-the-current-value-with-the-next-record/m-p/230922#M41954</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-10-21T12:58:34Z</dc:date>
    </item>
  </channel>
</rss>

