<?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: Error in log when calculate min value within a window in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Error-in-log-when-calculate-min-value-within-a-window/m-p/797307#M264164</link>
    <description>&lt;P&gt;Thank you for your help!&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
    <pubDate>Fri, 18 Feb 2022 19:35:34 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2022-02-18T19:35:34Z</dc:date>
    <item>
      <title>Error in log when calculate min value within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-in-log-when-calculate-min-value-within-a-window/m-p/797059#M255826</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;For a give day, I lookback 2 day and look forward 3 day and&amp;nbsp;min value and the date of that min value occurs.&lt;/P&gt;
&lt;P&gt;My code below seems to get the job done but it give error in log as below.&lt;/P&gt;
&lt;P&gt;How can I fix it?&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;HHC&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;12744&lt;BR /&gt;12745 data want;&lt;BR /&gt;12746 set have;&lt;BR /&gt;12747 min=value;&lt;BR /&gt;12748 min_date=date;&lt;BR /&gt;12749 do n = _N_-2 to _N_+3 by 1;&lt;BR /&gt;12750 set have (keep = id date value rename=(id=id_1 date=date_1 value=value_1))point=n;&lt;BR /&gt;12751 if id_1 = id and value_1&amp;lt;min then do;&lt;BR /&gt;12752 min=value_1;&lt;BR /&gt;12753 min_date=date_1;&lt;BR /&gt;12754 end;&lt;BR /&gt;12755 end;&lt;BR /&gt;12756 run;&lt;/P&gt;
&lt;P&gt;id=1 date=1 value=1 min=1 min_date=1 n=5 id_1=1 date_1=4 value_1=8 _ERROR_=1 _N_=1&lt;BR /&gt;id=1 date=2 value=3 min=1 min_date=1 n=6 id_1=1 date_1=5 value_1=9 _ERROR_=1 _N_=2&lt;BR /&gt;id=2 date=6 value=80 min=20 min_date=7 n=18 id_1=2 date_1=8 value_1=60 _ERROR_=1 _N_=14&lt;BR /&gt;id=2 date=7 value=20 min=20 min_date=7 n=19 id_1=2 date_1=8 value_1=60 _ERROR_=1 _N_=15&lt;BR /&gt;id=2 date=8 value=60 min=20 min_date=7 n=20 id_1=2 date_1=8 value_1=60 _ERROR_=1 _N_=16&lt;BR /&gt;NOTE: There were 16 observations read from the data set WORK.HAVE.&lt;BR /&gt;NOTE: The data set WORK.WANT has 16 observations and 8 variables.&lt;BR /&gt;NOTE: DATA statement used (Total process time):&lt;BR /&gt;real time 0.03 seconds&lt;BR /&gt;cpu time 0.01 seconds&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input id date value;
datalines;
1 1 1
1 2 3
1 3 4
1 4 8
1 5 9
1 6 8
1 7 2
1 8 6
2 1 10
2 2 30
2 3 40
2 4 80
2 5 90
2 6 80
2 7 20
2 8 60
;run;

data want; 
set have;
min=value;
min_date=date;
do n = _N_-2 to _N_+3 by 1;
	set have (keep = id date value rename=(id=id_1 date=date_1 value=value_1))point=n;
	if id_1 = id and value_1&amp;lt;min then do;
		min=value_1;
		min_date=date_1;
	end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Feb 2022 23:06:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-in-log-when-calculate-min-value-within-a-window/m-p/797059#M255826</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-02-17T23:06:31Z</dc:date>
    </item>
    <item>
      <title>Re: Error in log when calculate min value within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-in-log-when-calculate-min-value-within-a-window/m-p/797106#M255846</link>
      <description>&lt;P&gt;This loop would never execute:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do n = _N_-2 to N+3 by 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since N is missing when N+3 is calculated, the TO value will be missing.&lt;/P&gt;
&lt;P&gt;I suspect this is not the code you really ran. Post the&amp;nbsp;&lt;EM&gt;complete&lt;/EM&gt;&amp;nbsp;(all code and messages) log of your step by copy/pasting into a window opened with this button:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/54552i914D97BE1B0F21E5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Feb 2022 22:27:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-in-log-when-calculate-min-value-within-a-window/m-p/797106#M255846</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-02-17T22:27:15Z</dc:date>
    </item>
    <item>
      <title>Re: Error in log when calculate min value within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-in-log-when-calculate-min-value-within-a-window/m-p/797110#M255850</link>
      <description>&lt;P&gt;You are right, the command should be:&amp;nbsp;do n = _N_-2 to &lt;STRONG&gt;_N_&lt;/STRONG&gt;+3 by 1;&lt;/P&gt;
