Announcement

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

  • mean exposure on panel data

    Hi all,

    I have panel data with date of birth (dt_birth), date of conception (dt_conception), gestational length (weeks_gestation and days_gestation), and daily exposure to smoke. The exposure data is aligned with the date of birth. That means that the value in that row means that the smoke was measured in date of birth.

    The data looks like this:
    Click image for larger version

Name:	f9960739-dc9d-4cab-b8ea-e834e3a22ff6.jpg
Views:	1
Size:	114.3 KB
ID:	1634839
    Well, what I need is to calculate the average smoke exposure between conception and birth. I tried to use
    Code:
    egen mean_smoke = mean(inrange(dt_conception - dt_birth) * smoke)
    but it didn't work.

    It's important to highlight that as I have repeated measures (several births with different gestational ages on the same day). So I need that it calculates the exposure per row (case) and not per day of birth only.

    Any ideas on how I could do this?

    Thank you in advance.




  • #2
    It's not helpful to say something "didn't work." There are many ways in which something can fail, but usually only one way in which it can meet your expectations. It is much better to say what actually happened--which, in the context of this forum, means showing the actual response you got from Stata and, unless it is blatantly obvious, explaining why that's not what you wanted.

    In this case, however, it is clear that inrange(dt_conception - dt_birth) is a syntax error. inrange() is a function that requires three arguments. Its semantics is that -inrange(x, a, b)- is 1 if a <= x <= b and 0 otherwise (with some exceptions when a or b is a missing value, that are not relevant to discuss here.) What I think you want to do is calculate the number of days from conception to birth and multiply that by the smoke variable. So that would be:

    Code:
    gen smoke_exposure = (dt_birth - dt_conception) * smoke
    That will give you a separate estimate of smoke_exposure for each observation in the data set. If you also need the mean value of that you can get that with:
    Code:
    summ smoke_exposure
    local mean_smoke_exposure = r(mean)
    There you have the mean value of the smoke exposure variable stored in a local macro named mean_smoke_exposure, and you can refer to that later in the code if you need to. You can also create a "variable" containing that value in every observation, but storing constants in variables is usually not wise and should only be done if there is a special reason that it is necessary.

    Comment

    Working...
    X