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

How can I get the maximum of a character field using proc means? I get "ERROR: Variable TERM_CODE in list does not match type prescribed for this list", with the following code.

 

proc means data=stulasr.vday_summary_eg max;

var TERM_CODE;

run;

 

I would find it surprising if proc means were unable to generate order statistics for a alphanumeric sample. proc sql works.

 

Ultimately, I'm trying to create a character macro variable for downstream use in a proc sql step. Perhaps there's a better approach in general. 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

PROC MEANS is optimized for generating statistics on numeric variables. You can use character variables as CLASS variables with PROC MEANS.  You could use that to get counts:

proc summary data=have nway;
  class term_code;
  output out=want ;
run;
data _null_;
  set want end=eof;
  if _n_=1 then put 'MIN ' term_code=;
  if eof then put 'MAX ' term_code=;
run;

If you just want min and max of a character variable then PROC SQL is a good choice.

create table min_max as 
select min(term_code) as min_term_code
    , max(term_code) as max_term_code
from have
;

If you want the full distribution of values use PROC FREQ.   Or perhaps you want PROC RANK?

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Max/Min of a character column is undefined.

 

PROC MEANS will not operate on character variables.

 

Can you give us a small example of the data you have and the output you want?

--
Paige Miller
tfarkas
Obsidian | Level 7
Thanks. Edited my post.
PaigeMiller
Diamond | Level 26

Ultimately, I'm trying to create a character macro variable for downstream use in a proc sql step. Perhaps there's a better approach in general. 

 

I don't think there's enough information in these two sentences for anybody to help.

 

Again, I ask for a small example of your data, and the desired output. (And put it in a reply at the bottom of this thread instead of at the top of the thread).

--
Paige Miller
novinosrin
Tourmaline | Level 20

Hi @tfarkas   

 

"How can I get the maximum of a character field using proc means?"

 

The idea or the design that you are trying to flow downstream in itself is not the best in my humble opinion and I am pretty certain most people would mostly likely will advice to redesign your flow that accommodates robust approaches in SAS.

 

Yes, I agree proc sql works fine and so will datastep like

data w;
length want $1;
do char='c','b','a';
want=char<>want;
end;
run;

However the analysis variables in sas procs doesn't quite do the collating sequence of sorts be it EBCIDIC or ASCII. 

 

In any event, I am sure the pool of experts would be pleased to offer you a clean solution if you can post your requirement clearly.

 

 

 

Tom
Super User Tom
Super User

PROC MEANS is optimized for generating statistics on numeric variables. You can use character variables as CLASS variables with PROC MEANS.  You could use that to get counts:

proc summary data=have nway;
  class term_code;
  output out=want ;
run;
data _null_;
  set want end=eof;
  if _n_=1 then put 'MIN ' term_code=;
  if eof then put 'MAX ' term_code=;
run;

If you just want min and max of a character variable then PROC SQL is a good choice.

create table min_max as 
select min(term_code) as min_term_code
    , max(term_code) as max_term_code
from have
;

If you want the full distribution of values use PROC FREQ.   Or perhaps you want PROC RANK?

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 4303 views
  • 0 likes
  • 4 in conversation