<?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: Filling a cell with the value above by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Filling-a-cell-with-the-value-above-by-group/m-p/391108#M277657</link>
    <description>&lt;P&gt;Use the FIRST. variable generated by the BY statement to figure out when you are starting a new by group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set a ;
  by group ;
  if first.group then want=var;
  else want=coalesce(var,want);
  retain want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 204px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/14658i36F14EFE30C3733B/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 27 Aug 2017 02:14:22 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-08-27T02:14:22Z</dc:date>
    <item>
      <title>Filling a cell with the value above by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-a-cell-with-the-value-above-by-group/m-p/391103#M277655</link>
      <description>&lt;P&gt;Hi everyone&lt;/P&gt;
&lt;P&gt;I have a dataset with a column containing missing values.&lt;/P&gt;
&lt;P&gt;I am trying to fill these missing values from the cells above provided that the two rows belong to the same group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input N	Group	Var;
datalines;

1	1	3
2	1	6
3	1	2
4	1	.
5	2	.
6	2	6
7	3	3
8	3	.
9	3	5
10	4	. 
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;so for N=4 the var column should fill with the value above (2) since both rows belong to group 1. However, for N=5, this should be left empty as it is a new group.&lt;/P&gt;
&lt;P&gt;i tried the following code, but it is filling the cells with the value above regardless the group:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want (DROP = X) ;
SET have ;
by group;
RETAIN X ; 
IF NOT MISSING(var) THEN X = var ; 
var = X ;
if missing(var) then delete;
RUN ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the output dataset should look like the following&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;N	Group	Var
1	1	3
2	1	6
3	1	2
4	1	2
5	2	.
6	2	6
7	3	3
8	3	3
9	3	5
10	4	.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any help is appreciated&lt;/P&gt;
&lt;P&gt;kind regards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Aug 2017 01:18:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-a-cell-with-the-value-above-by-group/m-p/391103#M277655</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2017-08-27T01:18:29Z</dc:date>
    </item>
    <item>
      <title>Re: Filling a cell with the value above by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-a-cell-with-the-value-above-by-group/m-p/391106#M277656</link>
      <description>&lt;P&gt;PLease try&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set a;
by group;
retain x;
if first.group then x=.;
if var ne . then x=var;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 27 Aug 2017 02:13:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-a-cell-with-the-value-above-by-group/m-p/391106#M277656</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2017-08-27T02:13:30Z</dc:date>
    </item>
    <item>
      <title>Re: Filling a cell with the value above by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Filling-a-cell-with-the-value-above-by-group/m-p/391108#M277657</link>
      <description>&lt;P&gt;Use the FIRST. variable generated by the BY statement to figure out when you are starting a new by group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set a ;
  by group ;
  if first.group then want=var;
  else want=coalesce(var,want);
  retain want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 204px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/14658i36F14EFE30C3733B/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Aug 2017 02:14:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Filling-a-cell-with-the-value-above-by-group/m-p/391108#M277657</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-08-27T02:14:22Z</dc:date>
    </item>
  </channel>
</rss>

