<?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: Lag function to find most recent non-missing value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-to-find-most-recent-non-missing-value/m-p/584717#M166580</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	retain want_group;
	infile cards missover;
	input class group $;
	if not missing(group) then want_group=group;
	cards;
1001 A
1002 B
1003 C
1003 
1003 C
1020 D 
1020
1020
1020
1020
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 28 Aug 2019 18:29:01 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-08-28T18:29:01Z</dc:date>
    <item>
      <title>Lag function to find most recent non-missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-to-find-most-recent-non-missing-value/m-p/584710#M166576</link>
      <description>&lt;P&gt;I'm trying to figure out how to impute missing values with the most recent value. I found a paper (&lt;A href="https://documentation.sas.com/?docsetId=casdspgm&amp;amp;docsetTarget=p0x15q4nde1191n10s3x42uy4epc.htm&amp;amp;docsetVersion=3.2&amp;amp;locale=en"&gt;https://documentation.sas.com/?docsetId=casdspgm&amp;amp;docsetTarget=p0x15q4nde1191n10s3x42uy4epc.htm&amp;amp;docsetVersion=3.2&amp;amp;locale=en&lt;/A&gt;), but it seems to assume an upper bound for the gap between non-missing values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Lets say we have the following dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;class Group&lt;/P&gt;&lt;P&gt;1001 A&lt;/P&gt;&lt;P&gt;1002 B&lt;/P&gt;&lt;P&gt;1003 C&lt;/P&gt;&lt;P&gt;1003&amp;nbsp;&lt;/P&gt;&lt;P&gt;1003 C&lt;/P&gt;&lt;P&gt;1020 D&amp;nbsp;&lt;/P&gt;&lt;P&gt;1020&lt;/P&gt;&lt;P&gt;1020&lt;/P&gt;&lt;P&gt;1020&lt;/P&gt;&lt;P&gt;1020&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;class Group&lt;/P&gt;&lt;P&gt;1001 A&lt;/P&gt;&lt;P&gt;1002 B&lt;/P&gt;&lt;P&gt;1003 C&lt;/P&gt;&lt;P&gt;1003 C&lt;/P&gt;&lt;P&gt;1003 C&lt;/P&gt;&lt;P&gt;1020 D&amp;nbsp;&lt;/P&gt;&lt;P&gt;1020 D&lt;/P&gt;&lt;P&gt;1020 D&lt;/P&gt;&lt;P&gt;1020 D&lt;/P&gt;&lt;P&gt;1020 D&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Assume the group stays consistent within all classes and the first instance of the class always has a filled in group. The number of records in a class is variable. I tried using the first function, but I keep getting a value of 1 even though the Group variable is composed of characters or strings, so I'm not sure whats happening there.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 18:23:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-to-find-most-recent-non-missing-value/m-p/584710#M166576</guid>
      <dc:creator>toesockshoe</dc:creator>
      <dc:date>2019-08-28T18:23:51Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function to find most recent non-missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-to-find-most-recent-non-missing-value/m-p/584713#M166577</link>
      <description>&lt;P&gt;Can you show us the desired output from this data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why does the answer have to use LAG?&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 18:23:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-to-find-most-recent-non-missing-value/m-p/584713#M166577</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-28T18:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function to find most recent non-missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-to-find-most-recent-non-missing-value/m-p/584714#M166578</link>
      <description>&lt;P&gt;ok edited my post... it doesn't have to use lag (which is prolly the case), but I assumed that would be the most straightforward. Other solutions are still appreciated!&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 18:24:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-to-find-most-recent-non-missing-value/m-p/584714#M166578</guid>
      <dc:creator>toesockshoe</dc:creator>
      <dc:date>2019-08-28T18:24:32Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function to find most recent non-missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-to-find-most-recent-non-missing-value/m-p/584717#M166580</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	retain want_group;
	infile cards missover;
	input class group $;
	if not missing(group) then want_group=group;
	cards;
1001 A
1002 B
1003 C
1003 
1003 C
1020 D 
1020
1020
1020
1020
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Aug 2019 18:29:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-to-find-most-recent-non-missing-value/m-p/584717#M166580</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-08-28T18:29:01Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function to find most recent non-missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-to-find-most-recent-non-missing-value/m-p/584718#M166581</link>
      <description>&lt;P&gt;Hello &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/218524"&gt;@toesockshoe&lt;/a&gt;&amp;nbsp; Easy with UPDATE statement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards  truncover;
input class Group $;
cards;
1001 A
1002 B
1003 C
1003 
1003 C
1020 D 
1020
1020
1020
1020
;

data want;
update have(obs=0) have;
by class;
output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Aug 2019 18:28:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-to-find-most-recent-non-missing-value/m-p/584718#M166581</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-28T18:28:42Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function to find most recent non-missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-to-find-most-recent-non-missing-value/m-p/584720#M166583</link>
      <description>&lt;P&gt;yes, first. and last. variables are always 0 or 1.&amp;nbsp; Those values are not copied from the values of your GROUP in this example.&amp;nbsp; That is an important tool and well worth learning.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there are no other variables to consider, this would do the trick:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   update have (obs=0) have;
   by class;
   output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If there are other variables that you want to leave unaffected, try it this way:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by class;
   if first.class or group &amp;gt; ' ' then new_group = group;
   retain new_group;
   drop group;
   rename new_group = group;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 18:31:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-to-find-most-recent-non-missing-value/m-p/584720#M166583</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-08-28T18:31:18Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function to find most recent non-missing value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-to-find-most-recent-non-missing-value/m-p/584729#M166591</link>
      <description>&lt;P&gt;cheers mate.... works like a charm&lt;/P&gt;</description>
      <pubDate>Wed, 28 Aug 2019 18:39:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-to-find-most-recent-non-missing-value/m-p/584729#M166591</guid>
      <dc:creator>toesockshoe</dc:creator>
      <dc:date>2019-08-28T18:39:47Z</dc:date>
    </item>
  </channel>
</rss>

