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: Call for Content

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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