turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- Need help to create a macro to for proc ttest to o...

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-17-2013 04:50 PM

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

06-17-2013 05:10 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-03-2015 08:37 AM

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)

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-03-2015 09:21 AM

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);
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-03-2016 04:34 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-06-2016 09:22 AM

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

- The first IF-THEN statement doesn't do anything. In the statement

if _N_=1 then;

there is an empty body. - String comparisons in SAS are case sensitive. So the second IF-THEN statement should read

if ... and variances = 'Unequal' /* note the capitalization! */ - 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. - 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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

12-14-2016 01:52 AM

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