Announcement

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

  • foreach loop

    Hi, I have to create the average monthly exposure during the first trimester, second trimester, third trimester, exposure during 3 to 6 months, 6 to 9 months, and 1 to 9 months in utero.

    I have written a loop that calculates temperature exposure during 1 to 3, 1 to 6 and 1 to 9 months and merges the files into one file. I am confused about how I can incorporate 3 to 6 months of exposure and 6 to 9 months of exposure.


    foreach var in 3 6 9{
    use hh ,clear

    rename month month_child
    rename year year_child

    geonear ID LATNUM_hh LONGNUM_hh using "air_temp_all.dta", n(temp_ID LATNUM_temp LONGNUM_temp) miles genstub(ignore)
    rename ignore temp_ID
    gen time_var = ym(year_child, month_child)

    expand `var'
    sort ID
    by ID: generate time_`var' = time_var + 1 - _n
    format %tm time_`var'
    gen month = month(dofm(time_`var'))
    gen year = year(dofm(time_`var'))

    egen UNID = group (ID month year)


    save neardistance, replace

    merge 1:1 temp_ID month year using air_temp_all
    keep if _merge == 3
    drop _merge
    drop month
    drop year
    collapse (mean) temp`var'pre=temp precip`var'pre=precip , by(ID)

    save hh_`var', replace


    }


    use hh_3, clear
    foreach var in 6 9{

    merge 1:1 ID using hh_`var'
    drop _merge
    save hh, replace

    }


  • #2
    I don't understand. What about this code doesn't do what you want, or what does it do that it shouldn't do?

    Comment


    • #3
      Originally posted by Jared Greathouse View Post
      I don't understand. What about this code doesn't do what you want, or what does it do that it shouldn't do?
      Hi Thanks for the comment


      I have to calculate the average temperature exposure during


      first three months of in utero period
      first six month of in utero period
      9 months exposure

      3 to 6 months in utero exposure
      6 to 9 months in utero exposure



      the code does the first three but I am confused about the last two please


      I just asking how I have incorporate the last two in the code please


      Comment


      • #4
        Okay well give an example of your dataset and I'll look.

        Comment

        Working...
        X