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 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
Barite | Level 11

Thank you, Arthur and Tom for your help.

HHC

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 427 views
  • 3 likes
  • 3 in conversation