<?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: Comparing a variable from one record to the previous record in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111444#M23084</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If last 'date' is the same as the current 'date', then flag=1, otherwise, flag=0. If the first record of each ChildID, reset flag to missing value:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;input (ChildID Disposition Date) (:$);&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;123 TPR 10/12/12&lt;/P&gt;&lt;P&gt;123 TPR 2/1/13&lt;/P&gt;&lt;P&gt;123 Surrender 4/1/13&lt;/P&gt;&lt;P&gt;456 TPR 2/1/13&lt;/P&gt;&lt;P&gt;456 TPR 2/1/13&lt;/P&gt;&lt;P&gt;456 Surrender 4/1/13&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by childid;&lt;/P&gt;&lt;P&gt;flag=ifn(first.childid, ., ifn(date=lag(date),1,0));&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Haikuo&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 17 Apr 2013 21:05:22 GMT</pubDate>
    <dc:creator>Haikuo</dc:creator>
    <dc:date>2013-04-17T21:05:22Z</dc:date>
    <item>
      <title>Comparing a variable from one record to the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111442#M23082</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;Is there a way to compare a variable across records for a given sub-set of records? In the below example I would like to see if the date variable in one record is the same as the same date in the previous record within each of child records.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Paul&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ChildID&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Disposition&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Date&lt;/P&gt;&lt;P&gt;123&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPR&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10/12/12&lt;/P&gt;&lt;P&gt;123&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPR&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2/1/13&lt;/P&gt;&lt;P&gt;123&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Surrender&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4/1/13&lt;/P&gt;&lt;P&gt;456&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPR&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10/12/12&lt;/P&gt;&lt;P&gt;456&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TPR&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2/1/13&lt;/P&gt;&lt;P&gt;456&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Surrender&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4/1/13&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 17 Apr 2013 20:51:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111442#M23082</guid>
      <dc:creator>Paul_NYS</dc:creator>
      <dc:date>2013-04-17T20:51:38Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing a variable from one record to the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111443#M23083</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can you explain more specifically what you're doing?&amp;nbsp; That dataset doesn't look very useful.&amp;nbsp; Are you saying if row 2 and row 1 have the same date, or if child 123 and child 456 have the same set of dates?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 17 Apr 2013 20:55:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111443#M23083</guid>
      <dc:creator>snoopy369</dc:creator>
      <dc:date>2013-04-17T20:55:51Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing a variable from one record to the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111444#M23084</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If last 'date' is the same as the current 'date', then flag=1, otherwise, flag=0. If the first record of each ChildID, reset flag to missing value:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data have;&lt;/P&gt;&lt;P&gt;input (ChildID Disposition Date) (:$);&lt;/P&gt;&lt;P&gt;cards;&lt;/P&gt;&lt;P&gt;123 TPR 10/12/12&lt;/P&gt;&lt;P&gt;123 TPR 2/1/13&lt;/P&gt;&lt;P&gt;123 Surrender 4/1/13&lt;/P&gt;&lt;P&gt;456 TPR 2/1/13&lt;/P&gt;&lt;P&gt;456 TPR 2/1/13&lt;/P&gt;&lt;P&gt;456 Surrender 4/1/13&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by childid;&lt;/P&gt;&lt;P&gt;flag=ifn(first.childid, ., ifn(date=lag(date),1,0));&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Haikuo&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 17 Apr 2013 21:05:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111444#M23084</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2013-04-17T21:05:22Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing a variable from one record to the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111445#M23085</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;data t;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set dt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by child disposition;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if first.disposition then seq=1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else seq+1;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc transpose data=t out=trans;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; by child disposition ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var date;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; id seq;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;now you will get al the date variables next to next variables so you can compare easily.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Apr 2013 03:17:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111445#M23085</guid>
      <dc:creator>Sudhakar_A</dc:creator>
      <dc:date>2013-04-18T03:17:11Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing a variable from one record to the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111446#M23086</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Haikuo,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Always remember the LAG function doesn't retrieve a value from the previous row in the dataset, but from the previous iteration of the LAG function.&amp;nbsp; Therefore, if you want a value from the previous row, don't make its execution conditional.&amp;nbsp; Try this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by childid;&lt;/P&gt;&lt;P&gt;&amp;nbsp; prevdate=lag(date);&lt;/P&gt;&lt;P&gt;&amp;nbsp; if first.childid then prevdate = . ;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if date eq prevdate then flag = 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; else flag = 0;&lt;/P&gt;&lt;P&gt;&amp;nbsp; drop prevdate;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Dave&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Apr 2013 03:26:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111446#M23086</guid>
      <dc:creator>DaveBirch</dc:creator>
      <dc:date>2013-04-18T03:26:28Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing a variable from one record to the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111447#M23087</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dave, while your warning about lag is a good one to keep in mind, IFN evaluates all three conditions each time it is called, so it isn't accurate in this instance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;That said, I prefer the transpose solution - more flexible.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Example of lag with IFN:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt; test;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;set&lt;/SPAN&gt; sashelp.class;&lt;/P&gt;&lt;P class="p1"&gt;x = ifn(sex=&lt;SPAN class="s3"&gt;'M'&lt;/SPAN&gt;,ifn(age&amp;lt;&lt;SPAN class="s4"&gt;&lt;STRONG&gt;13&lt;/STRONG&gt;&lt;/SPAN&gt;,lag(height),&lt;SPAN class="s4"&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/SPAN&gt;),&lt;SPAN class="s4"&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/SPAN&gt;);&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;put&lt;/SPAN&gt; x= age= sex= height=;&lt;/P&gt;&lt;P class="p2"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN class="s5"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="s5"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="s5"&gt;You can see the 'lagged' height is the immediately previous record, not the previous male/under 13 record, since each and every record triggers the call to lag (even within two ifn's).&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Apr 2013 03:44:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111447#M23087</guid>
      <dc:creator>snoopy369</dc:creator>
      <dc:date>2013-04-18T03:44:21Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing a variable from one record to the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111448#M23088</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The Dataset which you have posted is not containing any record as per your requirement...And whenever you post query, remember to attach output you want...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Based on your information, you wrote "I would like to see if the date variable in one record is the same as the same date in the previous record within each of child records"...In SAS, record means entire observation not a single object or variable...So rather than posting such a confusing statements try to include sample output that you want...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-Urvish&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 18 Apr 2013 06:35:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111448#M23088</guid>
      <dc:creator>UrvishShah</dc:creator>
      <dc:date>2013-04-18T06:35:32Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing a variable from one record to the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111449#M23089</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hash Table is a good choice.&lt;/P&gt;&lt;P&gt;What output result do you want.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 20 Apr 2013 09:35:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111449#M23089</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2013-04-20T09:35:14Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing a variable from one record to the previous record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111450#M23090</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Are we making a simple problem difficult?&amp;nbsp; Does this do what you want:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp; by child date;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if first.date then same='N';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; else same='Y';&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If this isn't what you want, you might have to give an example or two of what you are looking to get.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 20 Apr 2013 23:31:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-a-variable-from-one-record-to-the-previous-record/m-p/111450#M23090</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2013-04-20T23:31:57Z</dc:date>
    </item>
  </channel>
</rss>

