<?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: Replacing missing IDs with the previous ones based on Date condition. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/499794#M133034</link>
    <description>&lt;P&gt;Use a retained variable to keep values across observations:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input ID_W Date_F: mmddyy10. ID_Fund;
format Date_F yymmddd10.;
datalines;
105246 06/30/2016 10
105246 09/30/2016 10
105246 12/31/2016 10
. 03/31/2017 10
. 06/30/2017 10
. 09/30/2017 10
. 12/31/2017 10
105250 12/31/2012 12
. 06/30/2017 12
. 09/30/2017 12
. 12/31/2017 12
105256 03/31/2016 25
105256 09/30/2016 25
. 06/30/2017 25
. 09/30/2017 25
;
run;

data want;
set have;
by id_fund;
retain _w;
if first.id_fund then _w = .;
if year(date_f) = 2016 and id_w ne . then _w = id_w;
if year(date_f) = 2017 and id_w = . and _w ne . then id_w = _w;
drop _w;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt; ID_W         Date_F    ID_Fund

105246    2016-06-30       10  
105246    2016-09-30       10  
105246    2016-12-31       10  
105246    2017-03-31       10  
105246    2017-06-30       10  
105246    2017-09-30       10  
105246    2017-12-31       10  
105250    2012-12-31       12  
     .    2017-06-30       12  
     .    2017-09-30       12  
     .    2017-12-31       12  
