Announcement

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

  • Counting with foreach

    Hello,

    I am working with a survey and i have several yes/no question. My goal is to count for each person how many questions he answered with yes (1).

    Due to the high amount of questions I was thinking about using a loop, in this case a "foreach" loop sounds most fitting. But I am not quite sure.

    Is this the best way of doing it, is there any other way?

    Secondly I have tried something but I keep getting "invalid syntax" errors...
    (pp24-pp72 is the list of variables I am working with)

    foreach x for varlist pp24-pp72 {
    count if `x' == 1
    }

    I think there is something major, that I am missing.
    Does anybody can give me some adive on how to move forward

    Btw i am using Stata 17.0

  • #2
    Julius:
    welcome to this forum.
    As far as your second question is concerned, you may want to try:
    Code:
    foreach x of varlist pp24-pp72 {
    count if  `x'== 1
    }
    like in the following toy-example:
    Code:
    . use "C:\Program Files\Stata17\ado\base\a\auto.dta"
    (1978 automobile data)
    
    . foreach x of varlist rep78-foreign {
      2. count if  `x'== 1
      3. }
      2
      0
      0
      0
      0
      0
      0
      0
      22
    
    .
    Last edited by Carlo Lazzaro; 10 Mar 2022, 06:15.
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Thank you very much!

      Comment


      • #4

        See countvalues from SSC. https://www.statalist.org/forums/for...lable-from-ssc


        Code:
        clear 
        set obs 1000 
        
        forval j = 24/72 { 
            gen pp`j' = runiform() > 0.`j' 
        } 
        
        countvalues pp24-pp72, values(1) 
        
          +------------+
          | name     1 |
          |------------|
          | pp24   748 |
          | pp25   778 |
          | pp26   779 |
          | pp27   702 |
          | pp28   714 |
          | pp29   732 |
          | pp30   692 |
          | pp31   683 |
          | pp32   697 |
          | pp33   661 |
          | pp34   680 |
          | pp35   666 |
          | pp36   628 |
          | pp37   634 |
          | pp38   624 |
          | pp39   576 |
          | pp40   606 |
          | pp41   596 |
          | pp42   599 |
          | pp43   557 |
          | pp44   532 |
          | pp45   526 |
          | pp46   558 |
          | pp47   542 |
          | pp48   514 |
          | pp49   539 |
          | pp50   501 |
          | pp51   486 |
          | pp52   501 |
          | pp53   464 |
          | pp54   477 |
          | pp55   459 |
          | pp56   417 |
          | pp57   422 |
          | pp58   378 |
          | pp59   436 |
          | pp60   364 |
          | pp61   397 |
          | pp62   364 |
          | pp63   375 |
          | pp64   363 |
          | pp65   345 |
          | pp66   356 |
          | pp67   325 |
          | pp68   310 |
          | pp69   316 |
          | pp70   295 |
          | pp71   293 |
          | pp72   314 |
          +------------+
        
        .

        Comment

        Working...
        X