Announcement

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

  • How to destring date variable formatted as year and week number

    Dear Statalist,

    I was just wondering if there is an easy way to destring a date variable formatted as "year and week number".
    for example,

    2008w34
    2008w35
    2008w36
    2008w37
    2008w38


    Thanks so much for your help!

    Yonatan

  • #2
    You shouldn't want to destring that. How to do it follows from reading the help -- ignore("w") -- but that would mean values like 200834, which are not closer to Stata numeric date variables.

    For more on why that is a bad idea, see

    SJ-18-4 dm0098 . . Speaking Stata: Seven steps for vexatious string variables
    . . . . . . . . . . . . . . . . . . . . N. J. Cox and C. B. Schechter
    Q4/18 SJ 18(4):981--994 (no commands)
    provides a step-by-step guide explaining how to convert string
    variables or -- as the case may merit -- to leave them as they
    are

    A superficial answer would be that the function weekly() is what you want, as in

    Code:
    . clear
    
    . input str7 myweek
    
            myweek
      1. 2008w34
      2. end
    
     gen wanted = weekly(myweek, "YW")
    
    . format %tw wanted
    
    . list
    
         +-------------------+
         |  myweek    wanted |
         |-------------------|
      1. | 2008w34   2008w34 |
         +-------------------+
    But hang on! How are your weeks defined? In the same way as Stata weeks? I doubt it. One tell-tale sign is if that you have any weekly dates that are week 53.

    For much more on weekly dates in Stata, see (for example) the recently active thread https://www.statalist.org/forums/for...-week-variable and its references.

    For anything other than Stata weeks:

    1. Work with the days of the week that define them.

    2. Use delta(7) with tsset or xtset.



    Comment


    • #3
      Thanks Nick!

      Comment


      • #4
        Hi Nick,
        May I ask you one more question?

        I just wondering if it is possible to estimate exact end date of each week (Stata week) given that that specific week number is known or some other exact date in that given week is known.

        Example;

        Let say, one patient received flu vaccination on 03/01/2017. It is week 1. Is it possible to estimate the exact end date of week l based on the provided information in Stata?

        I greatly appreciated your support.

        Yonatan

        Comment


        • #5
          You could test various possibilities. I'd look first at whether you ever have week 0 and/or week 53 in your data.

          It's better to drill down and get metadata from your data provider.

          Comment

          Working...
          X