BookmarkSubscribeRSS Feed
JackZ295
Pyrite | Level 9

Is there a way to find the mode and the frequency of the mode across a series of categorical variables ex. not just the mode for one variable, but the most frequent response among several variables?

 

Ex. Several items are scored as follows: 

 

1: No, strongly disagree

2: No, somewhat disagree

3: Neither agree nor disagree

4: Yes, somewhat agree

5: Yes, strongly agree

 

data sample; 
input id A1Q1 A2Q1 RFQ1 SE1Q1 SE2Q1 SE3Q1 SE4Q1 I1Q1 I2Q1 I3Q1 I4Q1; 
datalines;
1 2 1 1 3 3 4 2 5 3 2 
2 1 1 1 1 1 1 1 1 1 1
3 2 3 4 5 1 2 3 4 5 1 
4 1 2 3 4 5 5 4 3 2 1
5 1 1 1 1 1 1 1 1 1 1
;

As an example, how can I find the mode and the frequency of the mode of this data set across the variables 

A1Q1 A2Q1 RFQ1 SE1Q1 SE2Q1 SE3Q1 SE4Q1 I1Q1 I2Q1 I3Q1 I4Q1?

 

9 REPLIES 9
Kurt_Bremser
Super User

Do a transpose and freq:

data sample; 
input id A1Q1 A2Q1 RFQ1 SE1Q1 SE2Q1 SE3Q1 SE4Q1 I1Q1 I2Q1 I3Q1; 
datalines;
1 2 1 1 3 3 4 2 5 3 2 
2 1 1 1 1 1 1 1 1 1 1
3 2 3 4 5 1 2 3 4 5 1 
4 1 2 3 4 5 5 4 3 2 1
5 1 1 1 1 1 1 1 1 1 1
;

proc transpose data=sample out=trans;
by id;
run;

proc freq data=trans;
tables col1;
run;

I removed one variable from the input, as your datalines have only 11 columns.

JackZ295
Pyrite | Level 9

Hi @Kurt_Bremser , thank you for your help. However, I actually have the find the mode and frequency of the mode for 50 sets of these (ex. 

A1Q1 A2Q1 RFQ1 SE1Q1 SE2Q1 SE3Q1 SE4Q1 I1Q1 I2Q1 I3Q1

A1Q2 A2Q2 RFQ2 SE1Q2 SE2Q2 SE3Q2 SE4Q2 I1Q2 I2Q2 I3Q2

A1Q3 A2Q3 RFQ3 SE1Q3 SE2Q3 SE3Q3 SE4Q3 I1Q3 I2Q3 I3Q3

.....

all the way to Q50.)

 

I just gave an abbreviated version as an example. I have to find a separate mode and frequency of the mode for each set of 11 variables. 

All of the variables are in the same data set. The data set is set up such that there is one row per participant. 

 

Any advice regarding this?

JackZ295
Pyrite | Level 9

Hi @Kurt_Bremser, thanks again for your help. I should be a little more clear in terms of what the data set looks like. It more or less looks like this: 

 

ID A1Q1 A2Q1 RFQ1 SE1Q1 SE2Q1 SE3Q1 SE4Q1 I1Q1 I2Q1 I3Q1 I4Q1  A1Q2 A2Q2 RFQ2 SE1Q2...

1 [Responses from likert scale are here]

2

3

4

5

6

7

8

9

10

I need to find the mode and frequency of the mode in each set of 11: 

A1Q1 A2Q1 RFQ1 SE1Q1 SE2Q1 SE3Q1 SE4Q1 I1Q1 I2Q1 I3Q1 I4Q1  (mode and frequency of mode for set 1)

A1Q2 A2Q2 RFQ2 SE1Q2 SE2Q2 SE3Q2 SE4Q2 I1Q2 I2Q2 I3Q2 I4Q2  (mode and frequency of mode for set 2) 

.....                  

                                                                                                                      (mode and frequency of mode for set 50)

 

Any advice?

 

I also need to find a way to output this data so that I can perform calculations using the frequency of the mode. 

JackZ295
Pyrite | Level 9

@Kurt_Bremser, thanks again for your help. I would be treating each set of 11 variables as one row.

Kurt_Bremser
Super User

Since SAS procedures are designed to work vertically, you would transpose your "rows" into vertical groups:

data sample; 
input id A1Q1 A2Q1 RFQ1 SE1Q1 SE2Q1 SE3Q1 SE4Q1 I1Q1 I2Q1 I3Q1; 
datalines;
1 2 1 1 3 3 4 2 5 3 2 
2 1 1 1 1 1 1 1 1 1 1
3 2 3 4 5 1 2 3 4 5 1 
4 1 2 3 4 5 5 4 3 2 1
5 1 1 1 1 1 1 1 1 1 1
;

proc transpose data=sample out=trans;
by id;
run;

data trans1;
set trans;
varset = input(scan(_name_,2,'Q'),best.);
run;

proc freq data=trans1 noprint;
tables varset*col1 /out=want;
run;

The crucial step here is the creation of the varset variable; if your name structure is as shown, using the "Q" as delimiter makes this very simple.

JackZ295
Pyrite | Level 9

Hi @Kurt_Bremser thanks again for your help. Would you mind further explaining what this line of code does? 

 

varset = input(scan(_name_,2,'Q'),best.);

Thanks. 

Kurt_Bremser
Super User

Sorry for being late, but the weekend ...


@JackZ295 wrote:

Hi @Kurt_Bremser thanks again for your help. Would you mind further explaining what this line of code does? 

 

varset = input(scan(_name_,2,'Q'),best.);

Thanks. 


scan() splits out "words" from the first argument; the second argument is the "word" count, and the (optional) third argument contains a delimiter. Note my use of words in quotes, as you can use this in a lot of ways that don't relate to our usual concept of words in language.

So what this does is it takes everything out of the source after the first "Q", either up to the end of the source or to a possible second "Q".

Then I use input() to convert the string to a number.

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!

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
  • 9 replies
  • 2433 views
  • 0 likes
  • 2 in conversation