BookmarkSubscribeRSS Feed
buffalol
Fluorite | Level 6

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. 

 

6 REPLIES 6
art297
Opal | Level 21

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

 

buffalol
Fluorite | Level 6

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

art297
Opal | Level 21

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

 

buffalol
Fluorite | Level 6
Great thanks, this now runs, it doesn't look the way it should yet, but I think I know why. Will report back.
art297
Opal | Level 21

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

Tom
Super User Tom
Super User

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;

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 923 views
  • 0 likes
  • 3 in conversation