<?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: Previous observatio as last observation if missing in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Previous-observatio-as-last-observation-if-missing/m-p/694085#M211672</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/133090"&gt;@RandyStan&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think if you retain the most recent non-missing Var_A for each ID, you can accomplish what you're asking for fairly easily.&amp;nbsp; See sample code, below.&amp;nbsp; However, note that if all of the Var_A's are missing (as in ID 3), then the last occurrence of the ID will still be missing; there's not much we can do there.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Unaltered_Data;
	INFILE	Datalines	MISSOVER;
	INPUT	ID		$
			VAR_A	$
			;
DATALINES;
1            MF
1            MF
1            MF
2            HUF
2           
2           HUF
2
3         
3
3
4
4           I
4           I
4           I
4           NRI
4 
;
RUN;

DATA	Non_Missing_Last;
	DROP	_:;
	RETAIN	_Prior_Not_Missing '       ';

	SET	Unaltered_Data;
		BY	ID	NOTSORTED;

	IF	FIRST.ID				THEN
		CALL	MISSING(_Prior_Not_Missing);

	IF	LAST.ID					AND
		MISSING(VAR_A)			THEN
		VAR_A				=	_Prior_Not_Missing;

	IF	NOT	MISSING(VAR_A)		THEN
		_Prior_Not_Missing	=	VAR_A;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 25 Oct 2020 05:28:26 GMT</pubDate>
    <dc:creator>jimbarbour</dc:creator>
    <dc:date>2020-10-25T05:28:26Z</dc:date>
    <item>
      <title>Previous observatio as last observation if missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Previous-observatio-as-last-observation-if-missing/m-p/694084#M211671</link>
      <description>&lt;PRE&gt;Dear All:

My data is as follows:

ID       VAR_A
1            MF
1            MF
1            MF
2            HUF
2           
2           HUF
2
3         
3
3
4
4           I
4           I
4           I
4           NRI
4 

I want my data to look as follow -- where the rule is that if the last observation is missing the missing value should &lt;BR /&gt;be the previous non-missing observation.

ID       VAR_A
1            MF
1            MF
1            MF
2            HUF
2           
2           HUF
2           HUF
3         
3
3
4
4           I
4           I
4           I
4           NRI
4           NRI&lt;/PRE&gt;</description>
      <pubDate>Sun, 25 Oct 2020 05:07:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Previous-observatio-as-last-observation-if-missing/m-p/694084#M211671</guid>
      <dc:creator>RandyStan</dc:creator>
      <dc:date>2020-10-25T05:07:35Z</dc:date>
    </item>
    <item>
      <title>Re: Previous observatio as last observation if missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Previous-observatio-as-last-observation-if-missing/m-p/694085#M211672</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/133090"&gt;@RandyStan&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think if you retain the most recent non-missing Var_A for each ID, you can accomplish what you're asking for fairly easily.&amp;nbsp; See sample code, below.&amp;nbsp; However, note that if all of the Var_A's are missing (as in ID 3), then the last occurrence of the ID will still be missing; there's not much we can do there.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	Unaltered_Data;
	INFILE	Datalines	MISSOVER;
	INPUT	ID		$
			VAR_A	$
			;
DATALINES;
1            MF
1            MF
1            MF
2            HUF
2           
2           HUF
2
3         
3
3
4
4           I
4           I
4           I
4           NRI
4 
;
RUN;

DATA	Non_Missing_Last;
	DROP	_:;
	RETAIN	_Prior_Not_Missing '       ';

	SET	Unaltered_Data;
		BY	ID	NOTSORTED;

	IF	FIRST.ID				THEN
		CALL	MISSING(_Prior_Not_Missing);

	IF	LAST.ID					AND
		MISSING(VAR_A)			THEN
		VAR_A				=	_Prior_Not_Missing;

	IF	NOT	MISSING(VAR_A)		THEN
		_Prior_Not_Missing	=	VAR_A;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Oct 2020 05:28:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Previous-observatio-as-last-observation-if-missing/m-p/694085#M211672</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2020-10-25T05:28:26Z</dc:date>
    </item>
    <item>
      <title>Re: Previous observatio as last observation if missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Previous-observatio-as-last-observation-if-missing/m-p/694090#M211673</link>
      <description>&lt;P&gt;Merge fun:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA	have;
INFILE	Datalines	MISSOVER;
INPUT	ID	$	VAR_A	$;
DATALINES;
1            MF
1            MF
1            MF
2            HUF
2           
2           HUF
2
3         
3
3
4
4           I
4           I
4           I
4           NRI
4 
;
RUN;

data want;
 merge have have(rename=(var_a=_var_a) where=(_var_a&amp;gt;' '));
 by id;
 if last.id and var_a=' ' then var_a=_var_a;
 drop _:;
run;

 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 25 Oct 2020 09:47:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Previous-observatio-as-last-observation-if-missing/m-p/694090#M211673</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-10-25T09:47:05Z</dc:date>
    </item>
    <item>
      <title>Re: Previous observatio as last observation if missing</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Previous-observatio-as-last-observation-if-missing/m-p/694158#M211688</link>
      <description>&lt;P&gt;You would use the LAG(VAR_A) under the conditions of missing VAR_A and last.id in a group of size &amp;gt; 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The logic evaluating such a condition can be encapsulated in a single functional expression.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  by  id;

  var_a = coalesceC (var_a, ifc (last.id and not first.id, lag(var_a), ''));
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Oct 2020 03:39:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Previous-observatio-as-last-observation-if-missing/m-p/694158#M211688</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-10-26T03:39:58Z</dc:date>
    </item>
  </channel>
</rss>