105256    2016-03-31       25  
105256    2016-09-30       25  
105256    2017-06-30       25  
105256    2017-09-30       25  
&lt;/PRE&gt;</description>
    <pubDate>Fri, 28 Sep 2018 07:20:12 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-09-28T07:20:12Z</dc:date>
    <item>
      <title>Replacing missing IDs with the previous ones based on Date condition.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/499773#M133032</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;I have the following dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;Data&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;input ID_W&lt;/SPAN&gt;&lt;/CODE&gt;&lt;CODE class="  language-sas"&gt; Date_F: mmddyy10. ID_Fund&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;/SPAN&gt;datalines&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/CODE&gt;105246 06/30/2016 10&lt;BR /&gt;105246 09/30/2016 10&lt;BR /&gt;105246 12/31/2016 10&lt;BR /&gt;. 03/31/2017 10&lt;BR /&gt;. 06/30/2017 10&lt;BR /&gt;. 09/30/2017 10&lt;BR /&gt;. 12/31/2017 10&lt;BR /&gt;105250 12/31/2012 12&lt;BR /&gt;. 06/30/2017 12&lt;BR /&gt;. 09/30/2017 12&lt;BR /&gt;. 12/31/2017 12&lt;BR /&gt;105256 03/31/2016 25&lt;BR /&gt;105256 09/30/2016 25&lt;BR /&gt;. 06/30/2017 25&lt;BR /&gt;. 09/30/2017 25&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token punctuation"&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;Data&lt;/SPAN&gt; want&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;input ID_W&lt;/SPAN&gt;&lt;/CODE&gt;&lt;CODE class="  language-sas"&gt; Date_F: mmddyy10. ID_Fund&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;/SPAN&gt;datalines&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/CODE&gt;105246 06/30/2016 10&lt;BR /&gt;105246 09/30/2016 10&lt;BR /&gt;105246 12/31/2016 10&lt;BR /&gt;105246 03/31/2017 10&lt;BR /&gt;105246 06/30/2017 10&lt;BR /&gt;105246 09/30/2017 10&lt;BR /&gt;105246 12/31/2017 10&lt;BR /&gt;105250 12/31/2012 12&lt;BR /&gt;. 06/30/2015 12&lt;BR /&gt;. 09/30/2017 12&lt;BR /&gt;. 12/31/2017 12&lt;BR /&gt;105256 03/31/2016 25&lt;BR /&gt;105256 09/30/2016 25&lt;BR /&gt;105256 06/30/2017 25&lt;BR /&gt;105256 09/30/2017 25&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token punctuation"&gt;&lt;BR /&gt;run;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This means for each ID_Fund, if ID_W exists for the dates of the year 2016, then it should replace the missing ID_Ws for all the dates of the year 2017. However, if&amp;nbsp;&lt;SPAN&gt;ID_W exists for any year before&amp;nbsp;2016, then it should not replace the missing values.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Sep 2018 06:43:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/499773#M133032</guid>
      <dc:creator>Saba1</dc:creator>
      <dc:date>2018-09-28T06:43:06Z</dc:date>
    </item>
    <item>
      <title>Replacing missing IDs with the previous ones based on Date condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/499785#M133033</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;I have the following dataset:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;Data&lt;/SPAN&gt; have&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;input ID_W&lt;/SPAN&gt;&lt;/CODE&gt;&lt;CODE class="  language-sas"&gt; Date_F: mmddyy10. ID_Fund&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;/SPAN&gt;datalines&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/CODE&gt;105246 06/30/2016 10&lt;BR /&gt;105246 09/30/2016 10&lt;BR /&gt;105246 12/31/2016 10&lt;BR /&gt;. 03/31/2017 10&lt;BR /&gt;. 06/30/2017 10&lt;BR /&gt;. 09/30/2017 10&lt;BR /&gt;. 12/31/2017 10&lt;BR /&gt;105250 12/31/2012 12&lt;BR /&gt;. 06/30/2017 12&lt;BR /&gt;. 09/30/2017 12&lt;BR /&gt;. 12/31/2017 12&lt;BR /&gt;105256 03/31/2016 25&lt;BR /&gt;105256 09/30/2016 25&lt;BR /&gt;. 06/30/2017 25&lt;BR /&gt;. 09/30/2017 25&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token punctuation"&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want the below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;Data&lt;/SPAN&gt; want&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token keyword"&gt;input ID_W&lt;/SPAN&gt;&lt;/CODE&gt;&lt;CODE class="  language-sas"&gt; Date_F: mmddyy10. ID_Fund&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;/SPAN&gt;datalines&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/CODE&gt;105246 06/30/2016 10&lt;BR /&gt;105246 09/30/2016 10&lt;BR /&gt;105246 12/31/2016 10&lt;BR /&gt;105246 03/31/2017 10&lt;BR /&gt;105246 06/30/2017 10&lt;BR /&gt;105246 09/30/2017 10&lt;BR /&gt;105246 12/31/2017 10&lt;BR /&gt;105250 12/31/2012 12&lt;BR /&gt;. 06/30/2015 12&lt;BR /&gt;. 09/30/2017 12&lt;BR /&gt;. 12/31/2017 12&lt;BR /&gt;105256 03/31/2016 25&lt;BR /&gt;105256 09/30/2016 25&lt;BR /&gt;105256 06/30/2017 25&lt;BR /&gt;105256 09/30/2017 25&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token punctuation"&gt;&lt;BR /&gt;run;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This means for each ID_Fund, if ID_W exists for the dates of the year 2016, then it should replace the missing ID_Ws for all the dates of the year 2017. However, if&amp;nbsp;&lt;SPAN&gt;ID_W exists for any year before&amp;nbsp;2016, then it should not replace the missing values.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Sep 2018 07:00:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/499785#M133033</guid>
      <dc:creator>Saba1</dc:creator>
      <dc:date>2018-09-28T07:00:23Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing missing IDs with the previous ones based on Date condition.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/499794#M133034</link>
      <description>&lt;P&gt;Use a retained variable to keep values across observations:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input ID_W Date_F: mmddyy10. ID_Fund;
format Date_F yymmddd10.;
datalines;
105246 06/30/2016 10
105246 09/30/2016 10
105246 12/31/2016 10
. 03/31/2017 10
. 06/30/2017 10
. 09/30/2017 10
. 12/31/2017 10
105250 12/31/2012 12
. 06/30/2017 12
. 09/30/2017 12
. 12/31/2017 12
105256 03/31/2016 25
105256 09/30/2016 25
. 06/30/2017 25
. 09/30/2017 25
;
run;

data want;
set have;
by id_fund;
retain _w;
if first.id_fund then _w = .;
if year(date_f) = 2016 and id_w ne . then _w = id_w;
if year(date_f) = 2017 and id_w = . and _w ne . then id_w = _w;
drop _w;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt; ID_W         Date_F    ID_Fund

