<?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 first and last observation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669406#M200812</link>
    <description>&lt;P&gt;That is very valuable to know. Thank you once again. Really appreciate your help!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 15 Jul 2020 11:01:11 GMT</pubDate>
    <dc:creator>MadMax</dc:creator>
    <dc:date>2020-07-15T11:01:11Z</dc:date>
    <item>
      <title>compare first and last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669361#M200788</link>
      <description>&lt;P&gt;Good Day,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a problem which I am sure there is a simple solution to.&amp;nbsp; I have a dataset with&amp;nbsp; 2 rows per customer&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Account &amp;nbsp; &amp;nbsp; &amp;nbsp; Month &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Flag&lt;/P&gt;&lt;P&gt;CustA &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; April &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; C&lt;/P&gt;&lt;P&gt;&lt;SPAN style="display: inline !important; float: none; background-color: #ffffff; color: #333333; cursor: text; font-family: inherit; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;CustA &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; May &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; C&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;CustB &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Nov &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C&lt;/P&gt;&lt;P&gt;CustB &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Dec &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; O&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the above example what I am lloking for is writing code to basically say:&amp;nbsp;&lt;/P&gt;&lt;P&gt;If your first observation for the customer is "C" and your last is also "C" then&amp;nbsp; indicator = "PASS"&lt;/P&gt;&lt;P&gt;but if your first observation of the flag is "C" and your last observation is "O" then your indicator = "FAIL"&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the result should look like this&amp;nbsp;&lt;/P&gt;&lt;P&gt;Account &amp;nbsp; &amp;nbsp; &amp;nbsp; Month &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Flag &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; Indicator&lt;/P&gt;&lt;P&gt;CustA &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; April &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; C &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; PASS&lt;/P&gt;&lt;P&gt;&lt;SPAN style="background-color: #ffffff; box-sizing: border-box; color: #333333; cursor: text; display: inline; float: none; font-family: inherit; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 300; letter-spacing: normal; line-height: 1.7142; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;CustA &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; May &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; C&lt;/SPAN&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; PASS&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;CustB &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Nov &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; FAIL&lt;/P&gt;&lt;P&gt;CustB &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Dec &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; O &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; FAIL&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 05:20:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669361#M200788</guid>
      <dc:creator>MadMax</dc:creator>
      <dc:date>2020-07-15T05:20:12Z</dc:date>
    </item>
    <item>
      <title>Re: compare first and last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669364#M200789</link>
      <description>&lt;P&gt;1) Can it be a customer with only one observation? or more than 2 observation?&lt;BR /&gt;&amp;nbsp; &amp;nbsp; In such case next code need adaption.&lt;/P&gt;
&lt;P&gt;2) I assume that ACCOUNT is the customer ID in your data, and that the dataset&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; is already sorted by Accout &amp;amp; Month, then:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
 set have;
      by account;
           retain first_flag;
           if first.account then first_flag = flag;
           else do;
                   if first_flag = 'C' and flag='C' then indicator='PASS;
                   else indicator = 'FAIL';
                  output;
          end;
          keep account indicator;
run;
data want;
merge have indicator;
  by account;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jul 2020 06:04:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669364#M200789</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-07-15T06:04:25Z</dc:date>
    </item>
    <item>
      <title>Re: compare first and last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669372#M200794</link>
      <description>&lt;P&gt;Thank you Shmuel,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I really appreciate your time.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For some reason all of the indicators come back as "FAIL" .&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data is already sorted by account and month's Descending.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the first.observation of the account's flag is "C" which means closed, and the previous month observation (last observation) is open, it means it should FAIL as you cant have a closed invoice this month if last month is still open.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If this month is closed , last month should also be closed (PASS)&amp;nbsp;&lt;/P&gt;&lt;P&gt;even is this month is "O" (Open) last month may be "C" (Closed) this too is a "PASS"&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 07:15:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669372#M200794</guid>
      <dc:creator>MadMax</dc:creator>
      <dc:date>2020-07-15T07:15:47Z</dc:date>
    </item>
    <item>
      <title>Re: compare first and last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669383#M200801</link>
      <description>&lt;P&gt;Do you only need to compare for sameness, or&amp;nbsp; is it necessary that both first and last observations have the specific value "C"?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 08:06:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669383#M200801</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-15T08:06:28Z</dc:date>
    </item>
    <item>
      <title>Re: compare first and last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669384#M200802</link>
      <description>&lt;P&gt;Anyway, use a double DO loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until (last.account);
  set have;
  by account;
  if first.account then _flag = flag;
end;
if flag = _flag
then indicator = 'PASS';
else indicator = 'FAIL';
do until (last.account);
  set have;
  by account;
  output;
end;
drop _flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You may need to adapt the condition in the IF.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 08:11:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669384#M200802</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-15T08:11:46Z</dc:date>
    </item>
    <item>
      <title>Re: compare first and last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669386#M200804</link>
      <description>&lt;P&gt;Next is a tested code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   input Account $ Month $ Flag  $;
cards;
CustA April C           
CustA May   C             
CustB Nov   C             
CustB Dec   O        
; run;
data temp;
 set have;
      by account;
           retain first_flag;
           if first.account then first_flag = flag;
           else do;
                   if first_flag = 'C' and flag='C' then indicator='PASS';
                   else indicator = 'FAIL';
                  output;
          end;
          keep account indicator;
run;
data want;
merge have temp;
  by account;
run;    
/**** result:

Obs	Account	Month	Flag	indicator
1	CustA	April	C	PASS
2	CustA	May	C	PASS
3	CustB	Nov	C	FAIL
4	CustB	Dec	O	FAIL
****/&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jul 2020 09:39:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669386#M200804</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-07-15T09:39:27Z</dc:date>
    </item>
    <item>
      <title>Re: compare first and last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669392#M200807</link>
      <description>&lt;P&gt;&amp;nbsp;Thank you so much!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have made a small change to your code but it is something you could not have catered for as you had limited view of the data I work with.&amp;nbsp; It is working perfectly now.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 10:20:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669392#M200807</guid>
      <dc:creator>MadMax</dc:creator>
      <dc:date>2020-07-15T10:20:38Z</dc:date>
    </item>
    <item>
      <title>Re: compare first and last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669397#M200809</link>
      <description>&lt;P&gt;The nice feature of the double DO loop is the fact that, physically, it requires only one reading pass through the input dataset, so it will usually be faster than a SQL with auto-remerge, or a sequence of steps with a merge-back.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 10:31:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669397#M200809</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-15T10:31:58Z</dc:date>
    </item>
    <item>
      <title>Re: compare first and last observation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669406#M200812</link>
      <description>&lt;P&gt;That is very valuable to know. Thank you once again. Really appreciate your help!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 11:01:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compare-first-and-last-observation/m-p/669406#M200812</guid>
      <dc:creator>MadMax</dc:creator>
      <dc:date>2020-07-15T11:01:11Z</dc:date>
    </item>
  </channel>
</rss>

