<?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 complete missing values with previous value in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/264895#M7304</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@FreelanceReinhard wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28675"&gt;@fri0&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: It should be noted that the action of the UPDATE statement is not limited to variable LAST. This means that also missing DATE values would be filled with the last available&amp;nbsp;date within the BY group.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is true but there are ways to work around that.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Initalize to missing the variable that you don't way to carry forward after the output statement.&lt;/LI&gt;
&lt;LI&gt;Only operate on (UPDATE) the LOCF variable(s) and get the others from SET. &amp;nbsp;KEEP or DROP depending on which is the shortest list.&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data patients; 
   input patientID $ year $ diagA $ diagB $ diagC $; 
   datalines; 
1 2010 . . . 
1 2011 . 1 . 
1 2012 . . 1 
1 2014 . . . 
2 2009 1 . . 
2 2010 1 . . 
2 2013 . 1 . 
2 2015 . . . 
;;;; 
   run;
%let locf=patientid diag:;
data patients;
   if 0 then set patients;
   update patients(obs=0 keep=&amp;amp;locf) patients(keep=&amp;amp;locf);
   by patientid;
   set patients(drop=&amp;amp;locf);
   output;
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 19 Apr 2016 20:17:39 GMT</pubDate>
    <dc:creator>data_null__</dc:creator>
    <dc:date>2016-04-19T20:17:39Z</dc:date>
    <item>
      <title>How to complete missing values with previous value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/264820#M7297</link>
      <description>&lt;P&gt;Hi, I need your help to complete a dataset. I have many stock shares ordered by share and date. Also I have its&amp;nbsp;last price of the day, but sometimes there is a missing value if there was no trading that day. I want to complete missing values with the last valid value. The amount of missing values are different by each stock shares.&amp;nbsp;Thanks in advance for your help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is an example of my dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Name Date Last&lt;/P&gt;
&lt;P&gt;AAA 4/1/16 5.10&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;AAA 5/1/16&amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;AAA 6/1/16&amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;AAA 7/1/16&amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;AAA 8/1/16 5.13&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;AAA 11/1/16 5.13&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;AAA 12/1/16 5.15&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;BBB&amp;nbsp;4/1/16 25.60&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BBB 5/1/16&amp;nbsp;25.61&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BBB 6/1/16&amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BBB 7/1/16&amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BBB 8/1/16 25.65&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BBB 11/1/16&amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BBB 12/1/16&amp;nbsp;25.66&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I need&amp;nbsp;this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;For the second register, last price is missing, so I need to replace it with the previous value 5.10. The third register also has no value for last, so I replace with the previous value 5.10 and then until find a valid value.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Name Date Last&lt;/P&gt;
&lt;P&gt;AAA 4/1/16 5.10&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;AAA 5/1/16&amp;nbsp;&lt;STRONG&gt;5.10&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;AAA 6/1/16 &lt;STRONG&gt;5.10&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;AAA 7/1/16 &lt;STRONG&gt;5.10&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;AAA 8/1/16 5.13&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;AAA 11/1/16 5.13&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;AAA 12/1/16 5.15&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;BBB&amp;nbsp;4/1/16 25.60&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BBB 5/1/16&amp;nbsp;25.61&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BBB 6/1/16&amp;nbsp;&lt;STRONG&gt;25.61&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BBB 7/1/16 &lt;STRONG&gt;25.61&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BBB 8/1/16 25.65&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BBB 11/1/16 &lt;STRONG&gt;25.65&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BBB 12/1/16&amp;nbsp;25.66&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Apr 2016 15:49:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/264820#M7297</guid>
      <dc:creator>fri0</dc:creator>
      <dc:date>2016-04-19T15:49:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to complete missing values with previous value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/264824#M7298</link>
      <description>&lt;P&gt;Well, not typing that data in, so this is untested:&lt;/P&gt;
&lt;PRE&gt;data want (drop=lstlast);
  set have;
  retain lstlast;
  if last ne . then lstlast=last;
  else last=lstlast;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Apr 2016 16:07:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/264824#M7298</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-04-19T16:07:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to complete missing values with previous value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/264828#M7299</link>
      <description>The SAS server is down, but I'll try when it'll be operative again.</description>
      <pubDate>Tue, 19 Apr 2016 16:18:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/264828#M7299</guid>
      <dc:creator>fri0</dc:creator>
      <dc:date>2016-04-19T16:18:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to complete missing values with previous value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/264829#M7300</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28675"&gt;@fri0﻿&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A nice trick that I've learned from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__﻿&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding﻿&lt;/a&gt;&amp;nbsp;is&amp;nbsp;to use&amp;nbsp;the UPDATE statement as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Name $ Date :ddmmyy. Last;
