BookmarkSubscribeRSS Feed
abudyak
Calcite | Level 5

I am not sure why I am getting this error. My professor gave me a sample of how to write proc ttest, but when I wrote it (the exact same way, just different numbers and different variable names) I get an error. 

 

My code:

data name;
	input one two @@;
	cards;
	16.03 16.02 16.04 15.97 16.05 15.96 16.05 16.01 16.02 15.99
	16.01 16.03 15.96 16.04 15.98 16.02 16.02 16.01 15.99 16.00
	;
proc ttest data=name;
	class one;
	var two;
run;

and the error that shows up when I run it says:

ERROR: The CLASS variable has more than two levels.

 

My data output prints out correctly. And I only have two levels.

16 REPLIES 16
Tom
Super User Tom
Super User

You are using the wrong syntax for the way you have the data structured.  You want to use the PAIRED statement.

https://documentation.sas.com/?docsetId=statug&docsetTarget=statug_ttest_syntax08.htm&docsetVersion=...

proc ttest data=name;
  paired one*two;
run;

To use the syntax you posted you would need to convert the data structure to have a class variable.  You could use PROC TRANSPOSE.

proc transpose data=name out=TALL ;
  var one two;
run;
proc ttest data=TALL ;
  class _name_;
  var col1;
run;

Look at the documentation to see how those two tests are different and think about what you data really is.  Are the values for ONE and TWO that appear on the same observation in your NAME dataset really paired?  

abudyak
Calcite | Level 5

Why does this code run, but mine does not? It i basically the same, but different numbers @Tom 

data burn;
	input type time @@;
	cards;
	1 65 1 81 1 57 1 66 1 82 1 82 1 67 1 59 1 75 1 70
	2 64 2 71 2 83 2 59 2 65 2 56 2 69 2 74 2 82 2 79
	;
proc ttest data=burn;
	class type;
	var time;
run;
PaigeMiller
Diamond | Level 26

@abudyak wrote:

Why does this code run, but mine does not? It i basically the same, but different numbers

data burn;
	input type time @@;
	cards;
	1 65 1 81 1 57 1 66 1 82 1 82 1 67 1 59 1 75 1 70
	2 64 2 71 2 83 2 59 2 65 2 56 2 69 2 74 2 82 2 79
	;
proc ttest data=burn;
	class type;
	var time;
run;


So, let's go back to your original code

My code:

data name;
	input one two @@;
cards;
	16.03 16.02 16.04 15.97 16.05 15.96 16.05 16.01 16.02 15.99
	16.01 16.03 15.96 16.04 15.98 16.02 16.02 16.01 15.99 16.00
	;
proc ttest data=name;
	class one;
	var two;
run;

and the error that shows up when I run it says:

ERROR: The CLASS variable has more than two levels.

 

My data output prints out correctly. And I only have two levels.

 

Your statement about having only two levels is incorrect. You have told SAS that the CLASS variable is ONE, and this variable has many levels.

 

In the code from @Tom, the class variable TYPE has only two levels, 1 and 2. If you look at Tom's data set, and you look at yours, you will see the difference.

--
Paige Miller
abudyak
Calcite | Level 5

if it was a pooled t-test, how would it be written differently? @PaigeMiller 

PaigeMiller
Diamond | Level 26

The code for a paired analysis has already been provided by @Tom 

--
Paige Miller
abudyak
Calcite | Level 5

Yes I know, but I am asking about a pooled t-test, not a paired. how would it be written in the case of a pooled t-test? @PaigeMiller @Tom 

PaigeMiller
Diamond | Level 26

Way back in message 2 of this thread, the code for a paired t-test was provided.

--
Paige Miller
abudyak
Calcite | Level 5

@PaigeMiller I am not talking about a paired test I am talking about a pooled test. Those are different tests. Do you know how to write the code for a pooled test which was not give. The paired test was given, but I am not asking about a paired test I am asking about pooled.

PaigeMiller
Diamond | Level 26

I am sorry for mis-reading and mis-understanding your question.

 

Nevertheless, in message 2, @Tom provides code for Paired and for Pooled.

--
Paige Miller
Tom
Super User Tom
Super User

Look at the values of TYPE in the BURN data versus the values of ONE in your NAME dataset.

proc freq data=burn;
 tables type;
run;
proc freq data=name;
 tables one ;
run;

If you want to create a TYPE variable with your current data stream you might do one of these two ways of reading that stream. Depending on if the values of two types alternate in the data steam or whether the first row are the values for TYPE=1 and the second row are values for type=2.

data nameA;
  do type=1 to 2 ;
    input value @@;
    output;
  end;
cards;
16.03 16.02 16.04 15.97 16.05 15.96 16.05 16.01 16.02 15.99
16.01 16.03 15.96 16.04 15.98 16.02 16.02 16.01 15.99 16.00
;

data nameB;
  infile cards truncover ;
  type+1;
  do until(value=.);
    input value @ ;
    if value ne . then output;
  end;
cards;
16.03 16.02 16.04 15.97 16.05 15.96 16.05 16.01 16.02 15.99
16.01 16.03 15.96 16.04 15.98 16.02 16.02 16.01 15.99 16.00
;

Note that NAMEA is basically the same thing you would get as the output of the PROC TRANSPOSE step in my first reply.

Then you can try ttest code like this:

proc ttest data=nameB;
  class type;
  var value;
run;

 

abudyak
Calcite | Level 5

Thank you @Tom that makes sense. If I was dealing with a pooled test instead of a paired test, how would that be written?

Tom
Super User Tom
Super User

Please re-read both of my posts.

The syntax you have is correct.  You just need structure the data in a way where you have one variable with two values and a second variable with numbers to be compared.

abudyak
Calcite | Level 5

I understand what you are saying now so that makes sense, but the data set I am working with has multiple levels for 'one.' so I would not be able to change one of the variables to just 2 levels. @Tom And I am struggling to figure out how to write the code as a pooled ttest when my data set has more than two levels.

Tom
Super User Tom
Super User

If your class variable has more than two levels then TTEST is probably not the statistic you want to run.

But if you want to get it to run then just subset the data to have just two levels.  So for example that ONE as 4 levels. For simplicity lets says they are the numbers 1,2,3,4.  You could then generate a TTEST comparing the values of TWO for the two classes where ONE=1 or ONE=2 using code like this:

proc ttest data=have;
  where one in (1 2);
  class one;
  var two;
run;

You could then repeat this for the other 5 possible combinations of two values or ONE:  1,3 ; 1,4 ; 2,3 ; 2,4 ; 3,4 .

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 25. 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
  • 16 replies
  • 5183 views
  • 0 likes
  • 4 in conversation