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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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;

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

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;

Tom
Super User Tom
Super User

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;

hhchenfx
Rhodochrosite | Level 12

Thank you, Arthur and Tom for your help.

HHC

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1050 views
  • 3 likes
  • 3 in conversation