DATA Step, Macro, Functions and more

univariate

Reply
Contributor
Posts: 26

univariate


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

Prefer proc univariate.

Super Contributor
Posts: 543

Re: univariate

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.

Contributor
Posts: 26

Re: univariate

the above eg will give only thetotal  frequency.

i need to calculate the mode, and its frequency and noSmiley Surprisedf 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.


Super Contributor
Posts: 543

Re: univariate

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 noSmiley Surprisedf distinct categories. Then run a univariate on it...and you get the mode (if it exists)...

Smiley Happy

Ask a Question
Discussion stats
  • 3 replies
  • 182 views
  • 0 likes
  • 2 in conversation