<?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: Replace null with value by looking back and ahead a certain number of days in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627099#M185029</link>
    <description>&lt;P&gt;Thank you for the explanation. I will admit that I am rusty with arrays, so I will adopt this logic for my data and update here.&lt;/P&gt;</description>
    <pubDate>Tue, 25 Feb 2020 05:03:48 GMT</pubDate>
    <dc:creator>tk89</dc:creator>
    <dc:date>2020-02-25T05:03:48Z</dc:date>
    <item>
      <title>Replace null with value by looking back and ahead a certain number of days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/626054#M184588</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table with patientID, admit date and diagnosis code. If the code is null, we look back 2 days to see if there is a valid code within the last two days. If that's null too, we look forward two days, then 5 days back, 5 days forward, and finally 9 days back. We do this each time we encounter a null value for the diagnosis code. I have tried doing a proc sql self join and using lag functions to lag by 2,5, and 9 but it did not help as the dates are not consecutive. So, for a null code, the sequence is to check date-2, then date+2, date-5, date+5 and finally date-9 until a valid code is found. I am using SAS Eg 8.1 Any inputs are greatly appreciated. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have:&lt;/P&gt;&lt;P&gt;PatientID&amp;nbsp; Admit_dt&amp;nbsp; &amp;nbsp;diag&amp;nbsp;&lt;BR /&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20160201&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20160210&amp;nbsp; 190&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20160215&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20160217&amp;nbsp; 29&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20160330&amp;nbsp; 23&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20160405&amp;nbsp; 32&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20160410&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20160510&amp;nbsp; 12&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 20160510&amp;nbsp; 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Want:&lt;/P&gt;&lt;P&gt;PatientID&amp;nbsp; Admit_dt&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;diag&amp;nbsp; &amp;nbsp; &amp;nbsp;diag_new&lt;BR /&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016-02-01&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016-02-10&amp;nbsp; &amp;nbsp; &amp;nbsp; 190&amp;nbsp; &amp;nbsp; 190&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016-02-15&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 29&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016-02-17&amp;nbsp; &amp;nbsp; &amp;nbsp; 29&amp;nbsp; &amp;nbsp; &amp;nbsp; 29&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016-03-30&amp;nbsp; &amp;nbsp; &amp;nbsp; 23&amp;nbsp; &amp;nbsp; &amp;nbsp; 23&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016-04-05&amp;nbsp; &amp;nbsp; &amp;nbsp; 32&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;32&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016-04-10&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 32&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016-05-10&amp;nbsp; &amp;nbsp; &amp;nbsp; 12&amp;nbsp; &amp;nbsp; &amp;nbsp; 12&lt;/P&gt;&lt;P&gt;101&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2016-05-10&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12&lt;/P&gt;</description>
      <pubDate>Thu, 20 Feb 2020 05:34:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/626054#M184588</guid>
      <dc:creator>tk89</dc:creator>
      <dc:date>2020-02-20T05:34:04Z</dc:date>
    </item>
    <item>
      <title>Re: Replace null with value by looking back and ahead a certain number of days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/626062#M184594</link>
      <description>&lt;P&gt;First thing to clarify: are those dates real sas dates? If not: fix this first. Second thing to note: there is a huge difference between null and zero, in the data you have posted there are no null-values. Do you mean zero instead of null? What exactly do you mean by "date+2"? Using the date in the first row, do you want to look for with Admit_dt = 20160203 or do you want to look two observations ahead?&lt;/P&gt;</description>
      <pubDate>Thu, 20 Feb 2020 07:16:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/626062#M184594</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-02-20T07:16:09Z</dc:date>
    </item>
    <item>
      <title>Re: Replace null with value by looking back and ahead a certain number of days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/626069#M184598</link>
      <description>&lt;P&gt;I have added conditions for only look forward, you can add the look backward conditions too.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input PatientID  Admit_dt:yymmdd8.   diag ;
format PatientID diag best12. Admit_dt yymmdd10.;
id=_n_;
cards;
101          20160201  0
101          20160210  190
101          20160215  0
101          20160217  29
101          20160330  23
101          20160405  32
101          20160410  0
101          20160510  12
101          20160510  0
;
run;

