Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6220237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:55:24+00:00 2026-05-24T07:55:24+00:00

I have a set of data like below: A B C D 1 2

  • 0

I have a set of data like below:

A B C D
1 2 3 4
2 3 4 5

They are aggregated data which ABCD constitutes a 2×2 table, and I need to do Fisher exact test on each row, and add a new column for the p-value of the Fisher exact test for that row.

I can use fisher.exact and loop to do it in R, but I can’t find a command in Stata for Fisher exact test.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:55:25+00:00Added an answer on May 24, 2026 at 7:55 am

    You are thinking in R terms, and that is often fruitless in Stata (just as it is impossible for a Stata guy to figure out how to do by ... : regress in R; every package has its own paradigm and its own strengths).

    There are no objects to add columns to. May be you could say a little bit more as to what you need to do, eventually, with your p-values, so as to find an appropriate solution that your Stata collaborators would sympathize with.

    If you really want to add a new column (generate a new variable, speaking Stata), then you might want to look at tabulate and its returned values:

        clear
        input x y f1 f2
        0 0 5 10
        0 1 7 12
        1 0 3 8
        1 1 9 5
        end
    

    I assume that your A B C D stand for two binary variables, and the numbers are frequencies in the data. You have to clear the memory, as Stata thinks about one data set at a time.

    Then you could tabulate the results and generate new variables containing p-values, although that would be a major waste of memory to create variables that contain a constant value:

        tabulate x y [fw=f1], exact
        return list
        generate p1 = r(p_exact)
        tabulate x y [fw=f2], exact
        generate p2 = r(p_exact)
    

    Here, [fw=variable] is a way to specify frequency weights; I typed return list to find out what kind of information Stata stores as the result of the procedure. THAT’S the object-like thing Stata works with. R would return the test results in the fisher.test()$p.value component, and Stata creates returned values, r(component) for simple commands and e(component) for estimation commands.

    If you want a loop solution (if you have many sets), you can do this:

       forvalues k=1/2 {
        tabulate x y [fw=f`k'], exact
        generate p`k' = r(p_exact)
       }
    

    That’s the scripting capacity in which Stata, IMHO, is way stronger than R (although it can be argued that this is an extremely dirty programming trick). The local macro k takes values from 1 to 2, and this macro is substituted as “k’` everywhere in the curly bracketed piece of code.

    Alternatively, you can keep the results in Stata short term memory as scalars:

        tabulate x y [fw=f1], exact
        scalar p1 = r(p_exact)
        tabulate x y [fw=f2], exact
        scalar p2 = r(p_exact)
    

    However, the scalars are not associated with the data set, so you cannot save them with the
    data.

    The immediate commands like cci suggested here would also have returned values that you can similarly retrieve.

    HTH, Stas

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of controls bound to data, on which I would like
I have a set of data that is structured like this: ItemA.GroupA ItemB.GroupA ItemC.GroupB
I have a data set that looks like this: 000 100 200 300 010
I have a set of survey data, and I'd like to generate plots of
I have several complex data structures like Map< A, Set< B > > Set<
I have a set of core, complicated JavaScript data structures/classes that I'd like to
We have this set of data that we need to get the average of
I have a set of data protected by 16bit checksums that I need to
I have a set of data that models a hierarchy of categories. A root
I have a set of data that needs to be displayed as a crosstab

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.