<?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: Retain to keep latest record in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644511#M192512</link>
    <description>&lt;P&gt;Thank you Sir &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp; That's the reason my professors at DePaul University, Chicago where I graduated had regarded me among the many students who will earnestly "listen and pay attention" to elders whom I value. (smiles)&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Proud of it!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With the above being said, I owe you SAS veterans my sincere gratitude in getting me up-to speed with all the necessary instructions and guidance when I needed the most. I have nicknames for each one of you that my friends and I had a laugh with our fun SAS community stories at the Exchequer pub right opposite to the lab. Hahaha.&amp;nbsp; I miss those days and Chicago.&amp;nbsp; Have a good day&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 01 May 2020 13:31:35 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-05-01T13:31:35Z</dc:date>
    <item>
      <title>Retain to keep latest record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644438#M192479</link>
      <description>&lt;P&gt;Hi everyone, I am looking to use the retain statement to keep the latest code for each customer. I have come up with this code below but it is not working and I am getting stuck. my code is below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data fix_code;
set lkup2;
retain r_code cust;
by customer_id;
if cust ne customer_id then do;
if not missing(code) then r_code = code;
else code = r_code;
end;
r_code = code;
cust = customer_id;
run;&lt;/PRE&gt;&lt;P&gt;I have attached a excel sheet with what I have and what I am wanting to achieve.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 May 2020 02:19:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644438#M192479</guid>
      <dc:creator>Scott86</dc:creator>
      <dc:date>2020-05-01T02:19:40Z</dc:date>
    </item>
    <item>
      <title>Re: Retain to keep latest record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644449#M192482</link>
      <description>&lt;P&gt;Multistep approach? Keep only last record and merge in? Retain carries records forward (down the column), in this case though your records are 'ahead' so you're trying to carry records backwards (up the column).&amp;nbsp;&lt;BR /&gt;This is a simple approach though there's like a hash solution&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
set have;
by customer_ID;
if last.customer_ID;
run;

data want;
merge have temp (rename= (code = last_code));
by customer_ID;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 May 2020 03:19:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644449#M192482</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-05-01T03:19:16Z</dc:date>
    </item>
    <item>
      <title>Re: Retain to keep latest record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644451#M192483</link>
      <description>&lt;P&gt;You can use DOW loop processing to capture the final code, and a second loop to apply it.&lt;/P&gt;
&lt;PRE&gt;data want;
  do _n_ = 1 by 1 until (last.customer_id);  /* repurpose _n_ */
    set have;
    by customer_id;
    last_code = code;   /* capture code */
  end;
  * at end of above loop last capture is last code in group;

  * use repurposed _n_ to iterate over the rows of the group;
  do _n_ = 1 to _n_;
    set have;                * second read buffer;
    code = last_code;  * apply captured value, overwriting original;
    OUTPUT;               * output one row per row of original data;
  end;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 May 2020 03:23:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644451#M192483</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-05-01T03:23:04Z</dc:date>
    </item>
    <item>
      <title>Re: Retain to keep latest record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644468#M192494</link>
      <description>&lt;P&gt;The idea is sound but needs a little bit of clean-up:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until (last.customer_id);
    set have;
    by customer_id;
    if not missing(code) then last_code = code; 
  end;
  * at end of above loop last capture is last non-missing code in group;

  do until (last.customer_id);
    set have;                * second read buffer;
    by customer_id;
    code = last_code;  * apply captured value, overwriting original;
    OUTPUT;               * output one row per row of original data;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 May 2020 08:21:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644468#M192494</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-05-01T08:21:00Z</dc:date>
    </item>
    <item>
      <title>Re: Retain to keep latest record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644500#M192505</link>
      <description>&lt;P&gt;Good morning everyone, My approach would be is to interleave and output the IN=b &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input Customer_id	Code;
cards;
1	.
1	23
2	.
2	1666
2	4356
3	.
3	345
4	.
4	24
;

data want;
 set have(in=a) have(in=b);
 by customer_id;
 retain _code;
 if a then _code=code;
 if b;
 code=_code;
 drop _code;
run;
proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;Customer_id&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;Code&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;23&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;23&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;4356&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;4356&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;4356&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;345&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;345&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;24&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;24&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;, I dozed off last night after the proc sql fun and just noticed the mention. Thank you. And now in the AM dancing with inherited spaghetti code.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 May 2020 11:45:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644500#M192505</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-01T11:45:51Z</dc:date>
    </item>
    <item>
      <title>Re: Retain to keep latest record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644510#M192511</link>
      <description>&lt;P&gt;Really cool approach&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;.&amp;nbsp; I'm so used to the DOW-loop approach, would never have thought of interleaving.&lt;/P&gt;</description>
      <pubDate>Fri, 01 May 2020 13:14:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644510#M192511</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2020-05-01T13:14:57Z</dc:date>
    </item>
    <item>
      <title>Re: Retain to keep latest record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644511#M192512</link>
      <description>&lt;P&gt;Thank you Sir &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp; That's the reason my professors at DePaul University, Chicago where I graduated had regarded me among the many students who will earnestly "listen and pay attention" to elders whom I value. (smiles)&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Proud of it!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With the above being said, I owe you SAS veterans my sincere gratitude in getting me up-to speed with all the necessary instructions and guidance when I needed the most. I have nicknames for each one of you that my friends and I had a laugh with our fun SAS community stories at the Exchequer pub right opposite to the lab. Hahaha.&amp;nbsp; I miss those days and Chicago.&amp;nbsp; Have a good day&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 May 2020 13:31:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644511#M192512</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-01T13:31:35Z</dc:date>
    </item>
    <item>
      <title>Re: Retain to keep latest record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644516#M192515</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This is a simple approach though there's like a hash solution&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Sure, here's one of them:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
if _n_=1 then do;
  dcl hash h(dataset:'have(where=(code&amp;gt;.z))', duplicate:'r');
  h.definekey('customer_id');
  h.definedata('code');
  h.definedone();
end;
set have;
_n_=h.find();
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 01 May 2020 13:50:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644516#M192515</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-05-01T13:50:19Z</dc:date>
    </item>
    <item>
      <title>Re: Retain to keep latest record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644877#M192702</link>
      <description>&lt;P&gt;Thanks guys this worked&lt;/P&gt;</description>
      <pubDate>Sun, 03 May 2020 20:50:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-to-keep-latest-record/m-p/644877#M192702</guid>
      <dc:creator>Scott86</dc:creator>
      <dc:date>2020-05-03T20:50:12Z</dc:date>
    </item>
  </channel>
</rss>

