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.
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?
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?
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).
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.
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?
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!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: