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;
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;
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;
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;
Thank you, Arthur and Tom for your help.
HHC
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.