Announcement

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

  • STATA frequency table for categorical variables using foreach loops

    *** I had posted this on Stack Exchange and was directed here:

    Dear All,

    I have been working with a panel survey data. I have a set of villages that have received the treatment and a set that have not (classified treat = 0 or 1). I also have schooling dummies Primary school (prim), middle and high school (midhigh), boys primary (bprim), boys midhigh, girls primary (gprim) etc. Now I want to create a count table for which I wanted to create count variables by treated and not-treated using STATA. The following code seems to work but I am not entirely sure this is the correct way:

    Code:
    local school prim midhigh bprim bmidhigh gprim gmidhigh
    foreach x in `school' {
    egen `x'_treat = sum(`x') if treat == 1
    egen `x'_nontreat = sum(`x') if treat ==0
    }
    Then, I was hoping to use the output to create the following count table using esttab (which is not an issue but for illustrative purposes)
    Treated vs Non-Treated
    prims # of primary schools
    midhigh # of middle and high schools
    bprim # of boys primary schools
    bmidhigh # boys of middle and high schools
    gprim # of girls primary schools
    gmidhigh # of girls middle and high schools
    Thank you in advance,

    Nair
    Last edited by Lorien Nair; 09 May 2019, 12:33.

  • #2
    It seems you won't need a loop for that. You can - tabulate - or - tabstat - the variables.
    Best regards,

    Marcos

    Comment


    • #3

      Code:
      tab2 treatment prim midhigh bprim bmidhigh gprim gmidhigh, firstonly
      may help as well.

      Comment


      • #4
        Thank you, Marcos!

        I did get the desired results, I was over complicating a simple task. Is it possible to incorporate all of the output into esttab or do I simply use tabout? Thanks again!

        Comment


        • #5
          Actually, I haven’t practiced with these 2 user-written programs, but I’m very confident you can do it in both.
          Best regards,

          Marcos

          Comment

          Working...
          X