<?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: Help with Do Loop dynamic range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Do-Loop-dynamic-range/m-p/786629#M251198</link>
    <description>&lt;P&gt;I need to use&amp;nbsp;do n = max(_n_-3+1,1) to _n_ by 1;&lt;/P&gt;
&lt;P&gt;+1 because the beginning (_N_-3) is the start and shouldn't be check.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data w;
keep day tic open	close	high	low 	count 		highest lowest day_leave;
set h;

highest = lag3(high); *assign the first hig/low;
lowest =lag3(low);
count=0;	

do n = max(_n_-3+1,1) to _n_  by 1;
	set h (rename = (day = i tic=t  open=o  high=h  low=l  close=c)) point=n;
		if lowest &amp;lt; o &amp;lt; highest then do;
			count=count+1; 
			highest = max(highest, h);
			lowest  = min(lowest, l);
			day_leave=i;
		end;
		else do; day_leave=i; leave;end; *good;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 19 Dec 2021 01:43:22 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2021-12-19T01:43:22Z</dc:date>
    <item>
      <title>Help with Do Loop dynamic range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Do-Loop-dynamic-range/m-p/786624#M251193</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I fail to find the bug in my code and I appreciate it if you could help.&lt;/P&gt;
&lt;P&gt;It is a stock data with tic, Open, High, Low, Close price.&lt;/P&gt;
&lt;P&gt;The sequence is that:&lt;/P&gt;
&lt;P&gt;For a given day, focus on that day and the last 3 day (_n_ to _n_ -3)&lt;/P&gt;
&lt;P&gt;- take the High and Low of day (_n_ -3) and assign to Highest = high(_n_ -3) and lowest = Low(_n_ -3)&lt;/P&gt;
&lt;P&gt;- Move to day _n_-2, if&amp;nbsp; Low(_n_ -3) &amp;lt; open(_n_ - 2) &amp;lt;High(_n_ -3) Then it is a qualify day and DO;&lt;/P&gt;
&lt;P&gt;# count = count +1;&lt;/P&gt;
&lt;P&gt;# update the range by:&amp;nbsp;Highest = MAX(current highest, high(_n_ -2)); Lowest= MIN(current lowest, low(_n_ -2));&lt;/P&gt;
&lt;P&gt;- Move to day _n_-1 ....&lt;/P&gt;
&lt;P&gt;- If anyday is not qualify, exit the loop and create day_leave.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in my sample data, for &lt;STRONG&gt;day2&lt;/STRONG&gt;: day 0,1&amp;nbsp; should be a qualify day. so the Count should be 2 instead of 3. Day_leave of 2 is correct.&lt;/P&gt;
&lt;P&gt;for day4, count=0 is correct but day_leave should be day2.&lt;/P&gt;
&lt;P&gt;for day 5, the count =4, should be 3&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data h;
input day tic $ open	close	high	low ;
datalines;
-1	a	40	30	45	10
-2	a	40	30	46	9
-3	a	40	30	47	20
0	a	40	30	60	7
1	a	55	30	50	10
2	a	100	30	160	20
3	a	50	61	70	40
4	a	40	30	60	20
5	a	30	10	40	10
6	a	66	15	80	5
7	a	75	9	100	5
8	a	120	55	120	5
9	b	40	30	50	10
10	b	40	30	60	20
11	b	50	61	70	40
12	b	50	61	70	40

;
run;


data w;
keep day tic open	close	high	low 	count 		highest lowest location day_leave;
set h;

highest = lag3(high); *assign the first hig/low;
lowest =lag3(low);
count=0;	

do n = max(_n_-3,1) to _n_  by 1;
	set h (rename = (day = i tic=t  open=o  high=h  low=l  close=c)) point=n;
		if lowest &amp;lt; o &amp;lt; highest then do;
			count=count+1; 
			highest = max(highest, h);
			lowest  = min(lowest, l);
			location= i;
		end;
		else do; day_leave=i; leave;end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Dec 2021 22:06:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Do-Loop-dynamic-range/m-p/786624#M251193</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2021-12-18T22:06:19Z</dc:date>
    </item>
    <item>
      <title>Re: Help with Do Loop dynamic range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-Do-Loop-dynamic-range/m-p/786629#M251198</link>
      <description>&lt;P&gt;I need to use&amp;nbsp;do n = max(_n_-3+1,1) to _n_ by 1;&lt;/P&gt;
&lt;P&gt;+1 because the beginning (_N_-3) is the start and shouldn't be check.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data w;
keep day tic open	close	high	low 	count 		highest lowest day_leave;
set h;

highest = lag3(high); *assign the first hig/low;
lowest =lag3(low);
count=0;	

do n = max(_n_-3+1,1) to _n_  by 1;
	set h (rename = (day = i tic=t  open=o  high=h  low=l  close=c)) point=n;
		if lowest &amp;lt; o &amp;lt; highest then do;
			count=count+1; 
			highest = max(highest, h);
			lowest  = min(lowest, l);
			day_leave=i;
		end;
		else do; day_leave=i; leave;end; *good;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 19 Dec 2021 01:43:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-Do-Loop-dynamic-range/m-p/786629#M251198</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2021-12-19T01:43:22Z</dc:date>
    </item>
  </channel>
</rss>

