Announcement

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

  • Coefficient of variation using time variables & loops

    [For information purposes: I am using Stata/MP version 15.1]

    Dear all,

    I am trying to find a way to calculate/generate the coefficient of variation for different time periods (years, quarters & months, as well as team changes).

    My main issue is that I am trying to calculate the coefficient of variation of year/quarter/month x, if the person was present in the team at that time, with time 0 being the Date of Incorporation (when the company was founded). However, I need a way to know the age of the person at each time period.

    I got this far for calculating the years, but it comes with it's mistakes. For example, for some people present during those years, it did not calculate a coefficient of variation

    Code:
    bysort BvdIdNumber: gen age_y0 = (DateIncorporation- BirthDate)/365.25
    forvalues i = 0/16 {
    
    bys BvdIdNumber: egen meanage_year`i' = mean(age_t0+`i') if year`i' == 1
    bys BvdIdNumber: egen sdage_year`i' = sd( age_t0+`i') if year`i' == 1
    gen Age_year`i' = sdage_year`i'/meanage_year`i'
    bys BvdIdNumber: egen VariationAge_year`i' = mean(Age_year`i')
    drop meanage_year`i' sdage_year`i' Age_year`i'
    }
    SideNote: I use 16 as I am only looking at 16 years of data (2005 (included) to 2020).

    However, I am not sure how to do this for other time measurements (months and quarters).
    I have the BirthDate, DateofIncorporation (when the firm began, which is time 0).

    For change, I have a variable which tells me the date of when a change in team (someone joined or left) occured.
    So, I believe the following code would be a great start to solve this issue, but I have encoured the following problem (variable Age_year1 already defined
    r(110);
    )

    Code:
    forvalues i = 1/27 {
    
    bys BvdIdNumber: egen meanage_change`i' = mean(DateIncorporation-change_`i') if change_present`i' == 1
    bys BvdIdNumber: egen sdage_change`i' = sd(DateIncorporation-change_`i') if change_present`i' == 1
    gen Age_year`i' = sdage_change`i'/meanage_change`i'
    bys BvdIdNumber: egen VariationAge_change`i' = mean(Age_change`i')
    drop meanage_year`i' sdage_change`i' Age_change`i'
    }

    Below an excerpt of my data using dataex. I have included

    BvdIdNumber: company number
    DMUci: employee number
    DateIncorporation: when company started
    Age_y0: age of person at time company started
    Change_1-change_5: dates when a change (someone joined/left the team) occured
    Change_present1-Change_present5 : dummy variable, was the person present during said change.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str16 BvdIdNumber float DateIncorporation str10 DMUci float(BirthDate age_y0 change_1 change_2 change_3 change_4 change_5 change_present1 change_present2 change_present3 change_present4 change_present5)
    "AT9070422953" 21164 "P300272480" 12160  24.65161 21808     .     .     .     . 1 . . . .
    "AT9070422953" 21164 "P301627755" 12852 22.757015 21808     .     .     .     . 1 . . . .
    "BE0501515635" 19318 "P144431731"  8755  28.91992 20121 21774     .     .     . 1 1 . . .
    "BE0501515635" 19318 "P238808799"  8937  28.42163 20121 21774     .     .     . 1 1 . . .
    "BE0501562254" 19318 "P044740365"  4467  40.65982 19814 21487     .     .     . 1 1 . . .
    "BE0501562254" 19318 "P044740365"  4467  40.65982 19814 21487     .     .     . 1 1 . . .
    "BE0501562254" 19318 "P044740365"  4467  40.65982 19814 21487     .     .     . 1 1 . . .
    "BE0501562254" 19318 "P123993699" 10456 24.262835 19814 21487     .     .     . 1 1 . . .
    "BE0501562254" 19318 "P123993699" 10456 24.262835 19814 21487     .     .     . 1 1 . . .
    "BE0501562254" 19318 "P123993699" 10456 24.262835 19814 21487     .     .     . 1 1 . . .
    "BE0505804718" 20054 "P103923129"  4749  41.90281 20800     .     .     .     . 1 . . . .
    "BE0505804718" 20054 "P103923129"  4749  41.90281 20800     .     .     .     . 1 . . . .
    "BE0505804718" 20054 "P238328579"  2922  46.90486 20800     .     .     .     . 1 . . . .
    "BE0505804718" 20054 "P238328579"  2922  46.90486 20800     .     .     .     . 1 . . . .
    "BE0505804718" 20054 "P246173452"  5114  40.90349 20800     .     .     .     . 1 . . . .
    "BE0505804718" 20054 "P246173452"  5114  40.90349 20800     .     .     .     . 1 . . . .
    "BE0518917237" 19414 "P042037775" -5047 66.970566 22140     .     .     .     . 1 . . . .
    "BE0518917237" 19414 "P042037775" -5047 66.970566 22140     .     .     .     . 1 . . . .
    "BE0518917237" 19414 "P044189380" -5050  66.97878 22140     .     .     .     . 1 . . . .
    "BE0536436625" 19537 "P009351640"  2557   46.4887 20119 20438     .     .     . 1 1 . . .
    "BE0536436625" 19537 "P009351640"  2557   46.4887 20119 20438     .     .     . 1 1 . . .
    "BE0536436625" 19537 "P009351700"     0  53.48939 20119 20438     .     .     . 1 1 . . .
    "BE0536436625" 19537 "P009351700"     0  53.48939 20119 20438     .     .     . 1 1 . . .
    "BE0536436625" 19537 "P014424187" -1461  57.48939 20119 20438     .     .     . 1 1 . . .
    "BE0536436625" 19537 "P014424187" -1461  57.48939 20119 20438     .     .     . 1 1 . . .
    "BE0536436625" 19537 "P048688796"  4018   42.4887 20119 20438     .     .     . 1 1 . . .
    "BE0536436625" 19537 "P048688796"  4018   42.4887 20119 20438     .     .     . 1 1 . . .
    "BE0555980046" 19914 "P039659380"  1827  49.51951 20305 21236     .     .     . 1 1 . . .
    "BE0555980046" 19914 "P076509994"  3967  43.66051 20305 21236     .     .     . 1 1 . . .
    "BE0656918246" 20627 "P141888237" 10804  26.89391 22089     .     .     .     . 1 . . . .
    "BE0656918246" 20627 "P141888237" 10804  26.89391 22089     .     .     .     . 1 . . . .
    "BE0656918246" 20627 "P349820174"  7615 35.624916 22089     .     .     .     . 1 . . . .
    "BE0656918246" 20627 "P349820174"  7615 35.624916 22089     .     .     .     . 1 . . . .
    "BE0657890622" 20635 "P077578372"  1784  51.61123 21720     .     .     .     . 1 . . . .
    "BE0657890622" 20635 "P350651532"  2140  50.63655 21720     .     .     .     . 1 . . . .
    "BE0664799594" 20745 "P006533911"  3497  47.22245 21906     .     .     .     . 1 . . . .
    "BE0664799594" 20745 "P006533913"  2968  48.67077 21906     .     .     .     . 1 . . . .
    "BE0664799594" 20745 "P263565204" -7435  77.15263 21906     .     .     .     . 1 . . . .
    "BE0808687713" 17889 "P043459088"  1260  45.52772 18629 21095     .     .     . 1 1 . . .
    "BE0808687713" 17889 "P043459088"  1260  45.52772 18629 21095     .     .     . 1 1 . . .
    "BE0808687713" 17889 "P043459088"  1260  45.52772 18629 21095     .     .     . 1 1 . . .
    "BE0808687713" 17889 "P043459088"  1260  45.52772 18629 21095     .     .     . 1 1 . . .
    "BE0808687713" 17889 "P043459088"  1260  45.52772 18629 21095     .     .     . 1 1 . . .
    "BE0808687713" 17889 "P068611593"  2936  40.93908 18629 21095     .     .     . 1 1 . . .
    "BE0808687713" 17889 "P068611593"  2936  40.93908 18629 21095     .     .     . 1 1 . . .
    "BE0808687713" 17889 "P068611593"  2936  40.93908 18629 21095     .     .     . 1 1 . . .
    "BE0808687713" 17889 "P068611593"  2936  40.93908 18629 21095     .     .     . 1 1 . . .
    "BE0808687713" 17889 "P068611593"  2936  40.93908 18629 21095     .     .     . 1 1 . . .
    "BE0810603264" 17975 "P123366320"  5725 33.538673 19328     .     .     .     . 1 . . . .
    "BE0810603264" 17975 "P123366320"  5725 33.538673 19328     .     .     .     . 1 . . . .
    "BE0810603264" 17975 "P123574091"  4289 37.470226 19328     .     .     .     . 1 . . . .
    "BE0810603264" 17975 "P123574091"  4289 37.470226 19328     .     .     .     . 1 . . . .
    "BE0812211682" 18058 "P046290225"  1045  46.57906 21825     .     .     .     . 1 . . . .
    "BE0812211682" 18058 "P046290228"   490  48.09856 21825     .     .     .     . 1 . . . .
    "BE0818085924" 18129 "P001050215"  -769   51.7399 20557 20754     .     .     . 1 1 . . .
    "BE0818085924" 18129 "P001050215"  -769   51.7399 20557 20754     .     .     . 1 1 . . .
    "BE0818085924" 18129 "P001050215"  -769   51.7399 20557 20754     .     .     . 1 1 . . .
    "BE0818085924" 18129 "P001050215"  -769   51.7399 20557 20754     .     .     . 1 1 . . .
    "BE0818085924" 18129 "P001068111"  2356  43.18412 20557 20754     .     .     . 1 1 . . .
    "BE0818085924" 18129 "P001068111"  2356  43.18412 20557 20754     .     .     . 1 1 . . .
    "BE0818085924" 18129 "P194531680" -2901    57.577 20557 20754     .     .     . 1 1 . . .
    "BE0818085924" 18129 "P194531680" -2901    57.577 20557 20754     .     .     . 1 1 . . .
    "BE0818085924" 18129 "P194531680" -2901    57.577 20557 20754     .     .     . 1 1 . . .
    "BE0818085924" 18129 "P194531680" -2901    57.577 20557 20754     .     .     . 1 1 . . .
    "BE0822663730" 18284 "P010015992"  -838  52.35318 20452 20453 20852 21285 21299 1 1 1 1 1
    "BE0822663730" 18284 "P010015992"  -838  52.35318 20452 20453 20852 21285 21299 1 1 1 1 1
    "BE0822663730" 18284 "P194256668"  3939  39.27447 20452 20453 20852 21285 21299 1 0 0 0 0
    "BE0822663730" 18284 "P194256668"  3939  39.27447 20452 20453 20852 21285 21299 1 0 0 0 0
    "BE0822663730" 18284 "P210520024"  1526  45.88091 20452 20453 20852 21285 21299 0 0 1 1 1
    "BE0822663730" 18284 "P210520024"  1526  45.88091 20452 20453 20852 21285 21299 0 0 1 1 1
    "BE0822663730" 18284 "P300158435"  7342 29.957563 20452 20453 20852 21285 21299 0 1 1 1 1
    "BE0822663730" 18284 "P300158435"  7342 29.957563 20452 20453 20852 21285 21299 0 1 1 1 1
    "BE0822663730" 18284 "P415739933"  4950 36.506504 20452 20453 20852 21285 21299 0 0 0 1 1
    "BE0822663730" 18284 "P415739933"  4950 36.506504 20452 20453 20852 21285 21299 0 0 0 1 1
    "BE0822663730" 18284 "P469602315"  4456    37.859 20452 20453 20852 21285 21299 0 0 0 0 0
    "BE0822663730" 18284 "P469602315"  4456    37.859 20452 20453 20852 21285 21299 0 0 0 0 0
    "BE0822726086" 18276 "P049564102"  1004  47.28816 20444     .     .     .     . 1 . . . .
    "BE0822726086" 18276 "P049564102"  1004  47.28816 20444     .     .     .     . 1 . . . .
    "BE0822726086" 18276 "P049564102"  1004  47.28816 20444     .     .     .     . 1 . . . .
    "BE0822726086" 18276 "P063938116"  5027 36.273785 20444     .     .     .     . 1 . . . .
    "BE0822726086" 18276 "P063938116"  5027 36.273785 20444     .     .     .     . 1 . . . .
    "BE0822726086" 18276 "P063938116"  5027 36.273785 20444     .     .     .     . 1 . . . .
    "BE0825343207" 18374 "P005145231" -4281  62.02601 21600     .     .     .     . 1 . . . .
    "BE0825343207" 18374 "P005145231" -4281  62.02601 21600     .     .     .     . 1 . . . .
    "BE0825343207" 18374 "P005145231" -4281  62.02601 21600     .     .     .     . 1 . . . .
    "BE0825343207" 18374 "P041721435"  1107  47.27447 21600     .     .     .     . 1 . . . .
    "BE0825343207" 18374 "P041721435"  1107  47.27447 21600     .     .     .     . 1 . . . .
    "BE0825343207" 18374 "P041721435"  1107  47.27447 21600     .     .     .     . 1 . . . .
    "BE0830917737" 18569 "P005223488"  3570  41.06503 20270 20436     .     .     . 1 1 . . .
    "BE0830917737" 18569 "P005223488"  3570  41.06503 20270 20436     .     .     . 1 1 . . .
    "BE0830917737" 18569 "P007073447"  4383  38.83915 20270 20436     .     .     . 1 1 . . .
    "BE0830917737" 18569 "P007073447"  4383  38.83915 20270 20436     .     .     . 1 1 . . .
    "BE0835023906" 18716 "P011838447"   366  50.23956 19708     .     .     .     . 1 . . . .
    "BE0835023906" 18716 "P011838447"   366  50.23956 19708     .     .     .     . 1 . . . .
    "BE0835023906" 18716 "P019045531"   366  50.23956 19708     .     .     .     . 1 . . . .
    "BE0835023906" 18716 "P019045531"   366  50.23956 19708     .     .     .     . 1 . . . .
    "BE0835023906" 18716 "P116393668"   731  49.24025 19708     .     .     .     . 1 . . . .
    "BE0835023906" 18716 "P116393668"   731  49.24025 19708     .     .     .     . 1 . . . .
    "BE0835023906" 18716 "P116393668"   731  49.24025 19708     .     .     .     . 1 . . . .
    end
    format %td DateIncorporation
    format %td BirthDate
    format %td change_1
    format %td change_2
    format %td change_3
    format %td change_4
    format %td change_5

    Thanks in advance for your help! Any tips to guide me in the right direction are very welcomed!
    Please let me know if something is not clear/ not following the guidelines, thanks!

    Kind regards,
    Laura
Working...
X