Announcement

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

  • Problem with Subtracting Variable that Represents a Number of Weeks from a Date Variable

    I am currently working with a set of data that contains the Date of Birth (dob) of an observation and the # of Weeks of Gestation at delivery. I am trying to figure out a way to generate a variable which represents the Date of Inception by subtracting the number of weeks of gestation from the dob. So far I've used these commands, but the DoI variable I'm generating is giving me incorrect values which are probably due to week representing the week in year 1960.

    *wrong
    format dob %tw
    gen weekob = wofd(dob)
    gen doincep = weekob - gestwk

    Any help would be appreciated as I am fairly new to Stata and this forum. Thank you so much for any time and effort!!

  • #2
    You don't really show enough of what data you're working with to judge what you might be doing wrong, but your commands seem okay. You could try something analogous to the following:

    .ÿ
    .ÿversionÿ15.1

    .ÿ
    .ÿclearÿ*

    .ÿ
    .ÿsetÿseedÿ`=strreverse("1493155")'

    .ÿquietlyÿsetÿobsÿ10

    .ÿ
    .ÿgenerateÿintÿbirth_dtÿ=ÿdate("2019-01-01",ÿ"YMD")ÿ+ÿruniformint(0,ÿ365)

    .ÿformatÿbirth_dtÿ%tdCY-N-D

    .ÿ
    .ÿgenerateÿbyteÿgestation_durationÿ=ÿruniformint(23,ÿ41)

    .ÿ
    .ÿ*
    .ÿ*ÿBeginÿhere
    .ÿ*
    .ÿgenerateÿintÿbirth_wkÿ=ÿwofd(birth_dt)

    .ÿgenerateÿintÿinception_wkÿ=ÿbirth_wkÿ-ÿgestation_duration

    .ÿ
    .ÿformatÿ*_wkÿ%twCCYY_!W!e!e!k_WW

    .ÿ
    .ÿlist,ÿnoobsÿseparator(0)ÿabbreviate(20)

    ÿÿ+---------------------------------------------------------------+
    ÿÿ|ÿÿÿbirth_dtÿÿÿgestation_durationÿÿÿÿÿÿÿbirth_wkÿÿÿinception_wkÿ|
    ÿÿ|---------------------------------------------------------------|
    ÿÿ|ÿ2019-03-02ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ38ÿÿÿ2019ÿWeekÿ09ÿÿÿ2018ÿWeekÿ23ÿ|
    ÿÿ|ÿ2019-11-14ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ35ÿÿÿ2019ÿWeekÿ46ÿÿÿ2019ÿWeekÿ11ÿ|
    ÿÿ|ÿ2019-06-30ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ23ÿÿÿ2019ÿWeekÿ26ÿÿÿ2019ÿWeekÿ03ÿ|
    ÿÿ|ÿ2019-09-27ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ24ÿÿÿ2019ÿWeekÿ39ÿÿÿ2019ÿWeekÿ15ÿ|
    ÿÿ|ÿ2019-08-11ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ37ÿÿÿ2019ÿWeekÿ32ÿÿÿ2018ÿWeekÿ47ÿ|
    ÿÿ|ÿ2019-10-02ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ26ÿÿÿ2019ÿWeekÿ40ÿÿÿ2019ÿWeekÿ14ÿ|
    ÿÿ|ÿ2019-04-14ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ30ÿÿÿ2019ÿWeekÿ15ÿÿÿ2018ÿWeekÿ37ÿ|
    ÿÿ|ÿ2019-05-18ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ29ÿÿÿ2019ÿWeekÿ20ÿÿÿ2018ÿWeekÿ43ÿ|
    ÿÿ|ÿ2019-11-02ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ34ÿÿÿ2019ÿWeekÿ44ÿÿÿ2019ÿWeekÿ10ÿ|
    ÿÿ|ÿ2019-12-14ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ30ÿÿÿ2019ÿWeekÿ50ÿÿÿ2019ÿWeekÿ20ÿ|
    ÿÿ+---------------------------------------------------------------+

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .

    Comment


    • #3
      Joseph Coveney Thank you so much for that thorough description! I finally figured out that my mistake was that I had gotten mixed up with which format I was currently using. In my original line of code I was mistaking the week of inception with the date of inception so once I added a couple more lines of code I was able to pull the correct date. Sorry my original post was lacking in information. This was my first time posting, so I was not sure of how to add that information. Thank you regardless, and you seem to be very knowledgeable. I may send a couple more questions your way as they develope.

      Comment


      • #4
        It would usually be better to subtract 7 times the number of weeks from the date of birth. Stata's definition of weeks as implemented in its week functions doesn't usually match any purpose but nesting weeks in years, as explained at length in many threads here.

        https://www.statalist.org/forums/for...-week-variable is an example and contains references to the literature.
        Last edited by Nick Cox; 14 Apr 2019, 00:26.

        Comment


        • #5
          Thanks, Nick. I vaguely remember now the caveat read somewhere (documentation?) long ago but had forgotten, never having had the need to use them.

          Comment

          Working...
          X