data test(drop=Admit_dt diag);
format Admit_dt1 yymmdd10.;
set have;
Admit_dt1=Admit_dt;
diag1=diag;
run;


proc sql;
create table want as
select a.PatientID,a.id,Admit_dt,Admit_dt1,diag,diag1
from have a, test b
where a.PatientID=b.PatientID and diag1 ne 0
order by PatientID, id, Admit_dt1;
quit;

data want1(drop=Admit_dt1 diag1 id);
set want;
retain diag_new;
by PatientID id;
if first.id then diag_new=diag;
if first.id and diag ne 0 then output;
if diag_new=0 then do;
	if Admit_dt1=Admit_dt and diag1 ne 0 then do;
		diag_new=diag1;
		output;
	end;
	else if Admit_dt1=Admit_dt+2 and diag1 ne 0 then do;
		diag_new=diag1;
		output;
	end;
	else if Admit_dt1=Admit_dt+5 and diag1 ne 0 then do;
		diag_new=diag1;
		output;
	end;
	else if Admit_dt1=Admit_dt+9 and diag1 ne 0 then do;
		diag_new=diag1;
		output;
	end;
end;
if last.id and diag_new = 0 then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Please let us know if this helped.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Feb 2020 07:50:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/626069#M184598</guid>
      <dc:creator>Satish_Parida</dc:creator>
      <dc:date>2020-02-20T07:50:48Z</dc:date>
    </item>
    <item>
      <title>Re: Replace null with value by looking back and ahead a certain number of days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/626315#M184708</link>
      <description>&lt;P&gt;Usually looking forward and backward are incremented symmetrically in 1-day intervals,&amp;nbsp; I.e. look at offsets &amp;nbsp; d=-1,d=1,&amp;nbsp; d=-2,d=2, etc.&amp;nbsp; which is easy to embed inside a DO D=1 to 9 loop, using a sign change inside.&lt;BR /&gt;&lt;BR /&gt;But you want this sequence:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; d=-1,d=-2, &amp;nbsp; d=1,d=2, &amp;nbsp;&amp;nbsp; d=-3,d=-4,d=-5,&amp;nbsp; d=3,d=4,d=5 ...&lt;/P&gt;
&lt;P&gt;so the loop definition has to be more detailed as here:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input PatientID  Admit_dt :yymmdd8.  diag;
  format admit_dt date9.;
datalines; 
101          20160201  0
101          20160210  190
101          20160215  0
101          20160330  23
101          20160405  32
101          20160410  0
101          20160510  12
101          20160510  0
;

%let begdate=01jan2016;
%let enddate=31dec2016;

data want (drop=d);
  set have (where=(diag^=0)  in=in_diag)
      have (in=in2);
  by patientid;

  array diag_hist {%sysevalf("&amp;amp;begdate"d):%sysevalf("&amp;amp;enddate"d)} _temporary_;

  if first.patientid then call missing(of diag_hist{*});
  if in_diag then diag_hist{admit_dt}=diag;

  if in2;
  if diag=0 then do d=0 ,-1,-2,1,2  ,-3,-4,-5,3,4,5  ,-6,-7,-8,-9,6,7,8,9;
    if diag_hist{admit_dt+d}^=. then do; 
      new_diag=diag_hist{admit_dt+d};
      leave;
    end;
  end;
  if new_diag=. then new_diag=diag;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;OL&gt;
