<?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: Compare the variable in first row to the another varibale in second row by the same id. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compare-the-variable-in-first-row-to-the-another-varibale-in/m-p/827199#M326739</link>
    <description>Thanks for the respond. Yeah, maybe it is not clear. So, the logic is if the 'start' in the second row is the same number as 'end' in the second row. Then we can say that there is not necessary to use two rows to write this dataset. we can replace it by using the 'start' in the first row and 'end' in the second row. And obey the same logic when they have same 'id'.&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
    <pubDate>Thu, 04 Aug 2022 18:14:20 GMT</pubDate>
    <dc:creator>ttqkroe</dc:creator>
    <dc:date>2022-08-04T18:14:20Z</dc:date>
    <item>
      <title>Compare the variable in first row to the another varibale in second row by the same id.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-the-variable-in-first-row-to-the-another-varibale-in/m-p/827188#M326732</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hi, everyone.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ttqkroe_1-1659634707159.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74149i89D89D2E02052E38/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ttqkroe_1-1659634707159.png" alt="ttqkroe_1-1659634707159.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This is what my dataset looks like. And I am trying to compare the 'start' and 'end' to delete unnecessary rows.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If 'start' is unqiue compare to the previous 'end', then we will keep the row. If 'start' is the same to the previous 'end', then we need to delete that row and make the 'end' in first row to be the same as the second row.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The result should be like&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ttqkroe_2-1659634716019.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74150iCD24EBDAFC6A267B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="ttqkroe_2-1659634716019.png" alt="ttqkroe_2-1659634716019.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2022 17:38:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-the-variable-in-first-row-to-the-another-varibale-in/m-p/827188#M326732</guid>
      <dc:creator>ttqkroe</dc:creator>
      <dc:date>2022-08-04T17:38:16Z</dc:date>
    </item>
    <item>
      <title>Re: Compare the variable in first row to the another varibale in second row by the same id.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-the-variable-in-first-row-to-the-another-varibale-in/m-p/827196#M326737</link>
      <description>&lt;P&gt;I don't understand the logic. It seems as if you want to either keep a row, or delete a row, but the first row in your output is not an actual row of the data set. Please explain.&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2022 17:59:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-the-variable-in-first-row-to-the-another-varibale-in/m-p/827196#M326737</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-08-04T17:59:24Z</dc:date>
    </item>
    <item>
      <title>Re: Compare the variable in first row to the another varibale in second row by the same id.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-the-variable-in-first-row-to-the-another-varibale-in/m-p/827198#M326738</link>
      <description>&lt;P&gt;Please post data as text not images. Code is untested as I'm too lazy to type it out to test anything.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use LAG to check for groupings. Then use PROC MEANS/SQL to collate the data. I'll use SQL as you may have character variables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data groups;
set have;
by ID;
prev_end= lag(end);
if first.id then do;
group=0;
call missing(prev_end);
end;
if start ne prev_end then group+1;
run;

proc sql;
create table want as 
select id, group, min(start) as start, max(end) as end, a, b, c, d,
from groups
group by id, group, a, b, c, d
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Thu, 04 Aug 2022 21:39:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-the-variable-in-first-row-to-the-another-varibale-in/m-p/827198#M326738</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-08-04T21:39:11Z</dc:date>
    </item>
    <item>
      <title>Re: Compare the variable in first row to the another varibale in second row by the same id.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-the-variable-in-first-row-to-the-another-varibale-in/m-p/827199#M326739</link>
      <description>Thanks for the respond. Yeah, maybe it is not clear. So, the logic is if the 'start' in the second row is the same number as 'end' in the second row. Then we can say that there is not necessary to use two rows to write this dataset. we can replace it by using the 'start' in the first row and 'end' in the second row. And obey the same logic when they have same 'id'.&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Thu, 04 Aug 2022 18:14:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-the-variable-in-first-row-to-the-another-varibale-in/m-p/827199#M326739</guid>
      <dc:creator>ttqkroe</dc:creator>
      <dc:date>2022-08-04T18:14:20Z</dc:date>
    </item>
    <item>
      <title>Re: Compare the variable in first row to the another varibale in second row by the same id.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-the-variable-in-first-row-to-the-another-varibale-in/m-p/827217#M326749</link>
      <description>&lt;P&gt;If the data are already sorted by ID/START, then you could:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=i nxt_:);
  merge have
        have (firstobs=2 keep=id start rename=(id=nxt_id start=nxt_start));

  if end^=nxt_start or id^=nxt_id then do i=1 to coalesce(dif(_n_),_n_);
    set have (drop=end);
    if i=1 then output;
  end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The MERGE statement read a number of observations until the current END doesn't match the next START.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The do loop rereads all those observations (except the variable END).&amp;nbsp; It outputs only the first of them (to get the initial START as well as initial a b c d).&amp;nbsp; But it keeps the END value from the last connected obs determined via the MERGE statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Aug 2022 20:12:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-the-variable-in-first-row-to-the-another-varibale-in/m-p/827217#M326749</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-08-04T20:12:19Z</dc:date>
    </item>
  </channel>
</rss>