105246    2016-06-30       10  
105246    2016-09-30       10  
105246    2016-12-31       10  
105246    2017-03-31       10  
105246    2017-06-30       10  
105246    2017-09-30       10  
105246    2017-12-31       10  
105250    2012-12-31       12  
     .    2017-06-30       12  
     .    2017-09-30       12  
     .    2017-12-31       12  
105256    2016-03-31       25  
105256    2016-09-30       25  
105256    2017-06-30       25  
105256    2017-09-30       25  
&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Sep 2018 07:20:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/499794#M133034</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-09-28T07:20:12Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing missing IDs with the previous ones based on Date condition.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/499798#M133036</link>
      <description>&lt;P&gt;Something like:&lt;/P&gt;
&lt;PRE&gt;data have;
  input id_w date_f: mmddyy10. id_fund;
  format date_f date9.;
datalines;
105246 06/30/2016 10
105246 09/30/2016 10
105246 12/31/2016 10
. 03/31/2017 10
. 06/30/2017 10
. 09/30/2017 10
. 12/31/2017 10
105250 12/31/2012 12
. 06/30/2017 12
. 09/30/2017 12
. 12/31/2017 12
105256 03/31/2016 25
105256 09/30/2016 25
. 06/30/2017 25
. 09/30/2017 25
;
run;

data want;
  set have;
  retain lstid;
  if id_w ne . and year(date_f)=2016 then lstid=id_w;
  if id_w ne . and id_w ne lstid then lstid=.;
  if id_w=. and year(date_f) ge 2016 then id_w=lstid;
run;
  
  
  &lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Sep 2018 07:24:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/499798#M133036</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-28T07:24:42Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing missing IDs with the previous ones based on Date condition.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/499848#M133048</link>
      <description>&lt;P&gt;Here is a solution that use the SET ... POINT=&amp;nbsp; instead of retained variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input ID_W Date_F: mmddyy10. ID_Fund;
format Date_F yymmddd10.;
datalines;
105246 06/30/2016 10
105246 09/30/2016 10
105246 12/31/2016 10
. 03/31/2017 10
. 06/30/2017 10
. 09/30/2017 10
. 12/31/2017 10
105250 12/31/2012 12
. 06/30/2017 12
. 09/30/2017 12
. 12/31/2017 12
105256 03/31/2016 25
105256 09/30/2016 25
. 06/30/2017 25
. 09/30/2017 25
run;

data want;
  set have ;
  by id_fund;;
  if first.id_fund then ptr=.;
  if year(date_f)=2016 then ptr=_n_;
  if id_w=. and ptr^=. then set have (keep=id_w) point=ptr;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The ptr variable keeps track of the position of most recent 2016 record within a BY ID_FUND group.&amp;nbsp;When you are concerned with updating only the ID_W variable, it has no particular advantage over the retain approach.&amp;nbsp; But if you wanted to recall (say) 20 variables from the 2016 record when ID_W is missing, then all you would need to do is list those 20 variables in the "keep=" parameter in the SET .... POINT= statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Sep 2018 11:55:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/499848#M133048</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-09-28T11:55:33Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing missing IDs with the previous ones based on Date condition.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/500093#M133127</link>
      <description>thanks a lot. it works really well.</description>
      <pubDate>Sat, 29 Sep 2018 12:38:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/500093#M133127</guid>
      <dc:creator>Saba1</dc:creator>
      <dc:date>2018-09-29T12:38:50Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing missing IDs with the previous ones based on Date condition.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/500094#M133128</link>
      <description>thank you for the guidance.</description>
      <pubDate>Sat, 29 Sep 2018 12:39:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/500094#M133128</guid>
      <dc:creator>Saba1</dc:creator>
      <dc:date>2018-09-29T12:39:14Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing missing IDs with the previous ones based on Date condition.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/500095#M133129</link>
      <description>thanks a lot.</description>
      <pubDate>Sat, 29 Sep 2018 12:39:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-missing-IDs-with-the-previous-ones-based-on-Date/m-p/500095#M133129</guid>
      <dc:creator>Saba1</dc:creator>
      <dc:date>2018-09-29T12:39:32Z</dc:date>
    </item>
  </channel>
</rss>

