DATA Step, Macro, Functions and more

Number of levels in character variables

Reply
Regular Contributor
Posts: 183

Number of levels in character variables

I am trying to create a macro that calculates number of levels (distinct categories) for all the character variables in a dataset. Currently, i am using PROC SQL to calculate distinct categories. If i have 100 variables, i have to run PROC SQL 100 times in a loop. Is there a better way to do it? Is "stackods" kind of option available in PROC SQL so that i can reshape my dataset easily?


Sample Code -

proc sql;

create table abc as

select count(distinct(sex)) as sex_levels

from sashelp.class;

quit;

Sample Output -

VariableN_Levels
Sex2
Age6

It's just the sample code and output for 2 variables. I need to do it for all the character variables in a dataset.

Note : All my categorical variables are stored as character variables (string) in my dataset.

Thanks in anticipation!

Super User
Super User
Posts: 7,039

Re: Number of levels in character variables

PROC FREQ NLEVELS ....

Regular Contributor
Posts: 227

Re: Number of levels in character variables

I am with Tom: proc.freq.nlevels provides the answer, ... for the variables in the data-set.

I have written several papers on extrapolating from n-levels.

the information you are seeking is in contents output data set which has a variable type -- in (1:n, 2:c)

you have to sort both out-nlevels and out-contents by name in order to merge them.

check this SASware ballot item.

Regular Contributor
Posts: 227

Re: Number of levels in character variables

Posted in reply to Ron_Fehd_macro_maven

%let data = sashelp.class;

%let data = sashelp.heart;

proc freq data = &data

         (keep = _character_)

          nlevels;

          ods       exclude onewayfreqs;

          ods       output

          nlevels = out_n_levels

         (keep    =  tablevar nlevels

          rename  = (tablevar = name

                     nlevels  = n_levels));

         *to match  out_contents;

proc print;

Ask a Question
Discussion stats
  • 3 replies
  • 906 views
  • 1 like
  • 3 in conversation