<?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 rows in the same table/dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669022#M200649</link>
    <description>&lt;P&gt;&lt;FONT size="2"&gt;Yes.&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;Expected output would be&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;Row&amp;nbsp; &amp;nbsp;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SITE&amp;nbsp; &amp;nbsp; END DATE&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1100&amp;nbsp; &amp;nbsp;AAA&amp;nbsp; &amp;nbsp; &amp;nbsp; BLANK&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1100&amp;nbsp; BBB&amp;nbsp; &amp;nbsp; &amp;nbsp; BLANK&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1200&amp;nbsp; AAA&amp;nbsp; &amp;nbsp; &amp;nbsp; 13JUN2020&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1200 CCC&amp;nbsp; &amp;nbsp; &amp;nbsp; 13JUN2020&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 13 Jul 2020 23:50:03 GMT</pubDate>
    <dc:creator>lmclaughlin10</dc:creator>
    <dc:date>2020-07-13T23:50:03Z</dc:date>
    <item>
      <title>Compare rows in the same table/dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669017#M200643</link>
      <description>&lt;P&gt;(New SAS user)&lt;/P&gt;&lt;P&gt;Not sure where to start.&amp;nbsp; I have a dataset which I need to read a row and perform some evaluations. Then I need to read the second row and compare it to the primary key (ID, SITE)&amp;nbsp; in the first row, if they are equal, read the next row, if primary key not equal perform the evaluation. For example: Read row 1, if end date less than date (specific field in dataset) then xxx, read new row, if ID, SITE equal ID, SITE in row 1, read next row, else perform evaluation. Thanks in advance for your assistance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample Dataset:&lt;/P&gt;&lt;P&gt;ROW&amp;nbsp; &amp;nbsp; &amp;nbsp;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SITE&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; END DATE&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1100&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AAA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; BLANK&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1100&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AAA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12JUN2020&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1100&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BBB&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; BLANK&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1200&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AAA&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 13JUN2020&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1200&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;CCC&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 13JUN2020&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jul 2020 23:27:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669017#M200643</guid>
      <dc:creator>lmclaughlin10</dc:creator>
      <dc:date>2020-07-13T23:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: Compare rows in the same table/dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669021#M200648</link>
      <description>Can you give us an idea of what you want in expected output using your sample dataset as a guide?</description>
      <pubDate>Mon, 13 Jul 2020 23:42:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669021#M200648</guid>
      <dc:creator>ketpt42</dc:creator>
      <dc:date>2020-07-13T23:42:44Z</dc:date>
    </item>
    <item>
      <title>Re: Compare rows in the same table/dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669022#M200649</link>
      <description>&lt;P&gt;&lt;FONT size="2"&gt;Yes.&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;Expected output would be&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;Row&amp;nbsp; &amp;nbsp;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SITE&amp;nbsp; &amp;nbsp; END DATE&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1100&amp;nbsp; &amp;nbsp;AAA&amp;nbsp; &amp;nbsp; &amp;nbsp; BLANK&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1100&amp;nbsp; BBB&amp;nbsp; &amp;nbsp; &amp;nbsp; BLANK&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1200&amp;nbsp; AAA&amp;nbsp; &amp;nbsp; &amp;nbsp; 13JUN2020&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="3"&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1200 CCC&amp;nbsp; &amp;nbsp; &amp;nbsp; 13JUN2020&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jul 2020 23:50:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669022#M200649</guid>
      <dc:creator>lmclaughlin10</dc:creator>
      <dc:date>2020-07-13T23:50:03Z</dc:date>
    </item>
    <item>
      <title>Re: Compare rows in the same table/dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669026#M200652</link>
      <description>&lt;P&gt;Hmm. That really didn't clear it up for me. I think I need to know:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;What the "date" is in your logic below?&lt;/LI&gt;
&lt;LI&gt;What you mean by "xxx"?&lt;/LI&gt;
&lt;LI&gt;Confirmation that you do not consider a missing end date as being less than "date".&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;Read row 1, if end date less than date (specific field in dataset) then xxx, read new row, if ID, SITE equal ID, SITE in row 1, read next row, else perform evaluation.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jul 2020 00:15:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669026#M200652</guid>
      <dc:creator>ketpt42</dc:creator>
      <dc:date>2020-07-14T00:15:10Z</dc:date>
    </item>
    <item>
      <title>Re: Compare rows in the same table/dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669080#M200671</link>
      <description>&lt;P&gt;BY group processing in SAS makes this fairly easy.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
