<?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: Use of lag() function and related issues in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-of-lag-function-and-related-issues/m-p/699576#M214003</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/221836"&gt;@The_Analyst&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Your solution worked!&amp;nbsp; I had tried to use retain at first, but had obviously applied it incorrectly.&amp;nbsp; I believe I was likely retaining the wrong variable.&amp;nbsp; That was when I went to lag() which is a dead end for this problem.&amp;nbsp; Your solution is so beautiful and simple as to warm the heart of a SAS programmer.&amp;nbsp; My sincere thanks for your expertise!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Without seeing the Retain code that didn't work it is hard to tell. However, if you use Retain with a variable that exists in a data set on the Set statement it gets reset to the value of the variable that already exists the next time a record is brought in from the data set. So it appears that retain doesn't do anything.&lt;/P&gt;</description>
    <pubDate>Tue, 17 Nov 2020 17:48:49 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-11-17T17:48:49Z</dc:date>
    <item>
      <title>Use of lag() function and related issues</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-lag-function-and-related-issues/m-p/699319#M213932</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;This is my first posting, so apologies if I fail in to provide sufficient info.&amp;nbsp; The parcel IDs and property types have been changed for presentation here for the sake of data privacy.&lt;/P&gt;&lt;P&gt;Here is the background.&amp;nbsp; There are parcels with distinctive parcel IDs.&amp;nbsp; A parcel can have more than one property type, such as agricultural homestead, agricultural production fields, or other types.&amp;nbsp; Some parcels can have as many as 6 property types.&lt;/P&gt;&lt;P&gt;I have data that has each property type, so the parcel IDs are replicated.&amp;nbsp; I've enclosed an Excel version (so the data be changed to maintain privacy) that shows how the data is arranged in the case of one parcel with 5 property types:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Parcel_ID&lt;/TD&gt;&lt;TD&gt;Property_Type_Code&lt;/TD&gt;&lt;TD&gt;Property_Code_Quantity&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;091-080-018100&lt;/TD&gt;&lt;TD&gt;X21&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;091-080-018100&lt;/TD&gt;&lt;TD&gt;X22&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;091-080-018100&lt;/TD&gt;&lt;TD&gt;C45&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;091-080-018100&lt;/TD&gt;&lt;TD&gt;Y10&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;091-080-018100&lt;/TD&gt;&lt;TD&gt;Y20&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to create a SAS program that does this in creating a string with all the property types:&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Parcel_ID&lt;/TD&gt;&lt;TD&gt;Property_Type_Code&lt;/TD&gt;&lt;TD&gt;Property_Code_Quantity&lt;/TD&gt;&lt;TD&gt;Property_Types&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;091-080-018100&lt;/TD&gt;&lt;TD&gt;X21&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;X21&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;091-080-018100&lt;/TD&gt;&lt;TD&gt;X22&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;X21 X22&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;091-080-018100&lt;/TD&gt;&lt;TD&gt;C45&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;X21 X22 C45&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;091-080-018100&lt;/TD&gt;&lt;TD&gt;Y10&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;X21 X22 C45 Y10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;091-080-018100&lt;/TD&gt;&lt;TD&gt;Y20&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;X21 X22 C45 Y10 Y20&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;Then I can select the last.Parcel_ID and there will be all the property types in the data for that parcel ID.&lt;/P&gt;&lt;P&gt;Here is a complication:&amp;nbsp; sometimes the number of property types present in the data does not always agree with the types presented.&amp;nbsp; There might be a total of 6 property types, but 5 or less are showing, since some are not selected for in creating the data.&amp;nbsp; For example,&amp;nbsp; property types that begin with "M" might have been excluded from the data.&amp;nbsp; There may 6 property types, but only 5 are listed in the data.&lt;/P&gt;&lt;P&gt;I tried with the lag() function, and believe it is the key, but my solution so far is not elegant and gets more complicated with each circumstance and is getting ugly.&amp;nbsp; I can do this OK if there are 1 or 2 or 3 property types on a parcel.&amp;nbsp; When there are 3 property types but only two records present, this breaks down.&amp;nbsp; After the code, I will show how it breaks down when run:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data work.Joined_Data_6;&lt;BR /&gt;set Run_Data.Joined_Data_2;&lt;BR /&gt;by Parcel_ID;&lt;BR /&gt;length Property_Codes $20.;&lt;BR /&gt;Previous_Type_1 = lag1(Property_Type_Code);&lt;BR /&gt;Previous_Type_2 = lag2(Property_Type_Code);&lt;BR /&gt;Previous_Type_3 = lag3(Property_Type_Code);&lt;BR /&gt;if (first.Parcel_ID) then Property_Codes = Property_Type_Code;&lt;BR /&gt;else if ((NOT first.Parcel_ID) and Property_Type_Code_Quantity = 2) then Property_Codes = catx(' ', Previous_Type_1, Property_Type_Code);&lt;BR /&gt;else if ((NOT first.Parcel_ID) and (NOT last.Parcel_ID) and Property_Type_Code_Quantity = 3) then Property_Codes = catx(' ', Previous_Type_1, Property_Type_Code);&lt;BR /&gt;else if ((NOT first.Parcel_ID) and (last.Parcel_ID) and Property_Type_Code_Quantity = 3) then Property_Codes = catx(' ', Previous_Type_2, Previous_Type_1, Property_Type_Code);&lt;BR /&gt;run;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Parcel_ID&lt;/TD&gt;&lt;TD&gt;Property_Type_Code&lt;/TD&gt;&lt;TD&gt;Property_Type_Code_Quantity&lt;/TD&gt;&lt;TD&gt;Property_Codes&lt;/TD&gt;&lt;TD&gt;Previous_Type_1&lt;/TD&gt;&lt;TD&gt;Previous_Type_2&lt;/TD&gt;&lt;TD&gt;Previous_Type_3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01-03-912200&lt;/TD&gt;&lt;TD&gt;P45&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;P45&lt;/TD&gt;&lt;TD&gt;F56&lt;/TD&gt;&lt;TD&gt;R56&lt;/TD&gt;&lt;TD&gt;R54&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01-03-912200&lt;/TD&gt;&lt;TD&gt;Q50&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;F56 P45 Q50&lt;/TD&gt;&lt;TD&gt;P45&lt;/TD&gt;&lt;TD&gt;F56&lt;/TD&gt;&lt;TD&gt;R56&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've spent a lot of time on this and am frustrated--there has to be a robust solution out there.&amp;nbsp; I am using the latest version of SAS and EG.&amp;nbsp; Again, my sincere apologies if I have given an incomplete description.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your consideration.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 22:16:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-lag-function-and-related-issues/m-p/699319#M213932</guid>
      <dc:creator>The_Analyst</dc:creator>
      <dc:date>2020-11-16T22:16:35Z</dc:date>
    </item>
    <item>
      <title>Re: Use of lag() function and related issues</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-lag-function-and-related-issues/m-p/699321#M213933</link>
      <description>&lt;P&gt;This is actually a better case for RETAIN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data work.Joined_Data_6;
   set Run_Data.Joined_Data_2;
   by Parcel_ID;
   length Property_Codes $20.;
   retain Property_codes;
   if first.Parcel_id then Property_codes= Property_type_code;
   else Property_codes = catx(' ',Property_codes,Property_type_code);
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 22:12:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-lag-function-and-related-issues/m-p/699321#M213933</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-11-16T22:12:49Z</dc:date>
    </item>
    <item>
      <title>Re: Use of lag() function and related issues</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-lag-function-and-related-issues/m-p/699325#M213935</link>
      <description>&lt;P&gt;Your solution worked!&amp;nbsp; I had tried to use retain at first, but had obviously applied it incorrectly.&amp;nbsp; I believe I was likely retaining the wrong variable.&amp;nbsp; That was when I went to lag() which is a dead end for this problem.&amp;nbsp; Your solution is so beautiful and simple as to warm the heart of a SAS programmer.&amp;nbsp; My sincere thanks for your expertise!&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 22:29:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-lag-function-and-related-issues/m-p/699325#M213935</guid>
      <dc:creator>The_Analyst</dc:creator>
      <dc:date>2020-11-16T22:29:46Z</dc:date>
    </item>
    <item>
      <title>Re: Use of lag() function and related issues</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-lag-function-and-related-issues/m-p/699576#M214003</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/221836"&gt;@The_Analyst&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Your solution worked!&amp;nbsp; I had tried to use retain at first, but had obviously applied it incorrectly.&amp;nbsp; I believe I was likely retaining the wrong variable.&amp;nbsp; That was when I went to lag() which is a dead end for this problem.&amp;nbsp; Your solution is so beautiful and simple as to warm the heart of a SAS programmer.&amp;nbsp; My sincere thanks for your expertise!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Without seeing the Retain code that didn't work it is hard to tell. However, if you use Retain with a variable that exists in a data set on the Set statement it gets reset to the value of the variable that already exists the next time a record is brought in from the data set. So it appears that retain doesn't do anything.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Nov 2020 17:48:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-lag-function-and-related-issues/m-p/699576#M214003</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-11-17T17:48:49Z</dc:date>
    </item>
  </channel>
</rss>

