<?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: backfill some observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325517#M72397</link>
    <description>&lt;P&gt;Slight variation, change the 2 to whatever number you want. Idea is basic, add a counter to determine how many records you've backfilled and then use that in your condition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input year firm  $   value;
    cards;
2000     A          1
2001     A          .
2002     A          .
2003     A          .
2004     A          9
2000     B          2
2001     B          .
2002     B          3
2003     B          .
2004     B          .
2005     B          2
;
run;

proc sort data=have;
    by firm descending year;
run;



data want;
    set have;
    by firm;
    retain new_value backfill;

    if first.firm then do;
       backfill=0;
        new_value=.;
    end;

    if value=. then do;
        backfill+1;
        if backfill &amp;gt; 2 then new_value=.;
    end;
    else do; 
        backfill=0;
        new_value=value;
    end;
run;

proc sort data=want;
    by firm year;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 17 Jan 2017 23:51:23 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-01-17T23:51:23Z</dc:date>
    <item>
      <title>backfill some observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325170#M72295</link>
      <description>&lt;P&gt;There are missing values in my data set, and I want to backfill some missing values. See the example below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;year &amp;nbsp; &amp;nbsp; firm &amp;nbsp; &amp;nbsp; value&lt;/P&gt;
&lt;P&gt;2000 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2001 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2002 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2003 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2000 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2001 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2002 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2003 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I only want to backfill ONE year, after manipulation, I would like to get the results below.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;year &amp;nbsp; &amp;nbsp; firm &amp;nbsp; &amp;nbsp; value&lt;/P&gt;
&lt;P&gt;2000 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2001 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2002 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2003 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2000 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2001 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2002 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2003 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I actually know how to backfill values, but it will backfill all missing values. I want to know how to apply the criterion that only one year's&amp;nbsp;data will be backfilled. Can any one help me with this? Thanks.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2017 03:55:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325170#M72295</guid>
      <dc:creator>SeanZ</dc:creator>
      <dc:date>2017-01-17T03:55:18Z</dc:date>
    </item>
    <item>
      <title>Re: backfill some observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325175#M72298</link>
      <description>&lt;P&gt;In a roundabout way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input year firm  $   value;
    cards;
2000     A          1
2001     A          .
2002     A          .
2003     A          9
2000     B          2
2001     B          .
2002     B          3
2003     B          1
;
run;

proc sort data=have;
    by firm descending year;
run;

data want;
    set have;
    by firm;
    prev_value=lag(value);

    if first.firm then
        prev_value=.;

    if value=. then
        new_value=prev_value;
    else
        new_value=value;
run;

proc sort data=want;
    by firm year;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Jan 2017 04:35:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325175#M72298</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-01-17T04:35:04Z</dc:date>
    </item>
    <item>
      <title>Re: backfill some observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325177#M72300</link>
      <description>&lt;P&gt;Or use SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
create table want as
select a.year, a.firm, coalesce(a.value, b.value) as value
from have as a left join
    have as b on a.firm=b.firm and a.year+1 = b.year
order by firm, year;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Jan 2017 05:10:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325177#M72300</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-01-17T05:10:46Z</dc:date>
    </item>
    <item>
      <title>Re: backfill some observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325183#M72301</link>
      <description>Thanks for the solution.&amp;nbsp; I like this solution very much. But how to extend this to the case that I want to backfill 3 years instead if one?&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;&lt;BR /&gt;Sean&lt;BR /&gt;</description>
      <pubDate>Tue, 17 Jan 2017 06:30:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325183#M72301</guid>
      <dc:creator>SeanZ</dc:creator>
      <dc:date>2017-01-17T06:30:50Z</dc:date>
    </item>
    <item>
      <title>Re: backfill some observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325184#M72302</link>
      <description>&lt;P&gt;Assuming you don't want to backfill across firms, here's one way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by firm;&lt;/P&gt;
