Announcement

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

  • Converting results of tabulate command into a dataset

    Hi everyone,

    Just wondering if there's a way to convert the results of tabulate command into a dataset? I want my dataset to look like the table below (generated from the command "tab week randomization":

    week | 0 1 2 | Total
    -----------+---------------------------------+----------
    Week 1 | 0 0 3 | 3
    Week 2 | 5 4 7 | 16
    Week 3 | 3 4 4 | 11
    -----------+---------------------------------+----------
    Total | 8 8 14 | 30


    Any help on this will be appreciated.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float week byte randomization
    3 1
    2 2
    3 1
    2 1
    2 0
    3 2
    3 1
    3 2
    2 0
    2 1
    1 2
    2 2
    2 2
    2 2
    3 0
    1 2
    3 0
    1 2
    2 2
    3 1
    3 2
    2 0
    2 0
    3 2
    3 0
    2 1
    2 2
    2 1
    2 2
    2 0
    end
    label values week week
    label def week 1 "Week 1", modify
    label def week 2 "Week 2", modify
    label def week 3 "Week 3", modify

  • #2
    Here's a way to do it:
    Code:
    tab week randomization, matcell(freq)
    clear
    svmat freq

    Comment


    • #3
      Here's another way to do it. In addition, note the zero option of contract.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float week byte randomization
      3 1
      2 2
      3 1
      2 1
      2 0
      3 2
      3 1
      3 2
      2 0
      2 1
      1 2
      2 2
      2 2
      2 2
      3 0
      1 2
      3 0
      1 2
      2 2
      3 1
      3 2
      2 0
      2 0
      3 2
      3 0
      2 1
      2 2
      2 1
      2 2
      2 0
      end
      label values week week
      label def week 1 "Week 1", modify
      label def week 2 "Week 2", modify
      label def week 3 "Week 3", modify
      
      contract week randomization 
      
      list, sepby(week)
      
           +---------------------------+
           |   week   random~n   _freq |
           |---------------------------|
        1. | Week 1          2       3 |
           |---------------------------|
        2. | Week 2          0       5 |
        3. | Week 2          1       4 |
        4. | Week 2          2       7 |
           |---------------------------|
        5. | Week 3          0       3 |
        6. | Week 3          1       4 |
        7. | Week 3          2       4 |
           +---------------------------+

      Comment


      • #4
        Thank you both!

        Comment


        • #5
          Nick Cox & Mike Lacy
          Since this follow-up question is related to the previous one. I thought to continue the conversation here. Is there a way I can replace the missing values in a column with sum of all values prior to the missing value? For e.g. how to replace missing values in Week 1 with 19+30+32, missing values in Week 2 with 61+58+93 and so on..
          Code:
          * Example generated by -dataex-. For more info,    type    help    dataex
          clear
          input float(treat Week1 Week2 Week3 Total)
          0 19 61 25 105
          1 30 58 26 114
          2 32 93 22 147
          3  .  .  .   .
          end
          label values treat treat
          label def treat 0 "Control", modify
          label def treat 1 "IPTG", modify
          label def treat 2 "IPTG_Plus", modify
          label def treat 3 "Total", modify
          Last edited by danishussalam; 02 May 2022, 09:52.

          Comment


          • #6
            This seems a perverse thing to do Stata-wise, so I am not highly motivated to answer with code. But a hint is that you could loop over your variables with summarize and put r(sum) in observation 4.

            Comment

            Working...
            X