I am taking this from a website "Codility" that tests computer programmers in languages other than SAS.
Array leader is an element that happens in more than half an array.
1.) How would you find an array leader using SAS? If it doesn't exist, please return -1.
Equi leader is an array leader that is shared between two parts of the same array, split on one of the elements.
2.) How would you count the number of equi leaders in an array using SAS?
@pink_poodle wrote:
I am taking this from a website "Codility" that tests computer programmers in languages other than SAS.
Array leader is an element that happens in more than half an array.
1.) How would you find an array leader using SAS? If it doesn't exist, please return -1.
Return what otherwise?
Equi leader is an array leader that is shared between two parts of the same array, split on one of the elements.
2.) How would you count the number of equi leaders in an array using SAS?
I think you may need to provide a bit more of an example as "split on one of the elements" doesn't mean anything obvious to me.
And since you are saying "two parts" of the same array, how do you define a "part" of the same array?
1) For example, if one variable stores array sequences, like 123456, and the other variable - their leaders. This 123456 array will not have a leader, so the leader variable should default to (-1).
2) I will put this answer in a separate response.
@pink_poodle wrote:
1) For example, if one variable stores array sequences, like 123456, and the other variable - their leaders. This 123456 array will not have a leader, so the leader variable should default to (-1).
But you have not answered what the value should be if there is a "leader".
Note SAS arrays consist of a number of variables so I cannot actually map a single string like 123456 to an array as that appears to be a single element. With a single element then the most common value is the ONLY value. If you mean to imply that is a number of variables that each hold a single digit then that may makes a little more sense.
This link has one way to find a MODE value of an array in SAS, mode being the most frequent value. However for your "leader" approach you need some additional code to check if the mode actually represents more than a single value
Here is a clarification about equi leaders:
Keeping in mind that a leader, simply put, is the most popular element in an array,
if we have an array 434442, the overall leader is 4.
The questions is, how many times will we come across equi leaders in this array?
To find out, we split the array sequentially like this:
1) 4 (leader = 4) and 34442 (leader = 4) ----------> bingo! first instance of an equi leader
2) 43 (leader = -1, i.e., not there) and 4442 (leader = 4)
3) 434 (leader = 4) and 442 (leader = 4) ---------> bingo! second instance of an equi leader
4) 4344 (leader = 4) and 42 (leader = -1)
5) 11223 (leader = -1) and 3 (leader = 3)
Therefore, the count of equi leaders is 2.
Reference
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!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.