Announcement

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

  • loop for cross tab

    Dear members,

    Is there a way to use the loop for cross-tabulation?

    X and y are categorical

    What I am doing is-
    Code:
    tab y x1
    tab y x2
    .     .   .
    .     .   .
    .     .  .
    tab y  xk
    How to write a loop for keeping 'y' fixed and using 'x' for each next table?

    Thank you!
    Best regards,
    Mukesh

    (Stata 15.1 SE)

  • #2
    Code:
    foreach v of varlist x1 x2 x3 ... xk {  // DON'T USE ... IN REAL CODE; LIST THE VARIABLES
        tab y `v'
    will do that. But you really don't need a loop. You can just do:
    Code:
    tab2 y x1 x2 x3 ... xk, firstonly // AGAIN, DON'T USE ...; LIST THE VARIABLES
    Last edited by Clyde Schechter; 14 Jan 2024, 11:46.

    Comment


    • #3

      Thank you, Schechter

      Originally posted by Clyde Schechter View Post
      Code:
      foreach v of varlist x1 x2 x3 ... xk { // DON'T USE ... IN REAL CODE; LIST THE VARIABLES
      tab y `v'
      will do that. But you really don't need a loop. You can just do:
      Code:
      tab2 y x1 x2 x3 ... xk, firstonly // AGAIN, DON'T USE ...; LIST THE VARIABLES
      Is it possible to write a loop including y1, y2, and y3 - which are also in dummy form?
      Best regards,
      Mukesh

      (Stata 15.1 SE)

      Comment


      • #4
        Yes
        Code:
        foreach v of varlist y1 y2 y3 {
            tab2 `v' x1 x2 x3 ... xk
        }

        Comment


        • #5
          I'm sure Clyde meant to include the firstonly option in #4, as he did in #2.

          Code:
          foreach v of varlist y1 y2 y3 {
              tab2 `v' x1 x2 x3 ... xk, firstonly
          }
          --
          Bruce Weaver
          Email: [email protected]
          Version: Stata/MP 18.5 (Windows)

          Comment


          • #6
            Indeed, I did. Thank you Bruce.

            Comment


            • #7
              Thank you!

              One more clarification-

              Only fweight is allowed with tab2

              Originally posted by Bruce Weaver View Post
              I'm sure Clyde meant to include the firstonly option in #4, as he did in #2.

              Code:
              foreach v of varlist y1 y2 y3 {
              tab2 `v' x1 x2 x3 ... xk, firstonly
              }
              Best regards,
              Mukesh

              (Stata 15.1 SE)

              Comment


              • #8
                Sometimes when a thread makes a turn -- in fact, my real problem is more complicated than I told you -- then the answer just is more complicated too.

                If you want different weights, then the solution could be

                1. Cloning tab2.ado and generalizing it to support different weights. Save your own command under a different name in your own filespace.

                2. A double loop:

                Code:
                foreach y in y1 y2 y3 { 
                     foreach x in x1 x2 x3 { 
                           tab `y' `x' [aw=whatever] 
                     }
                }
                Here solution #2 is what I recommend unless and until you use it so often that #1 looks attractive.

                Comment


                • #9
                  Thank you, Dr. Cox

                  This is what I wanted!
                  Best regards,
                  Mukesh

                  (Stata 15.1 SE)

                  Comment

                  Working...
                  X