Announcement

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

  • Help with a simple loop

    Dear Stata members

    I have the following sample data

    Code:
    clear
    input str10 month int total float(count_aus count_ind count_us)
    "01-01-1998" 11968 .03 .09 .01
    "01-02-1998" 11328 .01  .1 .02
    "01-03-1998" 12571 .01 .05 .02
    "01-04-1998" 11951 .02 .03 .02
    "01-05-1998" 12753 .02 .06 .04
    "01-06-1998" 11933 .02 .04 .08
    "01-07-1998" 11776 .03 .05 .02
    "01-08-1998" 11800 .03  .1 .02
    "01-09-1998" 12657 .02 .02 .05
    "01-10-1998" 13029 .05 .01 .15
    "01-11-1998" 12264 .04 .02 .05
    "01-12-1998" 12140 .02 .09 .04
    "01-01-1999" 12545 .02 .06 .14
    "01-02-1999" 11366 .01 .02 .08
    "01-03-1999" 12671 .04 .05 .31
    "01-04-1999" 12302 .01 .05 .54
    "01-05-1999" 12602 .01 .02  .4
    "01-06-1999" 12151 .01 .02 .17
    "01-07-1999" 11617 .05   0 .07
    "01-08-1999" 11952 .02 .03 .04
    "01-09-1999" 12037 .03 .29   0
    "01-10-1999" 13051 .02 .07 .02
    "01-11-1999" 12063 .02 .04 .05
    "01-12-1999" 12127 .02 .05 .11
    "01-01-2000" 12456 .02 .04 .02
    "01-02-2000" 11758 .02 .04 .14
    "01-03-2000" 13022 .01 .04 .05
    "01-04-2000" 12022 .02 .03 .03
    "01-05-2000" 12386 .02 .03 .03
    "01-06-2000" 12140 .05 .08 .02
    "01-07-2000" 12398 .01 .05 .06
    "01-08-2000" 12753 .05 .04 .04
    "01-09-2000" 13219 .02 .01 .05
    "01-10-2000" 14055 .03 .01 .01
    "01-11-2000" 13361 .01 .01 .04
    "01-12-2000" 12844 .05 .02 .06
    end

    Now I want to get the average for each variable ( "count_aus", "count_ind" and "count_us") for each year

    So my basic code is
    Code:
    egen mean_count_aus=mean(count_aus), by ( year  )
    Now there is no year in my data but I think I can get it from the month though I tried to use "year" function it didnt work but if at all I can create year some how, how can I create loop to get the average values of all varialbes in one code.
    Something that starts like this
    foreach v of var count_* {

    Can someone help me here. I am not very comfortable with loops so I tried but to no avail. As I have some 43 variables if I try using egen, it will be tedious.


  • #2
    Here is a solution without loops. The trick is to first turn your string variable month in a Stata date variable. After that, there are many more functions available, for example a function year() that extracts the year from a date variable. See help datetime for more.

    Code:
    . // turn month in a Stata date
    . gen date = date(month, "DMY")
    
    .
    . // display the Stata date in a way that is easier for humans
    . format date %td
    
    .
    . // extract the year
    . gen year = year(date)
    
    .
    . // use year to create the variable you want
    . egen mean_count_aus=mean(count_aus), by ( year  )
    
    .
    . // admire the result
    . list, sepby(year)
    
         +-----------------------------------------------------------------------------------+
         |      month   total   coun~aus   count_~d   count_us        date   year   mean_c~s |
         |-----------------------------------------------------------------------------------|
      1. | 01-01-1998   11968        .03        .09        .01   01jan1998   1998       .025 |
      2. | 01-02-1998   11328        .01         .1        .02   01feb1998   1998       .025 |
      3. | 01-03-1998   12571        .01        .05        .02   01mar1998   1998       .025 |
      4. | 01-04-1998   11951        .02        .03        .02   01apr1998   1998       .025 |
      5. | 01-05-1998   12753        .02        .06        .04   01may1998   1998       .025 |
      6. | 01-06-1998   11933        .02        .04        .08   01jun1998   1998       .025 |
      7. | 01-07-1998   11776        .03        .05        .02   01jul1998   1998       .025 |
      8. | 01-08-1998   11800        .03         .1        .02   01aug1998   1998       .025 |
      9. | 01-09-1998   12657        .02        .02        .05   01sep1998   1998       .025 |
     10. | 01-10-1998   13029        .05        .01        .15   01oct1998   1998       .025 |
     11. | 01-11-1998   12264        .04        .02        .05   01nov1998   1998       .025 |
     12. | 01-12-1998   12140        .02        .09        .04   01dec1998   1998       .025 |
         |-----------------------------------------------------------------------------------|
     13. | 01-01-1999   12545        .02        .06        .14   01jan1999   1999   .0216667 |
     14. | 01-02-1999   11366        .01        .02        .08   01feb1999   1999   .0216667 |
     15. | 01-03-1999   12671        .04        .05        .31   01mar1999   1999   .0216667 |
     16. | 01-04-1999   12302        .01        .05        .54   01apr1999   1999   .0216667 |
     17. | 01-05-1999   12602        .01        .02         .4   01may1999   1999   .0216667 |
     18. | 01-06-1999   12151        .01        .02        .17   01jun1999   1999   .0216667 |
     19. | 01-07-1999   11617        .05          0        .07   01jul1999   1999   .0216667 |
     20. | 01-08-1999   11952        .02        .03        .04   01aug1999   1999   .0216667 |
     21. | 01-09-1999   12037        .03        .29          0   01sep1999   1999   .0216667 |
     22. | 01-10-1999   13051        .02        .07        .02   01oct1999   1999   .0216667 |
     23. | 01-11-1999   12063        .02        .04        .05   01nov1999   1999   .0216667 |
     24. | 01-12-1999   12127        .02        .05        .11   01dec1999   1999   .0216667 |
         |-----------------------------------------------------------------------------------|
     25. | 01-01-2000   12456        .02        .04        .02   01jan2000   2000   .0258333 |
     26. | 01-02-2000   11758        .02        .04        .14   01feb2000   2000   .0258333 |
     27. | 01-03-2000   13022        .01        .04        .05   01mar2000   2000   .0258333 |
     28. | 01-04-2000   12022        .02        .03        .03   01apr2000   2000   .0258333 |
     29. | 01-05-2000   12386        .02        .03        .03   01may2000   2000   .0258333 |
     30. | 01-06-2000   12140        .05        .08        .02   01jun2000   2000   .0258333 |
     31. | 01-07-2000   12398        .01        .05        .06   01jul2000   2000   .0258333 |
     32. | 01-08-2000   12753        .05        .04        .04   01aug2000   2000   .0258333 |
     33. | 01-09-2000   13219        .02        .01        .05   01sep2000   2000   .0258333 |
     34. | 01-10-2000   14055        .03        .01        .01   01oct2000   2000   .0258333 |
     35. | 01-11-2000   13361        .01        .01        .04   01nov2000   2000   .0258333 |
     36. | 01-12-2000   12844        .05        .02        .06   01dec2000   2000   .0258333 |
         +-----------------------------------------------------------------------------------+
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thanks Maarten Buis for the timely help especially the year function which I almost forgot. Sir, if I want to do this for say count_ind, I have to do it and similarly for others also. So is there a shortcut through which I can quickly the average per year for the variables

      Comment


      • #4
        You could loop, but for a small number of variables that is probably overkill. The loop in this case would be:

        Code:
        foreach var of varlist count_aus count_ind count_us {  
            egen mean_`var'=mean(`var'), by ( year )
        }
        Notice that in this case it takes three lines of code, while if I did three separate egen commands I would also need three lines of code...
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Dear Maarten Buis, you are absolutely correct that in my demo example 3 lines of codes separate are equal to constructing a loop but in the reality I have some 43 columns so I asked your help for the code and the one you gave me is the exact one that I require

          Comment

          Working...
          X