&lt;LI&gt;The array DIAG_HIST array will contain all the non-zero DIAG codes in indexed by date, with upper and lower bounds of 01jan2016 and 31dec2016 respectively.&amp;nbsp; It's a temporary array so it retains its value accorss multiple incoming observatinos.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;The SET statement reads, for each ID, &amp;nbsp; (a) first all the non-zero diag's and puts them in the array, (b) then all diag's and in the case of diag=0, searches the array for nearby diagnosis codes.&lt;/LI&gt;
&lt;LI&gt;As I mentioned above note the definition of the DO loop.&amp;nbsp; In addition to batching numbers &amp;nbsp;&amp;nbsp; -2,-1,1,2,... I start out with D=0.&amp;nbsp; This is to first capture instances in which a date might have 2 records, in which one has a non-null diagnosis.&lt;/LI&gt;
&lt;LI&gt;If you want to know which observations has been used to correct a null diagnosis, then eliminate the "drop=d" data set name parameter.&lt;/LI&gt;
&lt;LI&gt;Also note I set two macrovars BEGDATE and ENDDATE, which are then used inside a %sysevalf macro functions to establish the bound of the diag_hist array.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Edited additional note.&amp;nbsp; I guess one could somehow set up the loop iteration values using macrovars - especially if you want to modify the date-search cutpoints,&amp;nbsp; currently at {-2,2}, {-5,5},&amp;nbsp; and {-9,9}, but for now I think that goes beyond the proper scope of this topic.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Feb 2020 19:44:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/626315#M184708</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-02-20T19:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: Replace null with value by looking back and ahead a certain number of days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627096#M185026</link>
      <description>&lt;P&gt;Apologies for the wrong terminology. The code is numeric, so if it's a 0 or missing value, then it is considered invalid. Those are dates that I read in as yymddn8. By date+2, I mean looking forward till two days. So, for the first row, since it's the beginning and there's nothing to look back, I start by looking forward till 2 days, i.e., 20160201 - 20160203 and look at claims between the two dates. If there are claims, and any of them have a valid code, I pick that and copy its code and other values (rank, SOI etc) to the first row. If there are no claims or claims with missing or 0 code, then I look for claims between 20160201 and 20160206(i.e., date+5) and repeat the same process.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please note that I am not jumping backward and forward by the number of rows, rather by the number of days. So, if there are 10 rows between 20160201 and 20160203, then I look at all the 10 rows for my date+2 check. Hope this clarifies my question further. Thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 04:58:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627096#M185026</guid>
      <dc:creator>tk89</dc:creator>
      <dc:date>2020-02-25T04:58:48Z</dc:date>
    </item>
    <item>
      <title>Re: Replace null with value by looking back and ahead a certain number of days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627097#M185027</link>
      <description>&lt;P&gt;Thank you for your reply. Your code worked for a sample of my data, but when I run it for the whole table (around 5 million rows), it delves into producing a cartesian product. I am trying to troubleshoot and will update as soon as I can.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 05:01:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627097#M185027</guid>
      <dc:creator>tk89</dc:creator>
      <dc:date>2020-02-25T05:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: Replace null with value by looking back and ahead a certain number of days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627099#M185029</link>
      <description>&lt;P&gt;Thank you for the explanation. I will admit that I am rusty with arrays, so I will adopt this logic for my data and update here.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 05:03:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627099#M185029</guid>
      <dc:creator>tk89</dc:creator>
      <dc:date>2020-02-25T05:03:48Z</dc:date>
    </item>
    <item>
      <title>Re: Replace null with value by looking back and ahead a certain number of days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627113#M185034</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can replace the loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if ^DIAG then NEW_DIAG=coalesce(
 diag_hist{admit_dt-1}
,diag_hist{admit_dt-2}
,diag_hist{admit_dt+1}
,diag_hist{admit_dt+2}
,diag_hist{admit_dt-3}
,diag_hist{admit_dt-4}
,diag_hist{admit_dt-5}
,diag_hist{admit_dt+3}
,diag_hist{admit_dt+4}
,diag_hist{admit_dt+5}
,diag_hist{admit_dt-6}
,diag_hist{admit_dt-7}
,diag_hist{admit_dt-8}
,diag_hist{admit_dt-9)
); &lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;for, maybe, better speed or better clarity.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Feb 2020 07:18:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627113#M185034</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-02-25T07:18:50Z</dc:date>
    </item>
    <item>
      <title>Re: Replace null with value by looking back and ahead a certain number of days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627366#M185185</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I adopted your code for my data and it fails for one patientID when tested on a sample. The diag is a character field so I have made changes accordingly. I am getting the 'Array subscript out of range' error message and trying to figure that out. Please let me know what I'm doing wrong. Thank you!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   set nonspec5;
   where PatientID in ('4000052628862:40000526288622'
                      ,'4000052628862:40000526288627'
                      ,'4000122628869:40001226288692'
                      ,'4000322628869:40003226288692'
);
   format clistart date9.;
   keep PatientID clistart validDiag ;
run;

%let begdate=01jan2012;
%let enddate=31dec2012;

