<?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 Filling down N # of observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Filling-down-N-of-observations/m-p/346729#M79984</link>
    <description>&lt;P&gt;Hello, I am trying to fill down a variable within a by-group&amp;nbsp;to replace missing values. However, when filling down I only want to fill at most 3 observations if a non-blank value is not seen.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For instance, I start with a data set&amp;nbsp;"have" below, and need it to turn out like "want." I can easily fill missing values by-group in all cases (see "test1" below), but can't quite figure out how to set a limit on the number of observations to fill down.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any suggestions on how to best accomplish this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Data have;&lt;BR /&gt; input id var1;&lt;BR /&gt; datalines;&lt;BR /&gt; 1 54&lt;BR /&gt; 1 60&lt;BR /&gt; 1 .&lt;BR /&gt; 1 .&lt;BR /&gt; 1 .&lt;BR /&gt; 1 .&lt;BR /&gt; 1 .&lt;BR /&gt; 2 100&lt;BR /&gt; 2 .&lt;BR /&gt; 2 .&lt;BR /&gt; 2 200&lt;BR /&gt; 2 .&lt;BR /&gt; 2 .&lt;BR /&gt; ;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;Data want;&lt;BR /&gt; input id var1;&lt;BR /&gt; datalines;&lt;BR /&gt; 1 54&lt;BR /&gt; 1 60&lt;BR /&gt; 1 60&lt;BR /&gt; 1 60&lt;BR /&gt; 1 60&lt;BR /&gt; 1 .&lt;BR /&gt; 1 .&lt;BR /&gt; 2 100&lt;BR /&gt; 2 100&lt;BR /&gt; 2 100&lt;BR /&gt; 2 200&lt;BR /&gt; 2 200&lt;BR /&gt; 2 200&lt;BR /&gt; ;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;Data test1;&lt;BR /&gt; set have;&lt;BR /&gt; by id;&lt;BR /&gt; retain _var1;&lt;BR /&gt; if not missing(var1) then _var1=var1;&lt;BR /&gt; else var1=_var1;&lt;BR /&gt; drop _var1;&lt;BR /&gt;Run;&lt;/PRE&gt;</description>
    <pubDate>Mon, 03 Apr 2017 15:52:07 GMT</pubDate>
    <dc:creator>ttatum</dc:creator>
    <dc:date>2017-04-03T15:52:07Z</dc:date>
    <item>
      <title>Filling down N # of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-down-N-of-observations/m-p/346729#M79984</link>
      <description>&lt;P&gt;Hello, I am trying to fill down a variable within a by-group&amp;nbsp;to replace missing values. However, when filling down I only want to fill at most 3 observations if a non-blank value is not seen.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For instance, I start with a data set&amp;nbsp;"have" below, and need it to turn out like "want." I can easily fill missing values by-group in all cases (see "test1" below), but can't quite figure out how to set a limit on the number of observations to fill down.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any suggestions on how to best accomplish this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Data have;&lt;BR /&gt; input id var1;&lt;BR /&gt; datalines;&lt;BR /&gt; 1 54&lt;BR /&gt; 1 60&lt;BR /&gt; 1 .&lt;BR /&gt; 1 .&lt;BR /&gt; 1 .&lt;BR /&gt; 1 .&lt;BR /&gt; 1 .&lt;BR /&gt; 2 100&lt;BR /&gt; 2 .&lt;BR /&gt; 2 .&lt;BR /&gt; 2 200&lt;BR /&gt; 2 .&lt;BR /&gt; 2 .&lt;BR /&gt; ;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;Data want;&lt;BR /&gt; input id var1;&lt;BR /&gt; datalines;&lt;BR /&gt; 1 54&lt;BR /&gt; 1 60&lt;BR /&gt; 1 60&lt;BR /&gt; 1 60&lt;BR /&gt; 1 60&lt;BR /&gt; 1 .&lt;BR /&gt; 1 .&lt;BR /&gt; 2 100&lt;BR /&gt; 2 100&lt;BR /&gt; 2 100&lt;BR /&gt; 2 200&lt;BR /&gt; 2 200&lt;BR /&gt; 2 200&lt;BR /&gt; ;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;Data test1;&lt;BR /&gt; set have;&lt;BR /&gt; by id;&lt;BR /&gt; retain _var1;&lt;BR /&gt; if not missing(var1) then _var1=var1;&lt;BR /&gt; else var1=_var1;&lt;BR /&gt; drop _var1;&lt;BR /&gt;Run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Apr 2017 15:52:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-down-N-of-observations/m-p/346729#M79984</guid>
      <dc:creator>ttatum</dc:creator>
      <dc:date>2017-04-03T15:52:07Z</dc:date>
    </item>
    <item>
      <title>Re: Filling down N # of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-down-N-of-observations/m-p/346751#M79992</link>
      <description>&lt;P&gt;Try it this 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 id;&lt;/P&gt;
&lt;P&gt;if first.id or var1 &amp;gt; . then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; _var1 = var1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; retain _var1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; replaced_count=0;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;if var1=. then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; var1=_var1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; replaced_count + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if replaced_count=3 then _var1=.;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2017 16:53:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-down-N-of-observations/m-p/346751#M79992</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-04-03T16:53:19Z</dc:date>
    </item>
    <item>
      <title>Re: Filling down N # of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-down-N-of-observations/m-p/346776#M79999</link>
      <description>&lt;P&gt;Excellent, thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2017 17:31:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-down-N-of-observations/m-p/346776#M79999</guid>
      <dc:creator>ttatum</dc:creator>
      <dc:date>2017-04-03T17:31:40Z</dc:date>
    </item>
    <item>
      <title>Re: Filling down N # of observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-down-N-of-observations/m-p/346917#M80040</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
 input id var1;
 datalines;
 1 54
 1 60
 1 .
 1 .
 1 .
 1 .
 1 .
 2 100
 2 .
 2 .
 2 200
 2 .
 2 .
 ;
Run;
data want;
 set have;
 by id;
 retain v;
 if first.id then do;n=0;v=.;end;
 if not missing(var1) then do;n=0; v=var1;end;
 n+1;
 if n gt 4 then v=.;
run;
proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Apr 2017 03:26:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-down-N-of-observations/m-p/346917#M80040</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-04-04T03:26:38Z</dc:date>
    </item>
  </channel>
</rss>