input ROW     ID  $         SITE  $                ENDDATE :$9. ;
datalines;
1          1100         AAA                  BLANK
2          1100         AAA                  12JUN2020
3          1100         BBB                  BLANK
4          1200         AAA                  13JUN2020
5          1200         CCC                  13JUN2020
;

/* next step assumes the data set have is sorted by ID and Site*/

data want;
   set have;
   by id site;
   if first.site then result='do evaluation';
   else result = 'no evaluation';
run;
&lt;/PRE&gt;
&lt;P&gt;When a data set is processed with BY groups then SAS creates automatic variables First. and Last. (note the dot is important!) for each variable on the By statement. These are boolean (True/False or numerically 1/0).&lt;/P&gt;
&lt;P&gt;So in the above if the Site value is the first site it refers to within values of the ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note that the preferred way to share data is a data step. That answers lots of questions such as actual variable names, types, lengths and values. Paste code into a code box opened on the forum with either the &amp;lt;/&amp;gt; or "running man" icons to preserve formatting. Otherwise the message windows will reformat your code and may introduce invisible HTML characters that mean the code may not run.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jul 2020 03:42:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669080#M200671</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-07-14T03:42:25Z</dc:date>
    </item>
    <item>
      <title>Re: Compare rows in the same table/dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669096#M200679</link>
      <description>&lt;P&gt;You refer to this:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/329128"&gt;@lmclaughlin10&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;(New SAS user)&lt;/P&gt;
&lt;P&gt;date (specific field in dataset)&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;which is not in your rough dataset.&lt;/P&gt;
&lt;P&gt;Please supply your "have" dataset in this way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines truncover;
input ID $ SITE $ end_date :date9.;
format end_date yymmddd10.;
datalines;
1100 AAA
1100 AAA 12JUN2020
1100 BBB
1200 AAA 13JUN2020
1200 CCC 13JUN2020
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and include all variables that are relevant to your conditions and calculations.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jul 2020 06:29:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669096#M200679</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-14T06:29:37Z</dc:date>
    </item>
    <item>
      <title>Re: Compare rows in the same table/dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669156#M200698</link>
      <description>&lt;P&gt;The evaluation part is if date is less cut off date, then before, else after. ( case when end_date &amp;lt; cut_off_date then "before", else "after" end) as cutoff&lt;/P&gt;&lt;P&gt;The actual table i'm reading has many fields.&amp;nbsp; These are only fields I need to used in my query/sql.&amp;nbsp; Once I understand how to obtain the results needed, I can then add in the evaluation part.&amp;nbsp; Even if it's again a temporary work file.&amp;nbsp; The solution may be simple. I maybe overthinking it.&amp;nbsp; It may be as simple a removing duplicates.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jul 2020 13:32:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669156#M200698</guid>
      <dc:creator>lmclaughlin10</dc:creator>
      <dc:date>2020-07-14T13:32:13Z</dc:date>
    </item>
    <item>
      <title>Re: Compare rows in the same table/dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669171#M200701</link>
      <description>&lt;P&gt;&lt;STRONG&gt;There is no column cut_off_date in your dataset.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Please supply your dataset as a data step with datalines, as requested, and if you need to use a value that is not part of the dataset, but derived from somewhere else (and maybe stored in a macro variable), then say so.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jul 2020 14:29:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669171#M200701</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-14T14:29:06Z</dc:date>
    </item>
    <item>
      <title>Re: Compare rows in the same table/dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669217#M200725</link>
      <description>Thank Kurt for your feed back. The cut_off_date varies by site and other data within the overall dataset. Again, that part of the evaluation I could handle. However as mentioned in my last post, I probable was overthinking it. Using the NODUPKEY function returned me the results I needed.&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Tue, 14 Jul 2020 16:15:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-rows-in-the-same-table-dataset/m-p/669217#M200725</guid>
      <dc:creator>lmclaughlin10</dc:creator>
      <dc:date>2020-07-14T16:15:27Z</dc:date>
    </item>
  </channel>
</rss>

