Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Stata Table - Restrict Observations

    Hello

    Is is possible when working with TABLE in Stata to restrict observations to between two values of a variable....

    Example/// entered into the window is the expression outofsampledata == 1 & won == 1 & myfinalprice >20 <30

    In the above I want to restrict observations to between 20 and 30 of the 'myfinalprice' variable.
    Have tried a few things but none are providing a solution.

    Thanks.

  • #2
    Not directly; -table- does not support -if- or -in- clauses. But what you can do is restrict the data set before calling -table-.

    Code:
    preserve
    keep if outofsampledata == 1 & won == 1 & inrange(myfinalprice, 20, 30)
    table whatever
    restore
    If you don't actually need the data that are dropped before -table- for anything else later, then you can skip the -preserve- and -restore- commands.

    Comment


    • #3
      I don't follow. Are we talking about the new -table- command in Stata 17? If so, the help clearly states:

      Full syntax

      table (rowspec) (colspec) [(tabspec)] [if] [in] [weight] [, options]
      Edit: I just confirmed, and the old -table- command (pre-Stata 17) also used to allow for -in- and -if-.
      Last edited by Hemanshu Kumar; 07 Nov 2022, 11:26.

      Comment


      • #4
        I find Clyde Schechter response confusing as -table- does support in and if; here is an example using the lbw data set
        Code:
        . table race low if inrange(age,20,25)
        
        --------------------------------
                |    birthweight<2500g  
                |     N      Y     Total
        --------+-----------------------
        race    |                       
          white |    29     10        39
          black |     6      6        12
          other |    18     15        33
          Total |    53     31        84
        --------------------------------
        
        
        . table race low
        
        --------------------------------
                |    birthweight<2500g  
                |      N      Y    Total
        --------+-----------------------
        race    |                       
          white |     73     23       96
          black |     15     11       26
          other |     42     25       67
          Total |    130     59      189
        --------------------------------
        the second table is show as evidence that the "if" actually did work

        Comment


        • #5
          Sorry, Rich is correct, and an -if- clause is all that is needed here. Apologies for the confusion.

          Comment


          • #6
            Thanks all. Much appreciated.

            Comment

            Working...
            X