## Help with Do Loop error message

Solved
Super Contributor
Posts: 506

# Help with Do Loop error message

Hi Everyone,

I wrote the below code to calcuate the number of time price < level in the 3 records.

While SAS gives the result as expected, in the log there is ERROR code shows up. I think it notice that at the end of the file, there isnt enought 3 records for proper calculation.

So can you show me what to add to eliminate this error and SAS just give me the result as it does right now.

Also, how can I make SAS calculate only record that has enought 3 row of data.

Thank you,

HHC

data have;
input time price level;
datalines;
1     5     9
2     2     3
3     8     5
4     10    5
5     12    6
6 5 100

;run;

data want;
set have nobs=totalobs;
i+1;
t=0;
do j=i to i+2;

set have (keep = price level rename=(price=prc level=lv)) point=j;
if prc>lv then t=t+1;
end;
run;

Accepted Solutions
Solution
‎10-05-2014 01:56 PM
PROC Star
Posts: 8,164

## Re: Help with Do Loop error message

With you current code t gets a value of 1 for record 5. Was that what you want? If not, other than that, the following produces the same result you are currently getting:

data want;

set have nobs=totalobs;

i+1;

t=0;

if i le totalobs-2 then do j=i to i+2;

set have (keep = price level rename=(price=prc level=lv)) point=j;

if prc>lv then t=t+1;

end;

else do;

call missing(prc);

call missing(lv);

end;

run;

All Replies
Solution
‎10-05-2014 01:56 PM
PROC Star
Posts: 8,164

## Re: Help with Do Loop error message

With you current code t gets a value of 1 for record 5. Was that what you want? If not, other than that, the following produces the same result you are currently getting:

data want;

set have nobs=totalobs;

i+1;

t=0;

if i le totalobs-2 then do j=i to i+2;

set have (keep = price level rename=(price=prc level=lv)) point=j;

if prc>lv then t=t+1;

end;

else do;

call missing(prc);

call missing(lv);

end;

run;

Super User
Posts: 8,114

## Re: Help with Do Loop error message

You can also use FIRSTOBS= option instead of POINT= to do a look ahead.

data have;

input time price level @@;

put (time price level) (=);

datalines;

1 5 9 2 2 3 3 8 5 4 10 5 5 12 6 6 5 100

;;;;

data want;

set have nobs=totalobs;

if _n_ < totalobs then set have(keep=price level rename=(price=price2 level=level2) firstobs=2) ;

else call missing(price2,level2);

if _n_ <totalobs-1 then set have(keep=price level rename=(price=price3 level=level3) firstobs=3) ;

else call missing(price3,level3);

t = sum(price > level, price2 > level2, price3 > level3);

put (time t price level price2 level2 price3 level3) (=);

run;

Super Contributor
Posts: 506

## Re: Help with Do Loop error message

Thank you, Arthur and Tom for your help.

HHC

🔒 This topic is solved and locked.