Need help to create a macro to for proc ttest to o/p the variance test

Reply
Occasional Contributor
Posts: 7

Need help to create a macro to for proc ttest to o/p the variance test

I need help to create  a macro for a proc ttest which will output the variance test also for equity. so that i can see which p-value i can pick from the two test. i want the code to decide if the method should be pooled or otherwise. Any suggestion will be greatly appreciated as this is very urgent

I have this code inside a macro.I need to create variance too.

ods output ttest= p&var        /( var is the variables for which i need the test)/

ods trace on

proc ttest data =dataset;

class age;

var &var;

run;

ods trace off;

ods output close;

Grand Advisor
Posts: 17,325

Re: Need help to create a macro to for proc ttest to o/p the variance test

You can run multiple vars at once and capture the tables, then transpose and merge instead of a macro.

Occasional Contributor
Posts: 14

Re: Need help to create a macro to for proc ttest to o/p the variance test

Hello,

 

I'm struggling with a similar scenario if anyone may provide guidance...Thank you for any advice...I'm creating macro (%myttest) with four arguments:

 

-DAT=dataset

-MAINVAR=score var

-CLASSVAR=gender

-CUTOFF=p-value

 

I've got the following SAS code:

 

ODS OUTPUT EQUALITY=VARIANCE_TEST;

ODS OUTPUT TTESTS=P_VALUE; /* GET VALUES FROM TTEST INTO NEW TABLES */

PROC TTEST DATA=Q1;

VAR SCORE;

CLASS GENDER;

RUN;

ODS OUTPUT CLOSE;

/* CHECK VARIANCES AND DETERMINE PROPER P-VALUE */

DATA P_VALUE;

IF _N_=1 THEN

SET VARIANCE_TEST (KEEP=PROBF);

SET P_VALUE (KEEP=VARIANCES PROBT);

KEEP PROBT;

IF (PROBF <= .05 AND VARIANCES='Unequal')

OR (PROBF > .05 AND VARIANCES='Equal');

RUN;

 

but running into errors trying to embed into a macro:

 

TITLE;

%MACRO MYTTEST (DAT, MAINVAR, CLASSVAR, CUTOFF);

Title 'Summary Statistics';

PROC TTEST DATA=&DAT;

class &CLASSVAR;

var &MAINVAR;

run;

DATA _NULL_;

IF _N_=1 THEN

SET VARIANCE_TEST (KEEP=PROBF);

SET P_VALUE (KEEP=VARIANCES PROBT);

KEEP PROBT;

&CUTOFF=IF (PROBF <= .05 AND VARIANCES='Unequal')

OR (PROBF > .05 AND VARIANCES='Equal');

RUN;

%MEND MYTTEST;

%MYTTEST(Q1, SCORE, GENDER, 0.05)

 

Frequent Contributor
Posts: 128

Re: Need help to create a macro to for proc ttest to o/p the variance test

Just a few thoughts on looking at the differences between the code and the macro code:

  1) You don't have the ods output to get your variance_test and p_value data sets in the macro code

  2) You aren't creating a data set called p_value in your macro called as you are in the original code because of the data _NULL_

  3) Your call on the macro parameter cutoff is not where it should be, as you are setting your cutoff parameter equal to the if statement

 

Below is your macro code modified to what I think may help make it run, otherwise I would suggest providing the log with the error(s) you are getting when attempting to run it.  Hope this helps!

 

%macro myttest(dat,mainvar,classvar,cutoff);

ods output equality=variance_test;
ods output ttests=p_value;
proc ttest data=&dat;
class &classvar;
var &mainvar;
run;
ods output close;

data p_value;
if _N_=1 then;
set variance_test (keep=probf);
set p_value (keep=variances probt);
keep probt;
If (probf <= &cutoff AND variances="Unequal")
OR (probf > &cutoff AND variances="Equal");
run;
%mend;

%myttest(Q1,score,gender,0.05);
New Contributor
Posts: 3

Re: Need help to create a macro to for proc ttest to o/p the variance test

Hi,

Any help/advice is much appreciated. This is really urgent issue and I am kind of stuck with my codes without my expected output.

 

I have same scenario as above  and my codes are

proc ttest data=q1;
var score;
class gender;
ods output Equality = Equality TTests=TTests;
run;
proc print data=Equality;
run;

proc print data=TTests;
run;

%macro mytest(dat,mainvar,classvar,cutoff);
data p_value;
if _n_=1 then;
set equality(keep=ProbF);
set ttests(keep= df probt variances tvalue);
if probf le &cutoff and variances = 'unEqual'
then output;    
if probf gt &cutoff and variances = 'Equal'
then output;
drop probf;
run;
%mend;

 

my expected output is (when cutoff is 0.05)

Obs Variances   tValue  DF    Probt
1      Equal          -3.83   12     0.0024

 

when cutoff is 0.65

obs variances tvalue    df    probt

1 unequal -3.83       11.496  0.0026

 

i am able to acheive the expected output for 0.05 however when i change the value to 0.65 the output is empty. please help me to solve the issue.

 

Thanks in advance

SAS Super FREQ
Posts: 3,406

Re: Need help to create a macro to for proc ttest to o/p the variance test

I do not understand what you are trying to accomplish, but here are some suggestions:

  1. The first IF-THEN statement doesn't do anything.  In the statement
    if _N_=1 then;
    there is an empty body.
  2. String comparisons in SAS are case sensitive. So the second IF-THEN statement should read 
    if ... and variances = 'Unequal'    /* note the capitalization! */
  3. When debugging code, it is always simpler if you omit the macro wrapper. For your example, you can just say
    %let cutoff=0.05;
    data p_value;
    ...
    run;
    while you are debugging.
  4. If you insert the PUT _ALL_ statement at the end of the DATA step, the SAS log will show the values of variables for each observation in the P_VALUE data set. I recommend that you post that log so that others can see the values in the EQUALITY and TTESTS data sets.
New Contributor
Posts: 3

Re: Need help to create a macro to for proc ttest to o/p the variance test

Hi,

 

Thank you so much for your reply.

 

The first IF-THEN statement doesn't do anything.  In the statement
if _N_=1 then;

 

Removing the semicolon solved the issue

Ask a Question
Discussion stats
  • 6 replies
  • 630 views
  • 1 like
  • 6 in conversation