BookmarkSubscribeRSS Feed
supersonic
Calcite | Level 5


how to calculate the mode, frequency of mode and no:of distinct categories of all character variables in a dataset.

Prefer proc univariate.

3 REPLIES 3
AncaTilea
Pyrite | Level 9

Supersonic,

You cannot use PROC UNVIARIATE to calculate statistics on categorical variables.

However, PROC FREQ or PROC SQL can give you this information:

proc freq data = sashelp.class;

  table sex;

  ods output OneWayFreqs = my_count;

run;

*in this case the 'mode' will be sex= M, since it has the highest number of occurrences?

proc sql;

select sex from (select sex, count(1) as count_sex from sashelp.class group

by sex)

having count_sex = max(count_sex);

quit;

....oh,well.

supersonic
Calcite | Level 5

the above eg will give only thetotal  frequency.

i need to calculate the mode, and its frequency and no:of distinct categories.

suppose i have a list of variables in a macro list &list

so i need to claculate the mode for all variables in that list.


AncaTilea
Pyrite | Level 9

ok, it's a bit hard for me to understand the  meaning of "mode" for categorical variables. So, what about this:

DATA TEMP;

  SET SASHELP.CLASS;

  IF NAME = "Janet" THEN NAME = "Jane";

  IF NAME = "James" THEN NAME = "Jane";

  IF . <AGE < 12 THEN AGE_GRP = "< 12 yrs  ";

  ELSE IF 12 <= AGE <= 13 THEN AGE_GRP = "12-13 yrs";

  ELSE AGE_GRP = "> 13 yrs";

RUN;

PROC FREQ DATA = TEMP;

  TABLES SEX AGE_GRP;

  ODS OUTPUT ONEWAYFREQS = MY_F(KEEP = F_: FREQUENCY);

RUN;

PROC UNIVARIATE DATA = MY_F;

  VAR FREQUENCY;

  WHERE F_AGE_GRP NE " ";

RUN;

PROC UNIVARIATE DATA = MY_F;

  VAR FREQUENCY;

  WHERE F_SEX NE " ";

RUN;

In the first step, I modify the data SASHELP.CLASS to create another categorical variable, age_grp.

Then you run a frequency for the categorical variables...output a data set that will give you no:of distinct categories. Then run a univariate on it...and you get the mode (if it exists)...

Smiley Happy

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1058 views
  • 0 likes
  • 2 in conversation