format date ddmmyy8.;
cards;
AAA 4/1/16 5.10
AAA 5/1/16 .
AAA 6/1/16 .
AAA 7/1/16 .
AAA 8/1/16 5.13
AAA 11/1/16 5.13
AAA 12/1/16 5.15
BBB 4/1/16 .
BBB 5/1/16 25.61
BBB 6/1/16 .
BBB 7/1/16 .
BBB 8/1/16 25.65
BBB 11/1/16 .
BBB 12/1/16 25.66
;

data want;
update have(obs=0) have;
by name;
output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please note that I've replaced the first value of variable LAST for name 'BBB' with a missing value to demonstrate that with the above solution values from one name are &lt;STRONG&gt;not&lt;/STRONG&gt; carried forward into missing&amp;nbsp;data of another name.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: It should be noted that the action of the UPDATE statement is not limited to variable LAST. This means that also missing DATE values would be filled with the last available&amp;nbsp;date within the BY group.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Apr 2016 16:33:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/264829#M7300</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-04-19T16:33:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to complete missing values with previous value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/264847#M7301</link>
      <description>&lt;P&gt;Don't forget to take care of the case where &lt;EM&gt;last&lt;/EM&gt; is missing for the first record of a stock&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Name $ Date :mmddyy10. Last;
format date yymmdd10.;
datalines;
AAA 4/1/16 5.10
AAA 5/1/16 .
AAA 6/1/16 .
AAA 7/1/16 .
AAA 8/1/16 5.13
AAA 11/1/16 5.13
AAA 12/1/16 5.15
BBB 3/1/16 .
BBB 4/1/16 25.60
BBB 5/1/16 25.61
BBB 6/1/16 .
BBB 7/1/16 .
BBB 8/1/16 25.65
BBB 11/1/16 .
BBB 12/1/16 25.66
;

data want (drop=lstlast);
  set have; by name notsorted;
  retain lstlast;
  if first.name then call missing(lstLast);
  if last ne . then lstlast=last;
  else last=lstlast;
run;

proc print data=want noobs; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Apr 2016 17:35:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/264847#M7301</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-04-19T17:35:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to complete missing values with previous value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/264895#M7304</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@FreelanceReinhard wrote:&lt;BR /&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/28675"&gt;@fri0&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: It should be noted that the action of the UPDATE statement is not limited to variable LAST. This means that also missing DATE values would be filled with the last available&amp;nbsp;date within the BY group.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is true but there are ways to work around that.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Initalize to missing the variable that you don't way to carry forward after the output statement.&lt;/LI&gt;
&lt;LI&gt;Only operate on (UPDATE) the LOCF variable(s) and get the others from SET. &amp;nbsp;KEEP or DROP depending on which is the shortest list.&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data patients; 
   input patientID $ year $ diagA $ diagB $ diagC $; 
   datalines; 
1 2010 . . . 
1 2011 . 1 . 
1 2012 . . 1 
1 2014 . . . 
2 2009 1 . . 
2 2010 1 . . 
2 2013 . 1 . 
2 2015 . . . 
;;;; 
   run;
%let locf=patientid diag:;
data patients;
   if 0 then set patients;
   update patients(obs=0 keep=&amp;amp;locf) patients(keep=&amp;amp;locf);
   by patientid;
   set patients(drop=&amp;amp;locf);
   output;
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Apr 2016 20:17:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/264895#M7304</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2016-04-19T20:17:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to complete missing values with previous value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/264897#M7305</link>
      <description>&lt;P&gt;Woah!&amp;nbsp;&lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 19 Apr 2016 20:19:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/264897#M7305</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-04-19T20:19:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to complete missing values with previous value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/265536#M7329</link>
      <description>Thanks RW9, this work perfect when there is no missing space as first value for last. But, there were cases with first value missing, so I had to chose the PGSTats.</description>
      <pubDate>Thu, 21 Apr 2016 21:55:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/265536#M7329</guid>
      <dc:creator>fri0</dc:creator>
      <dc:date>2016-04-21T21:55:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to complete missing values with previous value</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/265537#M7330</link>
      <description>Thans PG, this work absolutely perfect.</description>
      <pubDate>Thu, 21 Apr 2016 21:55:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-to-complete-missing-values-with-previous-value/m-p/265537#M7330</guid>
      <dc:creator>fri0</dc:creator>
      <dc:date>2016-04-21T21:55:40Z</dc:date>
    </item>
  </channel>
</rss>

