BookmarkSubscribeRSS Feed
pink_poodle
Barite | Level 11

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?

 

8 REPLIES 8
ballardw
Super User

@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?

pink_poodle
Barite | Level 11

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.

ballardw
Super User

@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

pink_poodle
Barite | Level 11
It is an interesting idea about the mode. I think you forgot to attach the link.
Reeza
Super User
PROC UNIVARIATE calculates the mode. I think there's an example in the documentation. But...that means your data is transpose to a long format.
Reeza
Super User
SAS works better with long data, so I would transform to a long format. For #1 I'd run a proc freq and get the Counts/labels for variables where the percentages is the highest. You can use ORDER=FREQ to ensure the highest count is first.

I have no idea what #2 is asking or means.

An 'array' in SAS is different than many other languages, it's just a shortcut reference to other variables. If you want to do array/matrix type manipulations you could consider IML which is closer to R/Python.
pink_poodle
Barite | Level 11

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

 

Codility Lesson 8: Leaders

pink_poodle
Barite | Level 11
I agree, it is not as straight-forward to do array slices, like Python list slices.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 8 replies
  • 1076 views
  • 2 likes
  • 3 in conversation