Announcement

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

  • Converging multiple columns into one output table

    Hello, I have cleaned a survey which I now need to do some analysis, for the most part this has been straightforward except for a few questions that are the same format and which have confounded me so far.

    The type of question is one in which it has multiple sub-questions, in which respondents say "yes" or "no" or "don't know" for each subquestion, or select something from a Likert-type scale. However, I am hoping to calculate the frequency of all subquestions all in the same table per question. For those that are "yes/no", that would just be the percentage of those who say "yes", or would be the percentage who chose each Likert-item 1 through 4, for example. You can see in my data example that q15 and q21 are yes/no, and q24 is one with a likert scale.

    I have tried various attempts with generating new vars, then using -tab-, -tabstat-, or -fre-, and also attempted with -collapse- with no success.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(q15a q15b q15c q15d q21a q21b q21c q21d q24a q24b q24c q24d)
    1 1 2 1 1 1 1 1 . . . .
    1 1 1 1 1 1 1 1 2 2 2 2
    1 1 2 1 1 1 1 2 2 3 3 3
    1 2 2 1 2 2 1 1 3 . 3 3
    1 1 1 1 1 1 1 1 2 2 2 2
    1 1 1 1 1 1 2 . . . . .
    1 2 2 1 2 1 2 2 1 1 3 2
    1 2 2 2 1 1 1 1 . . . .
    1 1 1 1 1 1 1 1 . . . .
    1 1 2 1 1 1 1 1 . . . .
    1 1 1 1 1 1 1 1 4 . . .
    1 . . 1 1 1 1 1 3 . . .
    1 2 2 1 1 1 1 1 3 . . 3
    1 2 2 2 1 1 1 1 . . . .
    1 2 2 1 1 1 1 1 . . . 4
    1 . . . 1 1 1 . 4 . 4 4
    1 1 1 1 . . . . . . . .
    1 1 1 1 1 1 1 1 3 . . .
    1 1 1 1 1 1 1 1 3 2 3 2
    1 1 2 1 1 1 1 1 . . . 4
    1 . 2 1 1 1 1 1 3 . . 3
    1 2 2 1 1 1 1 1 1 . . .
    1 1 1 1 1 1 1 1 . . . 3
    1 2 2 1 1 2 2 1 . . 4 .
    1 1 1 1 1 1 1 1 3 3 3 3
    1 1 2 2 1 1 1 1 1 . . 3
    1 1 . 1 1 . 1 1 . . . .
    1 1 2 . 1 1 1 1 3 . . 3
    1 2 2 . 1 1 1 1 . . . .
    1 1 . 1 1 1 1 1 3 4 . 2
    1 1 1 2 1 1 1 2 3 1 3 3
    1 1 1 2 1 1 1 1 2 3 1 3
    1 1 2 1 1 2 1 1 . . . 2
    1 2 2 1 1 1 1 1 4 . . 4
    1 1 1 1 1 1 1 1 4 4 1 4
    1 . . 1 1 2 2 2 . . . .
    1 1 2 1 1 1 1 1 2 . . .
    1 2 2 1 1 1 1 1 . . . .
    1 1 1 1 1 1 1 1 . . . 4
    1 . . 2 . 2 1 . 2 . 2 2
    1 1 2 2 1 1 2 1 4 2 1 1
    1 . . 1 1 1 1 1 3 . . 4
    1 2 2 2 1 1 1 1 2 . . 4
    1 1 . 1 1 1 1 1 . . . .
    1 . . 2 1 1 2 1 . . . .
    1 2 1 2 1 1 2 1 3 3 4 2
    1 1 . 1 . . . . . . . .
    1 2 2 1 1 1 1 1 . . . 3
    1 1 2 1 1 1 2 1 1 1 2 1
    1 2 1 1 1 1 1 1 3 3 . 3
    1 2 2 1 2 2 2 1 4 . 4 4
    1 1 1 1 1 . 1 1 3 . . .
    1 1 1 1 1 1 1 1 2 . . .
    1 2 . 1 1 1 1 1 2 . . 3
    1 1 1 2 1 1 2 1 3 4 1 2
    1 2 2 2 1 1 1 1 2 . 2 .
    1 1 1 1 1 1 1 1 2 2 2 2
    1 1 1 1 1 1 1 1 4 . . 4
    1 . 2 . 1 1 1 1 3 . . 3
    1 2 2 1 1 2 2 1 4 3 . .
    1 . . 1 1 1 1 1 . . . 4
    1 1 2 1 1 1 1 2 2 2 2 2
    1 1 2 1 1 1 1 1 . . . .
    1 1 2 1 1 1 2 1 3 4 4 2
    1 1 1 1 1 1 1 1 . . . 4
    1 1 . 1 1 1 . 1 4 . . 4
    1 . . 1 2 2 2 1 . . . 3
    1 2 2 2 1 1 2 2 . 3 . 2
    1 2 2 2 1 1 1 1 . . . 4
    1 1 1 1 1 1 1 1 2 2 2 2
    1 2 2 . 1 1 . 2 3 . 3 3
    1 1 . 1 1 1 1 1 . . . .
    1 2 . . 1 2 . 2 2 3 4 3
    1 2 2 2 1 1 1 1 . . . .
    1 2 2 2 1 1 1 1 2 . . 3
    1 . 1 1 1 2 1 1 3 . . 3
    1 2 2 1 1 1 1 1 3 . . 3
    1 1 . 1 1 2 1 2 3 3 3 3
    1 2 2 2 1 1 1 1 . 1 4 1
    1 1 1 1 1 1 1 1 2 . . 4
    1 2 2 1 1 2 1 1 3 . 3 .
    1 1 2 1 2 2 1 1 2 2 2 .
    1 1 2 1 2 2 1 1 3 3 1 1
    1 1 . 1 1 1 1 1 2 . . 2
    1 1 1 1 2 1 2 1 1 1 3 2
    1 . 2 1 1 1 1 1 2 . . 3
    1 2 2 1 1 2 2 1 . . 3 .
    1 1 2 1 1 1 1 1 3 . . 4
    1 2 2 1 1 1 1 1 2 . . 4
    1 . . 1 1 1 1 1 . . . .
    1 1 2 1 1 1 1 1 3 . . 4
    1 1 2 2 1 1 1 1 . . . .
    1 2 2 2 1 2 1 1 2 . . .
    1 1 1 1 1 1 1 1 3 2 2 3
    1 2 2 . 2 2 1 1 3 . . .
    1 2 2 1 1 1 1 1 3 . 4 3
    1 2 2 1 1 1 1 1 3 . . 3
    1 1 2 1 1 1 1 2 2 2 2 3
    1 1 1 1 1 1 1 . 3 . 4 .
    1 2 2 1 2 1 1 2 2 3 3 2
    end

    So with this, I would hope for it (output table) to look something like (with a separate table for each question, obviously):
    q15a x%
    q15b x%
    q15c x%

  • #2
    Check out tab1, etc. Also, this may help

    Code:
    . findname, all(inlist(@, 1, 2, .)) 
    q15a  q15b  q15c  q15d  q21a  q21b  q21c  q21d
    
    . 
    . tabm `r(varlist)'  
    
               |        values
      variable |         1          2 |     Total
    -----------+----------------------+----------
          q15a |       100          0 |       100 
          q15b |        52         35 |        87 
          q15c |        28         53 |        81 
          q15d |        74         19 |        93 
          q21a |        88          9 |        97 
          q21b |        79         17 |        96 
          q21c |        79         16 |        95 
          q21d |        83         11 |        94 
    -----------+----------------------+----------
         Total |       583        160 |       743 
    
    . 
    . findname, any(inlist(@, 3, 4)) 
    q24a  q24b  q24c  q24d
    
    . 
    . tabm `r(varlist)' 
    
               |                   values
      variable |         1          2          3          4 |     Total
    -----------+--------------------------------------------+----------
          q24a |         5         23         30          9 |        67 
          q24b |         5         10         11          4 |        30 
          q24c |         5         11         12          9 |        37 
          q24d |         4         17         26         18 |        65 
    -----------+--------------------------------------------+----------
         Total |        19         61         79         40 |       199
    where findname is from the Stata Journal and tabm is from tab_chi (SSC).

    Comment


    • #3
      I had never come across findname before, thank you.

      It seems like this is good first step; using -tabm `r(varlist)' - gets me to a better starting place than I was at previously. Is there a command I can add on the end of that to have the table include a percentage of "1" (or "yes")? And similarly a percentage for the second table as well? I don't see info about showing percentages in -help tab1-

      Comment


      • #4
        I also now realize that with
        Code:
        findname, all(inlist(@, 1, 2, .)
        it will produce one large table with all the questions similar to q15 that have 1 or 2 as options...is there possibility to do it question by question with separate table for each?

        Comment

        Working...
        X