Announcement

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

  • Creating a new variable that has a sum of the values of 4 other variables?

    Hi everyone!

    I have four variables in stata typenonap1_recode typenonap2_recode typenonap3_recode typenonap4_recode. For each observation, each variable has a value of 1-14. If I tab each variable separately I see the frequency for each value. I want to generate a variable that will show the combined frequency of all 14 values in all the 4 variables.

    In other words, each variable says "What type of medication" and the answer is 1 antidepressant, 2 mood stabilizer... all the way up to 14 types. When I tab each variable separately I get the frequency of each value, so 1- antidepressant - freq 96, 2 mood stabilizer freq74. I can hand-sum the 4 variables, but can I just create a 5th variable that sums them?

    thanks!
    Will

  • #2
    You don't actually want the new variable to sum up the values of these four variables -- if you did that, the new variable's values would run between (1 x 4 = ) 4 and (14 x 4 = ) 56, and would not make sense.

    You can accomplish what you want by reshaping into a single variable:

    Consider:

    Code:
    * made up data example
    clear
    input byte(id typenonap1_recode typenonap2_recode typenonap3_recode typenonap4_recode)
    1   2   3   4   5
    2   14  13  12  11
    3   6   7   8   9
    4   10  11  12  13
    5   1   2   13  14
    end
    
    * something like this reshape command is all you need
    reshape long typenonap@_recode, i(id) j(num)
    so that now you can
    Code:
    . tab typenonap_recode
    
    typenonap_r |
          ecode |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              1 |          1        5.00        5.00
              2 |          2       10.00       15.00
              3 |          1        5.00       20.00
              4 |          1        5.00       25.00
              5 |          1        5.00       30.00
              6 |          1        5.00       35.00
              7 |          1        5.00       40.00
              8 |          1        5.00       45.00
              9 |          1        5.00       50.00
             10 |          1        5.00       55.00
             11 |          2       10.00       65.00
             12 |          2       10.00       75.00
             13 |          3       15.00       90.00
             14 |          2       10.00      100.00
    ------------+-----------------------------------
          Total |         20      100.00
    Last edited by Hemanshu Kumar; 17 Mar 2025, 12:07.

    Comment


    • #3
      I imagine that you can do excellent tables with the new table framework, but tabm from the package tab_chi on SSC will do what I think you want.

      This code is geared to your data example but note some key details.

      1. A 4 rows x 14 columns display is likely to work less well than its transpose.

      2. Concise but informative variable and value labels are crucial to a good table.

      Code:
      * made up data example
      clear
      input byte(id typenonap1_recode typenonap2_recode typenonap3_recode typenonap4_recode)
      1   2   3   4   5
      2   14  13  12  11
      3   6   7   8   9
      4   10  11  12  13
      5   1   2   13  14
      end
      
      forval j = 1/4 { 
          label var typenonap`j'_recode "nap`j'"
      }
      
      tabm *_recode , transpose 
      
                |                  variable
          values |      nap1       nap2       nap3       nap4 |     Total
      -----------+--------------------------------------------+----------
               1 |         1          0          0          0 |         1 
               2 |         1          1          0          0 |         2 
               3 |         0          1          0          0 |         1 
               4 |         0          0          1          0 |         1 
               5 |         0          0          0          1 |         1 
               6 |         1          0          0          0 |         1 
               7 |         0          1          0          0 |         1 
               8 |         0          0          1          0 |         1 
               9 |         0          0          0          1 |         1 
              10 |         1          0          0          0 |         1 
              11 |         0          1          0          1 |         2 
              12 |         0          0          2          0 |         2 
              13 |         0          1          1          1 |         3 
              14 |         1          0          0          1 |         2 
      -----------+--------------------------------------------+----------
           Total |         5          5          5          5 |        20
      .

      Comment

      Working...
      X