BookmarkSubscribeRSS Feed
hhchenfx
Barite | Level 11

Hi,

1 version of my code doesn't work and I know how to fix it. However, I am not clear and need your help.

The idea is that: when start = 10, look back 5 row:

if found value > 5, add 1 to the count (count_value_gt5)

if found value = 9, then look back 5 row:

if found value = 9 add 1 to the count (count9)

if found value = 99999 (which never have) exit 2 loop.

 

The thing is since the value 99999 is not there, the exit condition out=1 out=2 never hit and it cause trouble.

As you can see, for the not working code, for row2, count_value_gt5=3 is correct but count9=1 is wrong, it should be count9=2.

I am still not sure why it is 1. Even though I find the fix which is add the 1 line to check the end of loop.

Any explanation or other way of fix is very much appreciated.

HHC

data have;
input date start value ;
datalines;
1 1 1
2 10 5
3 1 9
4 1 9
5 1 5
6 10 5
7 2 100
8 2 0
9 2 -2
10 2 2
11 2 0
12 2 1
;run;

data want;
set have;
drop i vv:;
i+1;

if start=10 then do;
	out1=0;
	count_value_gt5=0;
	do j=i to i+5 until(out1=1);
	set have(keep = value rename=(value=vv1)) point = j;
		if vv1>5 then count_value_gt5=count_value_gt5+1;

		IF count_value_gt5=3 then do; exit=1; out1=1; end;
		ELSE
		if vv1=9 then do;
		out2=0;
		count9=0;
		do k=j to i+5 until (out2=1);
		set have(keep = value rename=(value=vv2)) point = k;
		if vv2=9 then count9=count9+1;
		else
		if vv2=9999999 then do; out2=1; out1=1;end;

		/*THIS IS THE CHECK THAT GIVE CORRECT ANSWER
		else
		if k=i+5 then do; out2=1; out1=1;end;
		*/

end; end; end;end;
run;

 

 

 

1 REPLY 1
ballardw
Super User

If you use any sort of stop value, like your 99999 then it is up to you to either make sure the value exists or provide an additional stopping rule. Possibly based on last observation in the Have set.

Maybe instead of an actual iterated loop you need a Do Until/While and control the incrementing in the loop.

 

Maybe there is some bit of timing where an OUTPUT statement would help.

Perhaps you need to stop dropping values of VV: variables to see if they are telling you something.

 

I can't follow the need or logic for this convoluted conditional "look back" or see the benefit so I am not going to spend a lot of headache generating time. When I see a variable named "date" that is basically just a sequence one strongly suspects an excess of simplification may be in shown in the example data, especially since Date values come with a large number of tools to work with.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 208 views
  • 1 like
  • 2 in conversation