Announcement

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

  • Conditioning for values within a variable while using foreach

    Hi everyone!
    I'm new to the forum, but here it goes. I have the following code, where I'm just trying to get responses back when more than 50% of respondents chose option 2 (which was "Bad").

    quietly foreach var in Q25_1 Q25_2 Q25_3 Q25_4 Q25_5 Q25_6 Q25_7 Q25_8 Q25_9 Q25_10 Q25_11 Q25_12 Q25_13{
    count if (`var'==2)
    local num=r(N)
    count if (`var'==1|`var'==2|`var'==3)
    local den=r(N)
    local frac=`num'/`den'
    if `frac'>=0.05 {
    noisily tab `var'
    noisily display "Question `var': `frac'"
    }
    }

    However, I also want to run this for different genders (I have 5 options of gender: 1==Cisgender man, 2== Trans man, 3== Cisgender woman, 4== Trans Woman, 5== Nonbinary). I want the same code to run for each of them, so I can compare the results and see if any group has more negative responses than others to the same question.

    This is what I've tried but haven't gotten good results.

    foreach i in 1 2 3 4 5{
    quietly foreach var in Q25_1 Q25_2 Q25_3 Q25_4 Q25_5 Q25_6 Q25_7 Q25_8 Q25_9 Q25_10 Q25_11 Q25_12 Q25_13 if Gender==`i'{
    count if (`var'==2)
    local num=r(N)
    count if (`var'==1|`var'==2|`var'==3)
    local den=r(N)
    local frac=`num'/`den'
    if `frac'>=0.05 {
    noisily tab `var'
    noisily display "Question `var': `frac'"
    }
    }

    Any help will be appreciated!
    Best
    Miguel
    Last edited by Miguel Fuentes; 05 Jul 2022, 21:26.

  • #2
    Welcome to Statalist! Please read through the FAQ above, which details how to write questions that are likely to provoke good answers! Typically, we ask that question askers provide a data example with -dataex- so that we can test code on our local machine. It also helps if you surround your code in Code tags like this:

    Code:
    foreach i in 1 2 3 4 5{
        quietly foreach var in Q25_1 Q25_2 Q25_3 Q25_4 Q25_5 Q25_6 Q25_7 Q25_8 Q25_9 Q25_10 Q25_11 Q25_12 Q25_13 if Gender==`i'{
            count if (`var'==2)
            local num=r(N)
            count if (`var'==1|`var'==2|`var'==3)
            local den=r(N)
            local frac=`num'/`den'
            if `frac'>=0.05 {
                 noisily tab `var'
                 noisily display "Question `var': `frac'"
        }
    }
    On first glance, it looks to me like you have "if Gender==`i'" on the wrong line. You probably want something like count if (`var' == 2 & Gender == `i') instead. This is, of course, untested on my end because you didn't provide example data. Please read through the FAQ and try to follow the posting guidelines.

    Edit: it also looks like you don't terminate your if block at the end. You have curly closing bracket for the first and second for loop, but not the if statement. It is also best to avoid the -quietly- command in cases like this. You should want to see the full output for debugging purposes.
    Last edited by Daniel Schaefer; 05 Jul 2022, 22:37.

    Comment

    Working...
    X