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

Hi There, what is wrong with this code?

proc iml;

use kaplan;

read all var{monset Survival survival1} into DM;

close;

Survival1 = DM[,3]; Survival = DM[,2]; monset = DM[,1];                                                              /*Survivalival is second column*/  /*monset is first column*/

n = nrow(DM);                                                                              /*number of rows in the dataset = obs*/ /*THE N DOESNT NEED TO BE FIXED*/

segment = J(nrow(DM),1,1); *print segment;

do i = 2 to nrow(DM);

if monset=monset[i+1] then survival1=survival1[i+1];

if monset[i-1]=monset then survival1[i-1]=survival1;

if monset=monset[i-1] then survival1=survival1[i-1];

end;

create Mkaplan var{monset Survival survival1};

append;

quit;

ERROR: (execution) Invalid subscript or subscript out of range.

operation : [ at line 872 column 20

operands  : monset, _TEM1002

MONSET    141 rows      1 col     (numeric)

_TEM1002      1 row       1 col     (numeric)

       142

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

It's a valid error. You've told the  loop to go from 2 to the number of rows (nrow) yet reference i+1.

When i=nrow, you're asking the loop to go past the number of rows, which means your out of bounds.

Whenever you see this error you check your boundary conditions.

Perhaps your loop should go from i=1 to nrow(DM)-1 instead?

View solution in original post

2 REPLIES 2
Reeza
Super User

It's a valid error. You've told the  loop to go from 2 to the number of rows (nrow) yet reference i+1.

When i=nrow, you're asking the loop to go past the number of rows, which means your out of bounds.

Whenever you see this error you check your boundary conditions.

Perhaps your loop should go from i=1 to nrow(DM)-1 instead?

Rick_SAS
SAS Super FREQ

Yes. The message tells you that the MONSET vector has 141 rows and you are trying to access element 142.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 1797 views
  • 2 likes
  • 3 in conversation