data want (drop=d);
  set have (where=(validDiag^=' ')  in=in_diag)
      have (in=in2);
  by patientid;

  array diag_hist {%sysevalf("&amp;amp;begdate"d):%sysevalf("&amp;amp;enddate"d)} $ _temporary_;

  if first.patientid then call missing(of diag_hist{*});
  if in_diag then diag_hist{clistart}=validDiag;

  if in2;
  if validDiag=' ' then do d=0,-2,2,-5,5,-9;
    if diag_hist{clistart+d}^=' ' then do; 
      new_diag=diag_hist{clistart+d};
      leave;
    end;
  end;
  if new_diag=' ' then new_diag=validDiag;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here's my log:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;28 data want (drop=d);&lt;BR /&gt;29 set have (where=(validDiag^=' ') in=in_diag)&lt;BR /&gt;30 have (in=in2);&lt;BR /&gt;31 by patientid;&lt;BR /&gt;32&lt;BR /&gt;33 array diag_hist {%sysevalf("&amp;amp;begdate"d):%sysevalf("&amp;amp;enddate"d)} $ _temporary_;&lt;BR /&gt;34&lt;BR /&gt;35 if first.patientid then call missing(of diag_hist{*});&lt;BR /&gt;36 if in_diag then diag_hist{clistart}=validDiag;&lt;BR /&gt;37&lt;BR /&gt;38 if in2;&lt;BR /&gt;39 if validDiag=' ' then do d=0,-2,2,-5,5,-9;&lt;BR /&gt;40 if diag_hist{clistart+d}^=' ' then do;&lt;BR /&gt;41 new_diag=diag_hist{clistart+d};&lt;BR /&gt;42 leave;&lt;BR /&gt;43 end;&lt;BR /&gt;44 end;&lt;BR /&gt;45 if new_diag=' ' then new_diag=validDiag;&lt;BR /&gt;46 run;&lt;/P&gt;&lt;P&gt;ERROR: Array subscript out of range at line 40 column 8.&lt;BR /&gt;in_diag=0 in2=1 PatientID=4000322628869:40003226288692 CliStart=06JAN2012 validDiag= FIRST.PatientID=0 LAST.PatientID=0 d=-9&lt;BR /&gt;new_diag= _ERROR_=1 _N_=45&lt;BR /&gt;NOTE: The SAS System stopped processing this step because of errors.&lt;BR /&gt;NOTE: There were 14 observations read from the data set WORK.HAVE.&lt;BR /&gt;WHERE validDiag not = ' ';&lt;BR /&gt;NOTE: There were 32 observations read from the data set WORK.HAVE.&lt;BR /&gt;WARNING: The data set WORK.WANT may be incomplete. When this step was stopped there were 30 observations and 4 variables.&lt;BR /&gt;WARNING: Data set WORK.WANT was not replaced because this step was stopped.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 01:10:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627366#M185185</guid>
      <dc:creator>tk89</dc:creator>
      <dc:date>2020-02-26T01:10:57Z</dc:date>
    </item>
    <item>
      <title>Re: Replace null with value by looking back and ahead a certain number of days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627378#M185195</link>
      <description>&lt;P&gt;Read the log, it's all there! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;ERROR: Array subscript out of range&lt;FONT color="#FF0000"&gt; at line 40&lt;/FONT&gt; column 8.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Line 40 is:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;40 if diag_hist{clistart+d}^=' ' then do;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The values are:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;in_diag=0 in2=1 PatientID=4000322628869:40003226288692 &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;CliStart=06JAN2012&lt;/FONT&gt;&lt;/STRONG&gt; validDiag= FIRST.PatientID=0 LAST.PatientID=0 &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;d=-9&lt;/FONT&gt;&amp;nbsp;&lt;/STRONG&gt;new_diag= _ERROR_=1 _N_=45&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;06JAN2012 - 9&lt;/FONT&gt; is outside of the array bounds.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Either have an array then spans more then a year, or test the index value before trying to read the array.&lt;/P&gt;
