Hello,
Below are the data I have and data I want:
data readin;
input y1-y6;
cards;
11 55 59 35 25 87
12 79 73 74 86 29
13 80 95 77 25 74
;
run;
data want;
set readin;
array values[*] y1-y6;
array large[3];
array names[3] $32;
do i = 1 to dim(large);
large[i] = largest(i,of values[*]);
index = whichn(large[i],of values[*]);
names[i] = vname(values[index]);
end;
drop i index;
run;
This code works as intended, except for the line with the VNAME statement (line 9). SAS returns the error "array subscript out of range" for this line. It runs successfully if that line is removed. To me, the logic makes sense, so I am not understanding why I am receiving this error. Can anyone enlighten me or provide a working code for this?
Thank you
Hi @Reeza
Yes, this appears to be the case, thank you. Here is a slightly edited readin dataset which produces the error:
data readin;
input y1-y6;
cards;
11 55 11 35 . 87
12 12 12 . . .
13 80 95 77 25 74
;
run;
Here is the log:
810
811
812 data want;
813 set readin;
814 array values[*] y1-y6;
815 array large[5];
816 array names[5] $32;
817 do i = 1 to dim(large);
818 large[i] = largest(i,of values[*]);
819 index = whichn(large[i],of values[*]);
820 names[i] = vname(values[index]);
821 end;
822 drop i index;
823
824 run;
ERROR: Array subscript out of range at line 820 column 24.
y1=12 y2=12 y3=12 y4=. y5=. y6=. large1=12 large2=12 large3=12 large4=. large5=. names1=y1 names2=y1
names3=y1 names4= names5= i=4 index=. _ERROR_=1 _N_=2
Is there any possible solution to this?
It would be best if missings were ignored or moved to the bottom, such that there would only one value listed in variable large1 if only one is non missing, only values listed in large1 and large2 if only two are non missing, etc.
As for ties, i'm not sure. There isn't really any preference for order in cases where the values are the same.
You have missing values (or ties?) such that there are not 3 "largest" values in some observations.
Just test before using the value of INDEX.
if index then names[i] = vname(values[index]);
Example:
data readin;
input y1-y6;
cards;
11 55 59 35 25 87
12 79 73 74 86 29
13 80 95 77 25 74
. . . . . .
;
run;
data want;
set readin;
array values[*] y1-y6;
array large[3];
array names[3] $32;
do i = 1 to dim(large);
large[i] = largest(i,of values[*]);
index = whichn(large[i],of values[*]);
if index then names[i] = vname(values[index]);
end;
drop i index;
run;
Of course, that makes sense. Not sure why I didn't think of this.
It seems both answers are basically the same. I tested it and this works well enough for my purposes. Thank you.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.