&lt;P&gt;Still there is an error shown below.&lt;/P&gt;
&lt;PRE&gt;12745 data want;
12746 set have;
12747 min=value;
12748 min_date=date;
12749 do n = _N_-2 to _N_+3 by 1;
12750 set have (keep = id date value rename=(id=id_1 date=date_1 value=value_1))point=n;
12751 if id_1 = id and value_1&amp;lt;min then do;
12752 min=value_1;
12753 min_date=date_1;
12754 end;
12755 end;
12756 run;

id=1 date=1 value=1 min=1 min_date=1 n=5 id_1=1 date_1=4 value_1=8 _ERROR_=1 _N_=1
id=1 date=2 value=3 min=1 min_date=1 n=6 id_1=1 date_1=5 value_1=9 _ERROR_=1 _N_=2
id=2 date=6 value=80 min=20 min_date=7 n=18 id_1=2 date_1=8 value_1=60 _ERROR_=1 _N_=14
id=2 date=7 value=20 min=20 min_date=7 n=19 id_1=2 date_1=8 value_1=60 _ERROR_=1 _N_=15
id=2 date=8 value=60 min=20 min_date=7 n=20 id_1=2 date_1=8 value_1=60 _ERROR_=1 _N_=16
NOTE: There were 16 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 16 observations and 8 variables.
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.01 seconds&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input id date value;
datalines;
1 1 1
1 2 3
1 3 4
1 4 8
1 5 9
1 6 8
1 7 2
1 8 6
2 1 10
2 2 30
2 3 40
2 4 80
2 5 90
2 6 80
2 7 20
2 8 60
;run;

data want; 
set have;
min=value;
min_date=date;
do n = _N_-2 to _N_+3 by 1;
	set have (keep = id date value rename=(id=id_1 date=date_1 value=value_1))point=n;
	if id_1 = id and value_1&amp;lt;min then do;
		min=value_1;
		min_date=date_1;
	end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Feb 2022 23:09:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-in-log-when-calculate-min-value-within-a-window/m-p/797110#M255850</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-02-17T23:09:23Z</dc:date>
    </item>
    <item>
      <title>Re: Error in log when calculate min value within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-in-log-when-calculate-min-value-within-a-window/m-p/797143#M255869</link>
      <description>&lt;P&gt;Look at the values of _N_ in your ERROR messages. Your DO loop needs to take care of the edge cases at the beginning and end of the dataset. When _N_ =1, you try to read observations -1 and 0, which do not exist. Similar happens at the end, when you try to read obs 17, 18 and 19.&lt;/P&gt;</description>
      <pubDate>Fri, 18 Feb 2022 06:22:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-in-log-when-calculate-min-value-within-a-window/m-p/797143#M255869</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-02-18T06:22:34Z</dc:date>
    </item>
    <item>
      <title>Re: Error in log when calculate min value within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-in-log-when-calculate-min-value-within-a-window/m-p/797207#M255891</link>
      <description>&lt;PRE&gt;data have; 
input id date value;
datalines;
1 1 1
1 2 3
1 3 4
1 4 8
1 5 9
1 6 8
1 7 2
1 8 6
2 1 10
2 2 30
2 3 40
2 4 80
2 5 90
2 6 80
2 7 20
2 8 60
;

data want;
 if _n_=1 then do;
   if 0 then set have;
   declare hash h();
   h.definekey('date');
   h.definedata('value');
   h.definedone();

   array d{6} _temporary_;
   array v{6} _temporary_;
 end;
h.clear();
call missing(of d{*} v{*});

do until(last.id);
 set have;
 by id;
 h.add();
end;



do until(last.id);
 set have;
 by id;
 old_value=value;
 n=0;

 do _date=date-2 to date+3;
  if h.find(key:_date)=0 then do;n+1;d{n}=_date;v{n}=value; end;
 end;
 min=min(of v{*});
 idx=whichn(min,of v{*});
 min_date=d{idx};

 output;
end;

drop value idx _date n;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Feb 2022 12:32:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-in-log-when-calculate-min-value-within-a-window/m-p/797207#M255891</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-02-18T12:32:46Z</dc:date>
    </item>
    <item>
      <title>Re: Error in log when calculate min value within a window</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Error-in-log-when-calculate-min-value-within-a-window/m-p/797307#M264164</link>
      <description>&lt;P&gt;Thank you for your help!&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Fri, 18 Feb 2022 19:35:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Error-in-log-when-calculate-min-value-within-a-window/m-p/797307#M264164</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2022-02-18T19:35:34Z</dc:date>
    </item>
  </channel>
</rss>