&lt;P&gt;Which is exactly was the message reads:&amp;nbsp;&lt;FONT face="courier new,courier" color="#FF0000"&gt;&lt;STRONG&gt;Array subscript out of range&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 02:20:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627378#M185195</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-02-26T02:20:57Z</dc:date>
    </item>
    <item>
      <title>Re: Replace null with value by looking back and ahead a certain number of days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627527#M185276</link>
      <description>&lt;P&gt;Chris:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One of the OP's data rows has DIAG=0 but has a duplicate date of another with DIAG^=0.&amp;nbsp; I think you'll need to insert "diag_hist(admit_dt}" at the front of the coalesce arguments.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 15:02:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627527#M185276</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-02-26T15:02:43Z</dc:date>
    </item>
    <item>
      <title>Re: Replace null with value by looking back and ahead a certain number of days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627530#M185278</link>
      <description>&lt;P&gt;You have an observation on the 6th of Jan in 2016.&amp;nbsp; The loop searches up to 9 days before (ie. late December 2015).&amp;nbsp; So either establish an earlier lower bound for the array, or test for date exceptions.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 15:05:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627530#M185278</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-02-26T15:05:22Z</dc:date>
    </item>
    <item>
      <title>Re: Replace null with value by looking back and ahead a certain number of days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627663#M185327</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;Thank you! Now I understand. One last question: Here, we are looking at a particular point of data in the array, how can I modify it to look at a range? For example, for date 15feb2016, at d=-5, we are looking at 10feb2016 in the array. Instead, how can I look at all the claims that exist between 15feb2016 and 10feb2016 (i.e., on 15,14,13,12,11, and 10feb2016)? I ask because for d=-5, I look at all claims within the 5 days and pick the one with the least Rank( a variable I pull from a different table). Please let me know if this is out of scope and I need to post this as a separate question. I'm sorry that my question was not clear from the get go, will do better henceforth.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Feb 2020 20:53:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627663#M185327</guid>
      <dc:creator>tk89</dc:creator>
      <dc:date>2020-02-26T20:53:42Z</dc:date>
    </item>
    <item>
      <title>Re: Replace null with value by looking back and ahead a certain number of days</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627844#M185425</link>
      <description>&lt;P&gt;You could create an array RANK_HIST analogous to the DIAG_HIST array.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you would still have to change the logic of the loop.&amp;nbsp; Instead of leaving the loop with the first non-zero DIAG_HIST you have to maintain another variable, say D_MIN, which is the value of D corresponding to the minimum RANK_HIST for non-zero DIAG_HIST.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;D_MIN starts out as missing.&amp;nbsp; As you go over each range (i.e. the offset D goes from start-of-range to end-of-range), whenever you hit a non-zero DIAG_HIST, then if D_MIN is missing, set it to D.&amp;nbsp; But if D_MIN is not missing, compare the RANK_HIST for D to the same for D_MIN, and update D_MIN accordingly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At the end of each range, if D_MIN is not missing, no more ranges are checked.&amp;nbsp; And finally if D_MIN is not missing, assign a value to NEW_DIAG.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something like this (totally untested):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  new_diag =coalesce(diag_hist{date},diag);

  array strt   {3} _temporary_  (1,3,6);
  array finish {3} _temporary_  (2,5,9);


  d_min=.;

  if new_diag^=0 then do r=1 to 3 until (d_min^=.);   
    do sign=-1,1 until (d_min^=.);
      do d = sign*strt{r} to sign*finish{r} by sign;
        if diag_hist{date+d}^=0 then do;
          if d_min=. then d_min=d;   else
          if rank_hist{date+d} &amp;lt; rank_hist{date+d_min} then d_min=d;
        end;
      end;
    end;
  end;
  if d_min^=. then new_diag=diag_hist{date+d_min};
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the first inner loop ("do sign=-1,1 …")&amp;nbsp; provides a way to set up this sequence of 6 ranges:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;do d=-1 to -2 by -1&lt;/LI&gt;
&lt;LI&gt;do d=1 to 2 by 1&lt;/LI&gt;
&lt;LI&gt;do d=-3 to -5 by -1&lt;/LI&gt;
&lt;LI&gt;do d=3 to 5 by 1&lt;/LI&gt;
&lt;LI&gt;do d=-6 to -9 by -1&lt;/LI&gt;
&lt;LI&gt;do d=6 to 9&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Feb 2020 10:43:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replace-null-with-value-by-looking-back-and-ahead-a-certain/m-p/627844#M185425</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-02-27T10:43:37Z</dc:date>
    </item>
  </channel>
</rss>

