Announcement

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

  • making loop with the values within the variable

    Hello,

    I have one of the variables G1 which has values from 1 to 17.
    Then I want to make a loop as below,but it didn't work out.
    Anyone can help what is the problem? thanks in advance.

    foreach i in G1 {
    sum sw20h if G1==`i'
    global Ng`i'=r(sum)
    }

  • #2
    Code:
    help levelsof
    and look at the examples one of shows how to use -levelsof- with -foreach-

    Comment


    • #3
      I tried with this code, but still doesn't work.

      levelsof G1, local(G1)
      foreach i of local G1 {
      sum sw20h if G1==`i'
      global Ng`i'=r(sum)
      }

      Where is wrong?

      Comment


      • #4
        Code:
        tabstat sw20h, s(sum) by(G1)
        is more direct as a way of showing sums.

        As for what is wrong, what does "doesn't work" mean?

        https://www.statalist.org/forums/help#stata advises:
        Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.

        Comment


        • #5
          It seems from your other thread that you want to do further calculations with these totals. You don't give any data example but the following silly example -- which all Stata users can run -- may help -- and help to show that loops are often not needed.

          Code:
          . sysuse auto, clear
          (1978 Automobile Data)
          
          . egen total_price = total(price), by(rep78)
          
          . tabdisp rep78, c(total_price)
          
          -----------------------
          Repair    |
          Record    |
          1978      | total_price
          ----------+------------
                  1 |        9129
                  2 |       47741
                  3 |      192877
                  4 |      109287
                  5 |       65043
                  . |       32152
          -----------------------
          
          * same results! 
          . tabstat price, by(rep78) s(sum) missing
          
          Summary for variables: price
               by categories of: rep78 (Repair Record 1978)
          
             rep78 |       sum
          ---------+----------
                 1 |      9129
                 2 |     47741
                 3 |    192877
                 4 |    109287
                 5 |     65043
                 . |     32152
          ---------+----------
             Total |    456229
          --------------------
          
          . su price, meanonly
          
          . di r(sum)
          456229
          
          . gen fraction_total = total_price / r(sum)
          
          . tabdisp rep78, c(total_price fraction_total)
          
          ------------------------------------------
          Repair    |
          Record    |
          1978      |    total_price  fraction_total
          ----------+-------------------------------
                  1 |           9129        .0200097
                  2 |          47741        .1046426
                  3 |         192877        .4227636
                  4 |         109287        .2395442
                  5 |          65043        .1425666
                  . |          32152        .0704734
          ------------------------------------------
          
          . format fraction_total %04.3f
          
          . tabdisp rep78, c(total_price fraction_total)
          
          ------------------------------------------
          Repair    |
          Record    |
          1978      |    total_price  fraction_total
          ----------+-------------------------------
                  1 |           9129           0.020
                  2 |          47741           0.105
                  3 |         192877           0.423
                  4 |         109287           0.240
                  5 |          65043           0.143
                  . |          32152           0.070
          ------------------------------------------


          Comment

          Working...
          X