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

Hello, I'm trying to find a way to swap the place of the two mean groups, Males and Females, in my data set so that when I run a proc ttest on the data set males are displayed first and then females so then my confidence interval for the difference between two means is (Males - Females) and not (Females - Males).  I've tried sorting by sex and class sex too, but I cannot get the Male group to come first in the output table.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Another option if you can't get an ORDER=Data to work is to assign a format that changes the order and use Order=Formatted

 

proc format;
value $mysex
'Male'=' Male'
'Female'='Female'
;
run;

proc ttest data=sashelp.heart alpha=0.05 order=Formatted;
class sex;
format sex $mysex.;
var weight;
run;

A side effect of most of the ODS output is that the space in front of the M that makes " M" come before "F" alphabetically is that the space is justified out of appearance.

 

Since T-test class variables should only have two levels it usually isn't that hard to come up with a format quickly.

It may be of interest that you can create the two groups for Ttest from a continuous or multivalued categorical variable by using a format.

proc format;
value tage
low-14='14 and younger'
15-high= 'Over 14';
run;

proc ttest data=sashelp.class alpha=0.05 order=Formatted;
class age;
format age tage.;
var weight;
run;

This can be very useful if you need a "this value" compared to "all the other values" of a category and pretty easy to code

proc format;
value oage
14='14'
other= 'Not 14';
run;

proc ttest data=sashelp.class alpha=0.05 order=Formatted;
class age;
format age oage.;
var weight;
run;

 

View solution in original post

3 REPLIES 3
TDechamp
Calcite | Level 5

The current code I've been trying is:

Data Example.heart; set sashelp.heart;

proc ttest data=example.heart alpha=0.05 order=data;
class sex;
var weight;
run;

ballardw
Super User

Another option if you can't get an ORDER=Data to work is to assign a format that changes the order and use Order=Formatted

 

proc format;
value $mysex
'Male'=' Male'
'Female'='Female'
;
run;

proc ttest data=sashelp.heart alpha=0.05 order=Formatted;
class sex;
format sex $mysex.;
var weight;
run;

A side effect of most of the ODS output is that the space in front of the M that makes " M" come before "F" alphabetically is that the space is justified out of appearance.

 

Since T-test class variables should only have two levels it usually isn't that hard to come up with a format quickly.

It may be of interest that you can create the two groups for Ttest from a continuous or multivalued categorical variable by using a format.

proc format;
value tage
low-14='14 and younger'
15-high= 'Over 14';
run;

proc ttest data=sashelp.class alpha=0.05 order=Formatted;
class age;
format age tage.;
var weight;
run;

This can be very useful if you need a "this value" compared to "all the other values" of a category and pretty easy to code

proc format;
value oage
14='14'
other= 'Not 14';
run;

proc ttest data=sashelp.class alpha=0.05 order=Formatted;
class age;
format age oage.;
var weight;
run;

 

FreelanceReinh
Jade | Level 19

Hello @TDechamp and welcome to the SAS Support Communities!

 

Use the ORDER= option of the PROC TTEST statement.

 

Example:

proc ttest data=sashelp.class order=data;
class sex;
var height;
run;

The output displays males first because the first non-missing value of variable SEX in the dataset is 'M'. If that weren't the case, a PROC SORT step as shown below could create a suitably sorted input dataset for PROC TTEST:

proc sort data=sashelp.class out=want;
by descending sex;
run;

Note that in other datasets (perhaps in yours) variable SEX might be numeric with an associated format, e.g., 1=male, 2=female. In this case order=internal could be used without sorting.

 

Edit: In SASHELP.HEART, as shown in your second post, SEX is a character variable and 'Female' happens to occur first. So, use PROC SORT as above to get 'Male' values to the top in EXAMPLE.HEART.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 1211 views
  • 1 like
  • 3 in conversation