06-04-2015 04:33 PM
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 -
create table abc as
select count(distinct(sex)) as sex_levels
Sample Output -
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!
06-04-2015 06:15 PM
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.
06-04-2015 08:44 PM
%let data = sashelp.class;
%let data = sashelp.heart;
proc freq data = &data
(keep = _character_)
ods exclude onewayfreqs;
nlevels = out_n_levels
(keep = tablevar nlevels
rename = (tablevar = name
nlevels = n_levels));
*to match out_contents;