Announcement

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

  • How to calculate the row Mean within an specific Range?

    Dear Stata Comunity,

    Basically I want to calculate the mean using an specific range:

    first last yr_1990 yr_1991 yr_1992 yr_1993 yr_1994 yr_1995 yr_1996
    1990 1994 300 500 600 1000 400 300 100
    1995 1996 300 500 600 1000 400 300 100



    So the idea is using the variables first and last, calculate the row mean between this period. In this case I need to calculate:

    (300+500+600+1000+400)/4=560

    And then the next it will be from 1995 to 1996, so


    (300+100)/2=200

    Any Idea How to calculate this in STATA?
    Im trying with rmean, but I dont know how to tell stata to calculate for the range that I need.

    Thanks a lot!!!!

    CARLOS.











    Last edited by cpineda_stata; 14 May 2015, 04:38.

  • #2
    Code:
    gen total = 0
    gen count = 0
    
    quietly forval y = 1990/1996 {
        replace total = total + inrange(`y', first, last) * yr_`y'
        replace count = count + inrange(`y', first, last)
    }
    
    gen mean = total/count
    All that said, many problems will be tough with this data structure. Consider a different structure.

    Code:
    input first last yr_1990 yr_1991 yr_1992 yr_1993 yr_1994 yr_1995 yr_1996
    1990 1994 300 500 600 1000 400 300 100
    1995 1996 300 500 600 1000 400 300 100
    end
    
    gen id = _n
    reshape long yr_, i(id)
    
    . l
    
         +---------------------------------+
         | id     _j   first   last    yr_ |
         |---------------------------------|
      1. |  1   1990    1990   1994    300 |
      2. |  1   1991    1990   1994    500 |
      3. |  1   1992    1990   1994    600 |
      4. |  1   1993    1990   1994   1000 |
      5. |  1   1994    1990   1994    400 |
         |---------------------------------|
      6. |  1   1995    1990   1994    300 |
      7. |  1   1996    1990   1994    100 |
      8. |  2   1990    1995   1996    300 |
      9. |  2   1991    1995   1996    500 |
     10. |  2   1992    1995   1996    600 |
         |---------------------------------|
     11. |  2   1993    1995   1996   1000 |
     12. |  2   1994    1995   1996    400 |
     13. |  2   1995    1995   1996    300 |
     14. |  2   1996    1995   1996    100 |
         +---------------------------------+
    
     12. |  2   1994    1995   1996    400   457.1429 |
     13. |  2   1995    1995   1996    300   457.1429 |
     14. |  2   1996    1995   1996    100   457.1429 |
         +--------------------------------------------+
    
    . drop mean
    
    . bysort id : egen mean = mean(yr_ / (inrange(_j, first, last)))
    
    . l
    
         +----------------------------------------+
         | id     _j   first   last    yr_   mean |
         |----------------------------------------|
      1. |  1   1990    1990   1994    300    560 |
      2. |  1   1991    1990   1994    500    560 |
      3. |  1   1992    1990   1994    600    560 |
      4. |  1   1993    1990   1994   1000    560 |
      5. |  1   1994    1990   1994    400    560 |
         |----------------------------------------|
      6. |  1   1995    1990   1994    300    560 |
      7. |  1   1996    1990   1994    100    560 |
      8. |  2   1990    1995   1996    300    200 |
      9. |  2   1991    1995   1996    500    200 |
     10. |  2   1992    1995   1996    600    200 |
         |----------------------------------------|
     11. |  2   1993    1995   1996   1000    200 |
     12. |  2   1994    1995   1996    400    200 |
     13. |  2   1995    1995   1996    300    200 |
     14. |  2   1996    1995   1996    100    200 |
         +----------------------------------------+
    Another way to do that last

    Code:
    . bysort id : egen mean = mean(cond(inrange(_j, first, last), yr_, .))
    Your variable names would benefit from editing in this structure.
    Last edited by Nick Cox; 14 May 2015, 04:48.

    Comment


    • #3
      Thanks a LOT Nick,

      You have just increased my stata knowledge with the comand inrange
      TAHNKS THANKS A LOT!!!

      It works perfectly,

      CARLOS.

      Comment


      • #4
        On the inrange() function, see

        [FN] Programming functions . . . . . . . . . inrange() programming function
        (help inrange())

        SJ-7-4 dm0033 . . . . . . Speaking Stata: Counting groups, especially panels
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
        Q4/07 SJ 7(4):571--581 (no commands)
        discusses how to count panels through reduction commands
        or through tabulation commands and how to overcome
        problems that do not yield easily to these approaches

        SJ-6-4 dm0026 . . . . . . Stata tip 39: In a list or out? In a range or out?
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
        Q4/06 SJ 6(4):593--595 (no commands)
        tip for use of inlist() and inrange()

        Comment


        • #5
          Thanks a LOT Nick
          You are very kind,

          Carlos.

          Comment

          Working...
          X