Announcement

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

  • How to assign different Treatment depending on the date of birth

    Hello,
    I have two different datasets, in the first one i have a ID identifier for each person and the date of birth ( i used the command date). Then, i have a second dataset in which each observation is a different day for different air pollution variables (CO, PM10 etc). What i want to do is to assign for each ID in my first dataset the mean of the air pollution variable for the 30 days before the date of birth.
    I hope someone can help me out with this one, and any type of information on how i could procede it would be very helpful.
    Thanks for the time!
    Sebastian

  • #2
    As you don't provide any example data to work with, I have created some demonstration data that I imagine resembles your data. To the extent it does not, we will have both wasted our time.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long id float dob
     1 -23
     2  22
     3   6
     4  20
     5 -17
     6   6
     7  24
     8  -9
     9 -15
    10  22
    end
    format %td dob
    tempfile person_data
    save `person_data'
    
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long date float measure
    -25  29.65739
    -24  11.14349
    -23 10.803738
    -22  6.077013
    -21 11.369622
    -20  19.86025
    -19   14.1388
    -18  8.761109
    -17 16.939981
    -16  5.937008
    -15  16.80749
    -14  7.992027
    -13  6.696951
    -12  7.774796
    -11  9.995811
    -10 18.008116
     -9  6.835224
     -8  5.214801
     -7  17.24388
     -6   5.11303
     -5  9.948895
     -4 10.179934
     -3  26.45075
     -2  7.442179
     -1 2.9290025
      0  3.440302
      1  29.30642
      2  3.985446
      3 13.453658
      4 4.1511045
      5 21.523777
      6  2.673076
      7 17.224815
      8  .6795027
      9 18.249796
     10   2.58044
     11   6.45596
     12  6.572391
     13  5.440248
     14  7.886301
     15  3.838931
     16  15.52381
     17  15.45446
     18  8.562012
     19   7.47939
     20  3.125802
     21  4.972286
     22 13.892295
     23 15.675455
     24  5.163946
     25  7.120703
    end
    format %td date
    tempfile environment_data
    save `environment_data'
    
    use `person_data', clear
    clonevar date = dob
    rangejoin date -30 -1 using `environment_data'
    collapse (mean) measure (first) dob, by(id)
    -rangejoin- is written by Robert Picard and is available from SSC.

    The code assumes that by "the 30 days before" you mean days -1 through -30, excluding the date of birth itself. Adjust the parameters of -rangejoin- accordingly if you had something else in mind. The variable "measure" in my code is just a standin for your CO, PM10, etc. Modify the code to include the actual names of your variables where I have used measure.

    In the future, when asking for help with code, always show example data. When showing example data, always use -dataex-, as I have done here.

    If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.


    Comment


    • #3
      Hello Clyde,
      Its my first time here so im sorry for to showing any example data, next time i will do it. Anyway, the data you showed me it was exactly what i have, so your information was very helpful for my purpose. I'll try working on it to see how it goes. Thank you for your time
      Sebastian

      Comment

      Working...
      X