Announcement

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

  • Categorizing a date variable

    Hi,

    I have a date variable dov (date of visit) in float %td format. The time period ranges from 2015 to 2018. I want to generate a time variable which will look like year1, year2, year3 only. Kindly help me in this.
    dov
    19-Jul-15
    1-May-15
    1-May-15
    10-Jun-15
    1-Aug-15
    20-Aug-15
    15-Mar-16
    2-Dec-16
    3-Dec-18
    23-Dec-16
    10-Feb-17
    2-Dec-16
    3-Dec-16
    2-Dec-16
    With Thanks
    Harish

  • #2
    Try the following code
    Code:
    gen numdate = substr(dov, strrpos(var1, "-")+1,.)
    destring numdate, replace
    egen mindate = min(numdate)
    gen numyear= 1 if numdate==mindate
    sort numyear
    replace numyear = numdate[_n]-numdate[1]+1 if _n>1
    tostring numyear, replace
    gen year = "year"+numyear
    destring numyear, replace
    drop numdate mindate
    You will get two variables
    1. numyear - representing year in numeric form
    2. year - representing year in year1, year2..form as you want

    Comment


    • #3
      Thanks for this.
      When I run the first line of the codes, it is showing as var1 not found. Could you please check if var1 required?

      Comment


      • #4
        I do not understand the task. How would this new year variable look for example for dates 10-Jun-15 and 23-Dec-16?

        Otherwise you can extract easily components from td variable, e.g.,
        Code:
        gen year = year(dov)
        will extract only the year in which the visit took place.

        Comment


        • #5
          You want a categorical variable that reflects the year of the visit (2015, 2016, 2017? Please note that you should provide a data example using dataex (see in the FAQ). It is difficult to help you without knowing how your data looks.

          Comment


          • #6
            Originally posted by Joro Kolev View Post
            I do not understand the task. How would this new year variable look for example for dates 10-Jun-15 and 23-Dec-16?

            Otherwise you can extract easily components from td variable, e.g.,
            Code:
            gen year = year(dov)
            will extract only the year in which the visit took place.
            Thanks a lot. It worked.

            Comment


            • #7
              Originally posted by Harish Kumar View Post
              Thanks for this.
              When I run the first line of the codes, it is showing as var1 not found. Could you please check if var1 required?
              Sorry for this. Replace var1 by dov.
              [CODE]

              Comment


              • #8
                Originally posted by Felix Scholl View Post
                You want a categorical variable that reflects the year of the visit (2015, 2016, 2017? Please note that you should provide a data example using dataex (see in the FAQ). It is difficult to help you without knowing how your data looks.
                Thanks for this, I will follow it.

                Comment


                • #9
                  Hi,

                  I have three variables in the attached data (date, PatientID, followup). I want to calculate the number of days between followup visits for each patient. Some patients had no followups, and some had 1,2,3,7,12 ... followups. Your help will be much appreciated.
                  Attached Files

                  Comment


                  • #10
                    Please read and act on https://www.statalist.org/forums/help#stata which explains why ,dta attachments are not a good idea.

                    Also, the question has nothing to do with the thread title. When that's true you should start a new thread.

                    Comment

                    Working...
                    X