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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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