Announcement

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

  • Extracting month and day from a variable

    Hi All, I have a date variable with observations recorded as "ddmmyyyy" (e.g. 07may2001). I want to drop the year in this variable OR keep the day and month (e.g. 07may). I couldn't find anything about this online and in the manual u24.pdf (stata.com). Any tips on how I can approach this? Thanks in advance.

  • #2
    Well, descriptions of data sets are usually insufficient to support help with code, and yours is a good example. From what you say, it's anybody's guess whether your variable "recorded as ddmmyyyy" is a string variable, or a numeric variable that reads just as ddmmyyyy without any datetime display format applied, or is a numeric variable that reads as ddmmyyyy after applying a datetime display format. Since the solution to your problem would differ depending on which this is, you'll need to post back showing your data in the helpful way that is recommended in the Forum FAQ (at #12). Specifically, use the -dataex- command.

    If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Thanks for the response! Here is a sample of the variable:
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float hosp_date_num
      20948
      20844
      20842
      20828
      20819
      21564
      18407
      18616
      18695
      18732
      18750
      18802
      18912
      18945
      18955
      19024
      19083
      19172
      19133
      19195
      19207
      19208
      19340
      19357
      19344
      19344
      19212
      18414
      18641
      18495
      18932
      18778
      19229
      18680
      18739
      18338
      18296
      18257
      18276
      18388
      18411
      18496
      18746
      18753
      18793
      18823
      18851
      18942
      15836
      15835
      15831
      15822
      15800
      15801
      15860
      15884
      16020
      16021
      16026
      16133
      16133
      16272
      16274
      16274
      16553
      16616
      16586
      16666
      16622
      16611
      16553
      16620
      16680
      16713
      16689
      16709
      16650
      16657
      16652
      16674
      16705
      16650
      16541
      16700
      16658
      16586
      16705
      16677
      16617
      16577
      16761
      16703
      16766
      16772
      16683
      16777
      16745
      16704
      16787
      16751
      end
      format %td hosp_date_num

      Comment


      • #4
        Extracting the day and month:
        Code:
        gen month = month(hosp_date_num)
        gen day = day(hosp_date_num)
        This gives you two separate variables, one for month and the other for day.

        If you want to combine those into a single string variable that looks like "m/d" you could run
        Code:
        egen month_day = concat(month day), punct("/")
        This gives you a genuine month_day single variable. It contains no year information at all. It is a string: you can't calculate anything with it, and you can't sort it into chronological order. But it has only the month and day as content so that the values corresponding to 3mar2024 and 3mar2005 would not only look the same, they would be equal if you were to test for equality.

        If, however, all you want is to not see the year, you can do this:
        Code:
        clonevar hosp_date_num2 = hosp_date_num
        format hosp_date_num2 %tdDDMon
        But be warned that the year is really still there--Stata is just hiding it at your request. So, with this approach, hosp_date_num2 will look like 3mar for both 3mar2024 and 3mar2005, but the two values would be unequal if you were to test them.

        Comment


        • #5
          Thanks Clyde! Is there a way that we can convert the variable from string to date? I want to use it with another date variable to calculate a change in time...

          Comment


          • #6
            There is no Stata date variable that contains day and month but not year. And it makes no sense to speak of calculating a change in time with such a variable if it did exist. Since 13mar2024 and 13mar2023 would be "the same" but for their year, change in time between 13mar and, say 25dec would differ depending on which year 13mar represented.

            Added: Even if we knew they referred to the same year, the change in time between 1feb and 1mar depends on which year is involved, as some years are leap years and others not.

            Comment

            Working...
            X