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

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;

 

 

twerwath_0-1623259760933.png

 

 

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
This may get you there but you may also need more to account for ties.
Test it and let me know if meets your needs.

if not missing(index) then names[i] = vname(values[index]);

View solution in original post

7 REPLIES 7
Reeza
Super User
Please show the log and error. It looks like you haven't handled the case when the index isn't valid - which may happen if you have missing values or ties.
Also, this would be drastically simpler in a long data set rather than a wide data set.

twerwath
Obsidian | Level 7

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?

Reeza
Super User
Well, what do you want to do when you don't have a top 3? And only have two or one?
twerwath
Obsidian | Level 7

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.

Reeza
Super User
This may get you there but you may also need more to account for ties.
Test it and let me know if meets your needs.

if not missing(index) then names[i] = vname(values[index]);
Tom
Super User Tom
Super User

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;
twerwath
Obsidian | Level 7

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.

 

 

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!

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