Announcement

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

  • Tabulate the same variable multiple times

    Dear all,

    I need to tabulate variable Y conditioned on different values another variable X can take. Currently, I am using the following code:
    Code:
    tabulate Y if X == 1
    tabulate Y if X == 2
    tabulate Y if X == 3
    tabulate Y if X == 4
    tabulate Y if X == 5
    tabulate Y if X == 6
    I am wondering if there is a more efficient code, perhaps using the foreach loop to do this. Many thanks in advance!

  • #2
    Code:
    forvalues x = 1/6{
        tabulate Y if X == `x'
    }

    Comment


    • #3
      I would try this first:

      Code:
      tab Y X

      Comment


      • #4
        Originally posted by Nick Cox View Post
        I would try this first:

        Code:
        tab Y X
        Thanks, Nick! I also need the frequency and Cum. columns which gets lost with this code.

        Comment


        • #5
          Originally posted by Ali Atia View Post
          Code:
          forvalues x = 1/6{
          tabulate Y if X == `x'
          }
          Works like a charm, many thanks!

          Comment


          • #6
            Naturally you can add whatever options you want, e.g.

            Code:
            tabulate Y X, col row

            Comment

            Working...
            X