<?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: how to replace missings values with nextvalues in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-replace-missings-values-with-nextvalues/m-p/891207#M352094</link>
    <description>&lt;P&gt;In group A, your sample input shows the VAR1 sequence to be&amp;nbsp; &amp;nbsp;1, ., 3,. 8.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why does your sample output show&amp;nbsp; 1,3,8,3?&amp;nbsp; Or are you prepared to take the next VAR1 from group B?&amp;nbsp; And it looks like you are MOVING a future non-missing value back, not just copying it.&amp;nbsp; Is that right&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And why does the sample input have 3 group B observations, but the sample output have only 2?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 27 Aug 2023 00:09:27 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-08-27T00:09:27Z</dc:date>
    <item>
      <title>how to replace missings values with nextvalues</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-replace-missings-values-with-nextvalues/m-p/891195#M352085</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds_missing_values;
	infile datalines dlm=',';
	input group $ var1 var2 var3;
	datalines;
A, 1, 2, 3
A, ., 2, 5
A, 3, 4, .
A, 8, ., .
B, ., ., .
B, ., 2, 6
B, 3, 1, 8
C, 9, 4, 8

;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How to replace missing values with next values for repective variables missings output like below&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="292"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="72.75px" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72.75px" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72.75px" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;8&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;4&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72.75px" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72.75px" height="30px"&gt;B&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72.75px" height="30px"&gt;B&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72.75px" height="30px"&gt;C&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;9&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Sat, 26 Aug 2023 06:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-replace-missings-values-with-nextvalues/m-p/891195#M352085</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2023-08-26T06:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: how to replace missings values with nextvalues</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-replace-missings-values-with-nextvalues/m-p/891196#M352086</link>
      <description>&lt;P&gt;Why does group change to "A" in every observation?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265860"&gt;@BrahmanandaRao&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds_missing_values;
	infile datalines dlm=',';
	input group $ var1 var2 var3;
	datalines;
A, 1, 2, 3
A, ., 2, 5
A, 3, 4, .
A, 8, ., .
B, ., ., .
B, ., 2, 6
B, 3, 1, 8
C, 9, 4, 8

;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How to replace missing values with next values for repective variables missings output like below&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="292"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="73"&gt;A&lt;/TD&gt;
&lt;TD width="73"&gt;1&lt;/TD&gt;
&lt;TD width="73"&gt;2&lt;/TD&gt;
&lt;TD width="73"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;8&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;A&lt;/TD&gt;
&lt;TD&gt;9&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Aug 2023 06:36:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-replace-missings-values-with-nextvalues/m-p/891196#M352086</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-08-26T06:36:43Z</dc:date>
    </item>
    <item>
      <title>Re: how to replace missings values with nextvalues</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-replace-missings-values-with-nextvalues/m-p/891197#M352087</link>
      <description>&lt;P&gt;When you have a variable like your Group variable it seems unlikely that you should acquire values from a different group to replace missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you describe the purpose of replacing these values? How will you use the data? That may give us some other ideas.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265860"&gt;@BrahmanandaRao&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds_missing_values;
	infile datalines dlm=',';
	input group $ var1 var2 var3;
	datalines;
A, 1, 2, 3
A, ., 2, 5
A, 3, 4, .
A, 8, ., .
B, ., ., .
B, ., 2, 6
B, 3, 1, 8
C, 9, 4, 8

;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;How to replace missing values with next values for repective variables missings output like below&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="292"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="72.75px" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72.75px" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72.75px" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;8&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;4&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72.75px" height="30px"&gt;A&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72.75px" height="30px"&gt;B&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72.75px" height="30px"&gt;B&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="72.75px" height="30px"&gt;C&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;9&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="72.75px" height="30px"&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 26 Aug 2023 06:51:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-replace-missings-values-with-nextvalues/m-p/891197#M352087</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-26T06:51:37Z</dc:date>
    </item>
    <item>
      <title>Re: how to replace missings values with nextvalues</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-replace-missings-values-with-nextvalues/m-p/891207#M352094</link>
      <description>&lt;P&gt;In group A, your sample input shows the VAR1 sequence to be&amp;nbsp; &amp;nbsp;1, ., 3,. 8.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why does your sample output show&amp;nbsp; 1,3,8,3?&amp;nbsp; Or are you prepared to take the next VAR1 from group B?&amp;nbsp; And it looks like you are MOVING a future non-missing value back, not just copying it.&amp;nbsp; Is that right&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And why does the sample input have 3 group B observations, but the sample output have only 2?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Aug 2023 00:09:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-replace-missings-values-with-nextvalues/m-p/891207#M352094</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-08-27T00:09:27Z</dc:date>
    </item>
    <item>
      <title>Re: how to replace missings values with nextvalues</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-replace-missings-values-with-nextvalues/m-p/891208#M352095</link>
      <description>&lt;P&gt;And additionally to questions already asked: What should happen in if the last row in a group has a missing value? Leave it missing or something else?&lt;/P&gt;</description>
      <pubDate>Sun, 27 Aug 2023 00:54:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-replace-missings-values-with-nextvalues/m-p/891208#M352095</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-08-27T00:54:02Z</dc:date>
    </item>
    <item>
      <title>Re: how to replace missings values with nextvalues</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-replace-missings-values-with-nextvalues/m-p/891267#M352100</link>
      <description>&lt;P&gt;In the absence of a reply, here's some code that solves what I presume is the typical "bringing back the future" problem.&amp;nbsp; For each record, a missing value is replaced by the earliest following non-missing value, within the same group.&amp;nbsp; This means of course that any group that ends with missing values will not have those values replaced:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds_missing_values;
	infile datalines dlm=',';
	input group $ var1 var2 var3;
	datalines;
A, 1, 2, 3
A, ., 2, 5
A, 3, 4, .
A, 8, ., .
B, ., ., .
B, ., 2, 6
B, 3, 1, 8
C, 9, 4, 8
run;

data want (drop=_:);
  set ds_missing_values (in=firstpass)
      ds_missing_values (in=secondpass);
  by group;

  array var {3} var1-var3;

  array history {3,20}  _histv1_1-_histv1_20  
                        _histv2_1-_histv2_20  
                        _histv3_1-_histv3_20 ;
  retain _: ;

  if first.group then call missing(_f,_s,of _:);
  _f+firstpass;

  if firstpass then do _v=1 to 3;
    history{_v,_f}=var{_v};
  end;

  if secondpass;
  _s+secondpass;
  call missing(history{1,_s},history{2,_s},history{3,_s});
  if var1=. then var1=coalesce(of _histv1:);
  if var2=. then var2=coalesce(of _histv2:);
  if var3=. then var3=coalesce(of _histv3:);
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The program passes through eac group twice: the first time to build a history (up to 20 obs in this case) of values for VAR1, VAR2, and VAR3.&amp;nbsp; The second pass will tap into that history, replacing a missing value with the earliest following non-missing (that's what the coalesce functiom does).&amp;nbsp; Also during the second pass, current data in the history array is eliminated, so only future values are exposed to the coalesce functions.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Aug 2023 20:27:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-replace-missings-values-with-nextvalues/m-p/891267#M352100</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-08-27T20:27:01Z</dc:date>
    </item>
  </channel>
</rss>

