BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Emma_at_SAS
Lapis Lazuli | Level 10

I have a dataset with 500 variables. I want to run proc freq and proc means for the character and numeric variables. SAS is great in handling _character_ and _numeric_ variables; however, the character variables in my dataset are coded as numeric because each character variable is a couple of levels that are saved with numbers (e.g., 1,2,3,4 then I have format to assign the character description in the output). Usually, I manually separate the character variables. I wonder if you have a suggestion to write a code to help me with distinguishing the character variables from the numeric variables. Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You could just define those variables as character when you create them.  So instead of defining those variables with values of 1 and 2 as numeric define then as character with values of '1' and '2' and create character formats instead of numeric formats.

 

What form does your original data come in?  If it is text file (like a CSV file) then just adjust the code of your data step that is reading it to define them as character.

 

Sounds like you want to distinguish the categorical variables from the continuous (or at least multi level)  variables whether or not the variables are defined as character.  The NLEVEL output of PROC FREQ is very useful for this.

ods output nlevels=nlevels;
proc freq nlevels data=have ;
  tables _all_ / noprint;
run;

You could then set some upper limit on the number of levels below which you want to consider the variable as categorical.

proc sql noprint;
select nliteral(TableVar) into :varlist separated by ' '
  from nlevels 
  where nlevels < 5 
;
quit;

You can then use that list of variables in your other code.  For example you might use that list as which variable to generate PROC FREQ tables for.  Or perhaps to exclude them from PROC MEANS analysis of the numeric variables.

proc freq data=have ;
  tables &varlist;
run;
proc means data=have(drop=&varlist);
run;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

You could just define those variables as character when you create them.  So instead of defining those variables with values of 1 and 2 as numeric define then as character with values of '1' and '2' and create character formats instead of numeric formats.

 

What form does your original data come in?  If it is text file (like a CSV file) then just adjust the code of your data step that is reading it to define them as character.

 

Sounds like you want to distinguish the categorical variables from the continuous (or at least multi level)  variables whether or not the variables are defined as character.  The NLEVEL output of PROC FREQ is very useful for this.

ods output nlevels=nlevels;
proc freq nlevels data=have ;
  tables _all_ / noprint;
run;

You could then set some upper limit on the number of levels below which you want to consider the variable as categorical.

proc sql noprint;
select nliteral(TableVar) into :varlist separated by ' '
  from nlevels 
  where nlevels < 5 
;
quit;

You can then use that list of variables in your other code.  For example you might use that list as which variable to generate PROC FREQ tables for.  Or perhaps to exclude them from PROC MEANS analysis of the numeric variables.

proc freq data=have ;
  tables &varlist;
run;
proc means data=have(drop=&varlist);
run;
Emma_at_SAS
Lapis Lazuli | Level 10
Thank you, Tom. This is brilliant!
Thank you, Reeza, for your thumbs up!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 2 replies
  • 299 views
  • 3 likes
  • 2 in conversation