BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

Hi,

I fail to find the bug in my code and I appreciate it if you could help.

It is a stock data with tic, Open, High, Low, Close price.

The sequence is that:

For a given day, focus on that day and the last 3 day (_n_ to _n_ -3)

- take the High and Low of day (_n_ -3) and assign to Highest = high(_n_ -3) and lowest = Low(_n_ -3)

- Move to day _n_-2, if  Low(_n_ -3) < open(_n_ - 2) <High(_n_ -3) Then it is a qualify day and DO;

# count = count +1;

# update the range by: Highest = MAX(current highest, high(_n_ -2)); Lowest= MIN(current lowest, low(_n_ -2));

- Move to day _n_-1 ....

- If anyday is not qualify, exit the loop and create day_leave.

 

So in my sample data, for day2: day 0,1  should be a qualify day. so the Count should be 2 instead of 3. Day_leave of 2 is correct.

for day4, count=0 is correct but day_leave should be day2.

for day 5, the count =4, should be 3

Thank you,

HHC

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 < o < 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;

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
hhchenfx
Barite | Level 11

I need to use do n = max(_n_-3+1,1) to _n_ by 1;

+1 because the beginning (_N_-3) is the start and shouldn't be check.

 

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 < o < 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;

View solution in original post

1 REPLY 1
hhchenfx
Barite | Level 11

I need to use do n = max(_n_-3+1,1) to _n_ by 1;

+1 because the beginning (_N_-3) is the start and shouldn't be check.

 

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 < o < 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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 508 views
  • 0 likes
  • 1 in conversation