Hi There,
what is the syntax to print only categorical variables in a dataset?
For eg: Syntax to print only numeric variables in a dataset is
proc print data=sashelp.cars;
var _numeric_;
run;
Use
var _character_;
Thank you very much Pagiemiller 🙂
SAS has two variable types. Floating point numbers and fixed length character strings. Whether a variable is categorical or not is something that SAS does not know. You would have to tell it.
The _NUMERIC_ variable list includes all of the numeric variables (whether they are "categorical" or not). The _CHARACTER_ variable list includes all of the character variables (whether they have defined categories or are free text).
Since SAS doesn't know which variables are 'categorical' you might look into the Proc freq NLEVELS option
ods select nlevels; proc freq data=sashelp.class nlevels; run;
As an example. The ODS SELECT option will have Proc Freq only display the NLEVELS table and not every single value of every single variable in the data set.
This shows the number of different values a variable has. In this case AGE, which is numeric, shows 6 values and might be considered "categorical". Name, which has 19 values in 19 records seems like less a "category" than "free text". Sex, character with 2 levels, likely would be a category.
The common meanings of the variable name in the SASHELP.CLASS data set may be sufficient but this technique may be helpful exploring a new data set.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.