Announcement

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

  • Performing operations by values of a specific unit, by year

    Hi All,

    I have data that resembles the following:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(individual year value)
    1 1999  .2
    2 1999 .54
    3 1999 .66
    1 2000   0
    2 2000   0
    3 2000  .4
    end
    ------------------ copy up to and including the previous line ------------------


    In the above, I have information on a variable "value" by individual i, for year t. However, I need to relativize the values for each individuals by those of individual 1. Specifically, for each year, I wish to divide the variable value for each individual by the value of the 1st individual. So, for the year 1999, the variable value should be replaced by value/2, that for 2000 should be replaced by value/0 and so on.

    Any guidance on this is much appreciated!

    Thanks,
    CS


  • #2
    You want to divide by zero (sometimes)?

    Comment


    • #3
      Yes sir. Its a small minority of cases.

      Comment


      • #4
        I have a solution:

        I simply do:

        Code:
        tempfile master
        save `master'
        keep if individual==1
        drop individual
        rename value value1
        merge 1:m year using `master'
        drop _merge

        Thereafter, I can easily divide as need be, by individual-year.

        Comment


        • #5
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(individual year value)
          1 1999  .2
          2 1999 .54
          3 1999 .66
          1 2000   0
          2 2000   0
          3 2000  .4
          end
          
          bysort year : gen divisor = value if individual == 1
          bysort year (divisor) : replace divisor = divisor[1]
          
          gen wanted = value / divisor
          See also https://www.stata-journal.com/articl...article=dm0055

          Naturally,

          Code:
          bysort year (individual) : gen divisor = value[1]
          works fine for your example, but the code above generalises more easily to arbitrary reference individuals.
          Last edited by Nick Cox; 24 Apr 2019, 01:32.

          Comment

          Working...
          X