&lt;P&gt;set have (keep=value rename=(value=next_value) firstobs=2) have (drop=_all_);&lt;/P&gt;
&lt;P&gt;if last.firm=0 and value=. then value = next_value;&lt;/P&gt;
&lt;P&gt;drop next_value;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It does replace all missing values. &amp;nbsp;However, if there are two missing values in a row, the replacement value (for the first one) will also be a missing value.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2017 07:08:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325184#M72302</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-17T07:08:43Z</dc:date>
    </item>
    <item>
      <title>Re: backfill some observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325307#M72345</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/52375"&gt;@SeanZ&lt;/a&gt; wrote:&lt;BR /&gt;Thanks for the solution.&amp;nbsp; I like this solution very much. But how to extend this to the case that I want to backfill 3 years instead if one?&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;&lt;BR /&gt;Sean&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Is your requirement specific numbers of years or "all but the first missing"?&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2017 15:47:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325307#M72345</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-01-17T15:47:40Z</dc:date>
    </item>
    <item>
      <title>Re: backfill some observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325386#M72365</link>
      <description>&lt;P&gt;I will define the number of years to fill for missing values.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2017 18:11:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325386#M72365</guid>
      <dc:creator>SeanZ</dc:creator>
      <dc:date>2017-01-17T18:11:30Z</dc:date>
    </item>
    <item>
      <title>Re: backfill some observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325487#M72387</link>
      <description>&lt;P&gt;Although the SQL method can be extended to fill more than one year, it will not scale well to an arbitrary number of years. The same can be said for most methods presented so far, except for&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;'s reverse sort approach.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2017 21:33:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325487#M72387</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-01-17T21:33:28Z</dc:date>
    </item>
    <item>
      <title>Re: backfill some observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325502#M72394</link>
      <description>&lt;P&gt;Thank you. But how can this way apply the year screening criterion? I only want to backfill three years of miss data.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2017 22:22:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325502#M72394</guid>
      <dc:creator>SeanZ</dc:creator>
      <dc:date>2017-01-17T22:22:44Z</dc:date>
    </item>
    <item>
      <title>Re: backfill some observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325503#M72395</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/52375"&gt;@SeanZ&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thank you. But how can this way apply the year screening criterion? I only want to backfill three years of miss data.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then why did you ask for one year?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please post data that reflects your new question.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2017 22:27:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325503#M72395</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-01-17T22:27:12Z</dc:date>
    </item>
    <item>
      <title>Re: backfill some observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325507#M72396</link>
      <description>&lt;P&gt;Thank you for your reply. I paste a new data set to reflect my question. I would like to backfill N years of missing data. In this case, I would like to set N=2. Eventually, I hope to control and set different N.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;year &amp;nbsp; &amp;nbsp; firm &amp;nbsp; &amp;nbsp; value&lt;/P&gt;
&lt;P&gt;2000 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2001 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2002 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2003 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2004 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2000 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2001 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2002 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2003 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2004 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2005 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The data I want&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;year &amp;nbsp; &amp;nbsp; firm &amp;nbsp; &amp;nbsp; value&lt;/P&gt;
&lt;P&gt;2000 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2001 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2002 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2003 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2004 &amp;nbsp; &amp;nbsp; A &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;9&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2000 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2001 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2002 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2003 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2004 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2005 &amp;nbsp; &amp;nbsp; B &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2017 23:02:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325507#M72396</guid>
      <dc:creator>SeanZ</dc:creator>
      <dc:date>2017-01-17T23:02:37Z</dc:date>
    </item>
    <item>
      <title>Re: backfill some observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325517#M72397</link>
      <description>&lt;P&gt;Slight variation, change the 2 to whatever number you want. Idea is basic, add a counter to determine how many records you've backfilled and then use that in your condition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input year firm  $   value;
    cards;
2000     A          1
2001     A          .
2002     A          .
2003     A          .
2004     A          9
2000     B          2
2001     B          .
2002     B          3
2003     B          .
2004     B          .
2005     B          2
;
run;

proc sort data=have;
    by firm descending year;
run;



data want;
    set have;
    by firm;
    retain new_value backfill;

    if first.firm then do;
       backfill=0;
        new_value=.;
    end;

    if value=. then do;
        backfill+1;
        if backfill &amp;gt; 2 then new_value=.;
    end;
    else do; 
        backfill=0;
        new_value=value;
    end;
run;

proc sort data=want;
    by firm year;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Jan 2017 23:51:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325517#M72397</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-01-17T23:51:23Z</dc:date>
    </item>
    <item>
      <title>Re: backfill some observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325535#M72401</link>
      <description>&lt;P&gt;More specifically (but untested), it looks like you could take Reeza's approach and make minor changes. &amp;nbsp;First, indicate how many years you are willing to backfill:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let n=2;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then change one line in the middle. &amp;nbsp;Currently, the code contains:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if backfill &amp;gt; 2 then new_value = .;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Change that to read:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if backfill &amp;lt;= &amp;amp;N then value = new_value;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not sure if Reeza intended that the proper result would be in VALUE or in NEW_VALUE. &amp;nbsp;In this modification, the proper value is VALUE, and you can drop NEW_VALUE.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2017 01:40:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325535#M72401</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-18T01:40:50Z</dc:date>
    </item>
    <item>
      <title>Re: backfill some observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325558#M72405</link>
      <description>&lt;P&gt;Here is my take on it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input year firm $ value;
datalines;
2000     A          1
2001     A          .
2002     A          .
2003     A          .
2004     A          9
2000     B          2
2001     B          .
2002     B          3
2003     B          .
2004     B          .
2005     B          2
;

proc sort data=have; by firm descending year; run;

%let n=2; /* The number of years to fill in */

data want(sortedby=firm descending year);
y = 99999;
do until(last.firm);
    set have; by firm;
    if missing(value) then do;
        if year + &amp;amp;n. &amp;gt;= y then value = v; 
        end;
    else do;
        y = year;
        v = value;
        end;
    output;
    end;
drop v y;
run;

proc sort data=want; by firm year; run;

proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that by using the year in the replacement condition, omitted or duplicated years should be handled properly. The sortedby= dataset option is an attempt to improve the efficiency of the last sort (I don't know if it makes a real difference).&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jan 2017 04:24:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/backfill-some-observations/m-p/325558#M72405</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2017-01-18T04:24:22Z</dc:date>
    </item>
  </channel>
</rss>

