<?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: Replace NULL values with most recent value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485521#M126156</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Although your sample data does not show it, consider if you need to handle 2 or more&amp;nbsp;contiguous rows that have NULL in small_area.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 09 Aug 2018 16:25:52 GMT</pubDate>
    <dc:creator>Amir</dc:creator>
    <dc:date>2018-08-09T16:25:52Z</dc:date>
    <item>
      <title>Replace NULL values with most recent value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485507#M126148</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have data in the following format&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;policy_id,lst_renew_date,small_area&lt;/P&gt;&lt;P&gt;101,20180507,00011&lt;/P&gt;&lt;P&gt;102,20160508,00022&lt;/P&gt;&lt;P&gt;102,20170508,NULL&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;102,20180509,00022&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;103,20160510,00033&lt;/P&gt;&lt;P&gt;103,20170511,&lt;SPAN&gt;00033&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;103,20180511,NULL&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to clean the data so that every policy_id has a small_area code where possible.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How could I go about creating replacing the NULL values for small_area with the most recent small_area code for that that policy_id?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Finbar&lt;/P&gt;</description>
      <pubDate>Thu, 09 Aug 2018 15:07:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485507#M126148</guid>
      <dc:creator>finbar_gillen</dc:creator>
      <dc:date>2018-08-09T15:07:52Z</dc:date>
    </item>
    <item>
      <title>Re: Replace NULL values with most recent value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485509#M126150</link>
      <description>&lt;P&gt;You could use the LAG function to determine the previous value and insert it in place on NULL. Assumes the data is properly sorted by POLICY_ID.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UNTESTED CODE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    by policy_id;
    prev_small_area=lag(small_area);
    if not first.policy_id and small_area='NULL' then small_area=prev_small_area;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Aug 2018 15:10:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485509#M126150</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-08-09T15:10:48Z</dc:date>
    </item>
    <item>
      <title>Re: Replace NULL values with most recent value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485510#M126151</link>
      <description>Would I need to sort this by policy_id and lst_renew_date? As I want to select the most recent small_area.</description>
      <pubDate>Thu, 09 Aug 2018 15:15:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485510#M126151</guid>
      <dc:creator>finbar_gillen</dc:creator>
      <dc:date>2018-08-09T15:15:24Z</dc:date>
    </item>
    <item>
      <title>Re: Replace NULL values with most recent value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485512#M126153</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/225525"&gt;@finbar_gillen&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Would I need to sort this by policy_id and lst_renew_date? As I want to select the most recent small_area.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Sounds correct to me.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Aug 2018 15:22:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485512#M126153</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-08-09T15:22:41Z</dc:date>
    </item>
    <item>
      <title>Re: Replace NULL values with most recent value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485521#M126156</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Although your sample data does not show it, consider if you need to handle 2 or more&amp;nbsp;contiguous rows that have NULL in small_area.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Aug 2018 16:25:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485521#M126156</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2018-08-09T16:25:52Z</dc:date>
    </item>
    <item>
      <title>Re: Replace NULL values with most recent value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485682#M126218</link>
      <description>&lt;P&gt;Hi Amir,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yeah, I do have cases lke that. What I would need is for the code to look for the first instance where there is a non Null value.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could an if statement be added to the above code to ignore where the value is a null?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Finbar&lt;/P&gt;</description>
      <pubDate>Fri, 10 Aug 2018 08:20:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485682#M126218</guid>
      <dc:creator>finbar_gillen</dc:creator>
      <dc:date>2018-08-10T08:20:11Z</dc:date>
    </item>
    <item>
      <title>Re: Replace NULL values with most recent value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485692#M126225</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To assist, I have created a data step that anyone can run to create the test data, this includes some extra rows (policies 105-107) some with contiguous NULL values and one policy with no NULL values at all. The second data step process the data in the manner described so far, I believe.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   infile datalines dsd;

   input policy_id      : $3.
         lst_renew_date : yymmdd10.
         small_area     : $5.
   ;

   datalines;
101,20180507,00011
102,20160508,00022
102,20170508,NULL
102,20180509,00022
103,20160510,00033
103,20170511,00033
103,20180511,NULL
104,20170511,00044
104,20170512,NULL
104,20170513,NULL
104,20170514,NULL
104,20170515,00044
105,20170511,NULL
105,20170512,NULL
105,20170513,00055
105,20170514,NULL
106,20170511,NULL
106,20170512,NULL
107,20170511,00077
107,20170512,00077
;


data want;
   set have;
   by policy_id;

   retain saved_small_area '     ';

   if first.policy_id then
      /* at start of a policy group reset saved value */
      saved_small_area = small_area;
   else
      /* otherwise save a non-NULL value if possible */
      if     saved_small_area eq 'NULL'
         and small_area       ne 'NULL'
      then
         saved_small_area = small_area;

   /* replace a NULL small_area if possible */
   if     small_area       eq 'NULL'
      and saved_small_area ne 'NULL'
   then
      small_area = saved_small_area;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If there are more scenarios of data that need to be catered for then try to represent them all in a data step we can all run (like the first step above), otherwise we might just be making incorrect assumptions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HTH.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Aug 2018 09:53:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485692#M126225</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2018-08-10T09:53:45Z</dc:date>
    </item>
    <item>
      <title>Re: Replace NULL values with most recent value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485705#M126231</link>
      <description>&lt;P&gt;Hi Amir,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for that, I also found this link which seems to give me a good output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://stackoverflow.com/questions/42961591/filling-in-missing-values-with-forward-backward-method-with-lag-in-sas" target="_blank"&gt;https://stackoverflow.com/questions/42961591/filling-in-missing-values-with-forward-backward-method-with-lag-in-sas&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Finbar&lt;/P&gt;</description>
      <pubDate>Fri, 10 Aug 2018 10:41:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485705#M126231</guid>
      <dc:creator>finbar_gillen</dc:creator>
      <dc:date>2018-08-10T10:41:22Z</dc:date>
    </item>
    <item>
      <title>Re: Replace NULL values with most recent value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485721#M126236</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Well done on continuing your search, the best solution is not necessarily the first or second one.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes the solution looks workable (I haven't tested it myself) and if you don't know it, it introduces the DOW loop technique which can be very useful. I found the following useful in this regard:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="http://support.sas.com/resources/papers/proceedings09/038-2009.pdf" target="_blank"&gt;http://support.sas.com/resources/papers/proceedings09/038-2009.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would advise you compare the performance of any solutions you come across, as, for example, in the solution you provided a link to, the data is being read twice (there are two &lt;FONT face="courier new,courier"&gt;set&lt;/FONT&gt; statements).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Aug 2018 11:34:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-NULL-values-with-most-recent-value/m-p/485721#M126236</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2018-08-10T11:34:10Z</dc:date>
    </item>
  </channel>
</rss>

