BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
dbjosiah
Obsidian | Level 7

I'm looking for a way to subset a dataset to only its categorical variables (or only continuous, doesn't matter) without knowing the names of the variables. So far haven't found anything that matches what I need. Thanks for any help.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Example:

 

data cars;
    set sashelp.cars(keep=_character_);
run;
--
Paige Miller

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

Example:

 

data cars;
    set sashelp.cars(keep=_character_);
run;
--
Paige Miller
dbjosiah
Obsidian | Level 7
That's so simple it's embarrassing. Thanks.
Reeza
Super User
It you can assume categorical variables are character.
For example, cylinders in SASHELP.CARS is a categorical categorical variable but numeric.
dbjosiah
Obsidian | Level 7
Thanks Reeza. Once I have the real continuous ones out, I'm subsetting the long character ones by hand.
Reeza
Super User

If you use SAS Studio, check out the characterize data set task. It does this automatically by looking at the # of distinct values within variables. Anything less than a threshold will be classified as categorical IIRC. 

 

dbjosiah
Obsidian | Level 7
That would be useful! But I don't use Studio.
Tom
Super User Tom
Super User

@dbjosiah wrote:
Thanks Reeza. Once I have the real continuous ones out, I'm subsetting the long character ones by hand.

You could try NLEVELS output of PROC FREQ to quickly find the number of levels for each variable.

proc freq nlevels data=have;
  tables _all_ / noprint;
run;
dbjosiah
Obsidian | Level 7
I've used nlevels, but never like that! Much better than hand subsetting. Thanks!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1542 views
  • 3 likes
  • 4 in conversation