DATA Step, Macro, Functions and more

Array subscript out of range

Reply
Occasional Contributor
Posts: 9

Array subscript out of range

Dataset and code:

 

Data prices;

Input pID quarter cost1 cost2;

Datalines;

1 99Q1 2530 6930

1 99Q2 5636 7000

1 99Q3 4123 5899

1 99Q4 5000 1425

1 00Q1 2550 4777

1 00Q2 1999 2300

2 99Q1 2500 6930

2 99Q2 5636 6000

2 99Q3 4000 5899

2 99Q4 5000 1600

2 00Q1 2300 4777

2 00Q2 1000 2300 /*this continues for about 18 different products, each with 6 quarters*/

;

run;

 

Data trp;

Set prices;

By PID;

Retain A1 - A6;

Retain B1 - B6;

Array CostA(6) A1 - A6;

Array CostB(6) B1 - B6;

If first.pid then do;

    do j = 1 to 6;

    costa(J)= .;

    costb(J)= .;

end;

else I + 1;

costa(i)=  cost1; /*line 420*/

costb()= cost2;

if last.pid then output;

run;

 

The error I get looks something like this, made up the numbers for a and b 1-6:

Error: Array subscript out of range at line 420 column 1. 

PID = 1 Quarter = 99Q1 cost1=15000 cost2=13000 first.pid = 0 last.pid=0 a1=10000 a2=12000 a3=12000 a4=15200 a5=16000 a6=13500 b1=10000 b2=12000 b3=12000 b4=15200 b5=16000 b6=13500 j=. i=7 _ERROR_=1 _N_=7

 

What am I doing wrong?

Am trying to get each pid to have one line and all the quarters cost1 and cost 2 on that same wide line. 

 

PROC Star
Posts: 7,356

Re: Array subscript out of range

You never set the value for i when it's first.pid. Also, on the next line you refer to an array without indicating which cell. Thus, you should change those lines to:

If first.pid then do;
    do j = 1 to 6;
    costa(J)= .;
    costb(J)= .;
end;
I + 1;
costa(i)=  cost1; /*line 420*/
costb(i)= cost2;

Art, CEO, AnalystFinder.com

 

Occasional Contributor
Posts: 9

Re: Array subscript out of range

I'm still getting the same error. The missing cell number was a typo when I was writing the question.

PROC Star
Posts: 7,356

Re: Array subscript out of range

There were some other errors in your code. If you actually have no more than 6 entries per id, like in your example, the following should work:

Data prices;
Input pID quarter $ cost1 cost2;
Datalines;
1 99Q1 2530 6930
1 99Q2 5636 7000
1 99Q3 4123 5899
1 99Q4 5000 1425
1 00Q1 2550 4777
1 00Q2 1999 2300
2 99Q1 2500 6930
2 99Q2 5636 6000
2 99Q3 4000 5899
2 99Q4 5000 1600
2 00Q1 2300 4777
2 00Q2 1000 2300
;
run;
 
Data trp;
Set prices;
By PID;
Retain A1 - A6;
Retain B1 - B6;
Array CostA(6) A1 - A6;
Array CostB(6) B1 - B6;
  If first.pid then do;
    i=1;
    do j = 1 to 6;
      costa(J)= .;
      costb(J)= .;
    end;
  end;
  else I + 1;
costa(i)=  cost1; /*line 420*/
costb(i)= cost2;
if last.pid then output;
run;

Art, CEO, AnalystFinder.com

 

Occasional Contributor
Posts: 9

Re: Array subscript out of range

Great thanks, this now runs, it doesn't look the way it should yet, but I think I know why. Will report back.
PROC Star
Posts: 7,356

Re: Array subscript out of range

If you happen to be on an operating system that doesn't ignore case, you'll have to change the else statement to:

  else i + 1;

Art, CEO, AnalystFinder.com

Super User
Super User
Posts: 6,498

Re: Array subscript out of range

Your code is not doing anything with the QUARTER variable.  Does every PID have exactly 6 observations? Do they all cover '99Q1' to '00Q2'?  If not then you might need to somehow map the actual QUARTER value into the index into your array.

 

Assuming that each PID has at most 6 observations and that you want to preserve the actual values of QUARTER here is another way that takes a little less coding because it places the SET statement inside of a DO loop so that each iteration of the DATA step covers one PID value.

 

data trp;
  do _n_=1 by 1 until (last.pid) ;
    set prices;
    by PID;
    array qtr(6) $4 qtr1-qtr6 ;
    array CostA(6) A1 - A6;
    array CostB(6) B1 - B6;
    qtr(_n_)=quarter;
    costa(_n_)= cost1;
    costb(_n_)= cost2;
  end;
run;

 

 

Ask a Question
Discussion stats
  • 6 replies
  • 148 views
  • 0 likes
  • 3 in conversation