Announcement

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

  • Combining values for multicode response variables to create a new cumulative variable

    I need to create a total variable that will sum the code frequencies from each response variable.

    I have q25_* which asks respondents to indicate which advice sources they use

    There are 15 different response options and respondents may choose up to 15 answers.

    The attached image represents how this data has been inputted to STATA

    n = 100 for q25_O1 spread across codes 1 - 15
    n = 76 for q25_O2 spread across codes 1 - 15 (76 out of the 100 respondents chose more than one answer)
    etc...

    I want to create a variable that sums each of the response codes for each of q25_* and this will generate a cumulative frequency, eg n = 100 + 76 + n3 + n4 etc... spread across response options 1 - 15

    Is someone able to help with syntax to generate this variable?
    Attached Files

  • #2
    What you ask for is a single constant, the total frequency of the non-missing values in your variables. So, there is no obvious point in putting that in a variable.


    Code:
    local count = 0
    
    quietly foreach v of var q25_* {
          count if !missing(`v')
          local count = `count' + r(N)
    }
    
    di `count'
    I suspect that you want other stuff too: you may need to say more specifically what that is, but row functions under egen may be what you seek.

    Please note the advice in https://www.statalist.org/forums/help#stata to use dataex to show data examples, not image attachments.

    Comment

    Working...
    X