Announcement

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

  • Creating super tables of proportions in STATA

    Hi, I have 12 variables labeled treat1 - treat12. All the responses are coded as Y/N. I would like to create a 2X2 of these responses, by admission status (Y/N) and have the admission status as the column variable and all treatments as rows. I attached the STATA example table I am trying to replicate. I am using the code below but getting an error that I have too many variables. Thank you.
    [code]

    ----------------------- copy starting from the next line -----------------------
    [CODE]



    table treat1 treat2 treat3 treat4 treat5 treat6 treat7 treat8 treat9 treat10 treat11 treat12, contents(freq ) by(admission)


    Click image for larger version

Name:	Screen Shot 2021-05-23 at 10.50.37 PM.png
Views:	2
Size:	116.5 KB
ID:	1611316



    Attached Files

  • #2
    Every additional variable after the -table- command further splits the data by that extra dimension, what your original code did would create >4,000 combinations of different yes/no responses. The table will be too complicated. If it's just a series of 2x2 table you're after, try looping:

    Code:
    fovalues x = 1/12{
    table treat`x' admission
    }
    the default output is already frequency, so there is no need for -contenst(freq)-.

    Comment


    • #3
      Thank you Ken. This worked beautifully. What if I have other variables that do not have the same treat... prefix, can I still use this code. For example, I have twenty variables labelled pre_albumin, pre_creatinine, pre_wbc, pre_marker, pre_o2, pre_saturation.....etc. How can I run the same code for these variables? Thank you.

      Comment


      • #4
        Originally posted by May Blake View Post
        Thank you Ken. This worked beautifully. What if I have other variables that do not have the same treat... prefix, can I still use this code. For example, I have twenty variables labelled pre_albumin, pre_creatinine, pre_wbc, pre_marker, pre_o2, pre_saturation.....etc. How can I run the same code for these variables? Thank you.
        If only about 20 and you don't mind typing them once, then this:

        Code:
        foreach x in pre_albumin pre_creatinine pre_wbc pre_marker pre_o2 pre_saturation{
            table `x' admission
        }
        In a couple situations this can be shortened:

        1) If the variables are contiguous in the data (aka, all their columns are packed together) with, say, albumin be the first and saturation the last:
        Code:
        foreach x of varlist pre_albumin-pre_saturation{
            table `x' admission
        }
        2) If the variables are the only group that start with "pre_":
        Code:
        foreach x of varlist pre_*{
            table `x' admission
        }

        Comment


        • #5
          Hi Ken,

          You are amazing. All the codes were exactly what I needed. Thank you so much for taking the the time to assist me 😊.

          May

          Comment


          • #6
            You're welcome. Good luck with the rest of the analysis.

            Comment

            Working...
            X