Announcement

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

  • Add a number to a numeric variable

    Please advise how to add "01" which is fixed day at the end of date variable "episode_stmmyy" which is currently in numeric format with "yyyymm" in it.

    Example generated by -dateex-
    Clear
    Input long episode_stmmyy
    200804
    200901
    201003
    201501
    202006

  • #2
    Date variables stored as numbers as you show are not useful in Stata. For example, while the difference between your montly dates for December 2008 and January 2009 is 1 month, the difference between 200812 and 200901 is 89. Constructing daily dates from these monthly dates will have the same problem.

    Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

    All Stata manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

    Comment


    • #3
      Short answer is to multiply the current variable by 100, and then add 1.

      A longer answer is please also use date() command to change that into a date that Stata can understand. Here are some reference codes:

      Code:
      clear
      input long episode_stmmyy
      200804
      200901
      201003
      201501
      202006
      end
      
      * Add trailing 01
      replace episode_stmmyy = episode_stmmyy * 100 + 1
      * Convert to string for converting to date
      tostring episode_stmmyy, replace
      
      * Generate a date variable that Stata recognizes
      gen date = date(episode_stmmyy, "YMD")
      * Properly format it
      format date %td
      
      * Display the data
      list
      Results:
      Code:
           +----------------------+
           | episod~y        date |
           |----------------------|
        1. | 20080401   01apr2008 |
        2. | 20090101   01jan2009 |
        3. | 20100301   01mar2010 |
        4. | 20150101   01jan2015 |
        5. | 20200601   01jun2020 |
           +----------------------+
      Also, please take the advice in #2 by heart.

      Comment


      • #4
        Thanks so much very useful advice, I never thought of this trick of multiplying by 100.

        Comment


        • #5
          Much good advice here, and the problem is solved, but more can be said.

          Note that date() is a function, not a command.

          Note that the example in #1, although helpful, is not as provided by dataex as Clear and Input are not Stata commands.

          Stata isn't especially smart about "run-together" monthly dates. Another solution would be to work with numeric components as in

          .
          Code:
           gen wanted_m = ym(floor(episode / 100), mod(episode, 100))
          
          . gen wanted_d = mdy(mod(episode, 100), 1, floor(episode / 100))
          and then assign appropriate display formats.

          Yet another solution would use the numdate command from SSC as introduced in https://www.statalist.org/forums/for...date-variables and as exemplified here:

          Code:
          clear
          input long episode_stmmyy
          200804
          200901
          201003
          201501
          202006
          end 
          
          numdate monthly mdate = episode_stmmyy, pattern(YM)
          numdate daily ddate = episode_stmmyy, pattern(YM)
          
          list 
          
               +-------------------------------+
               | episod~y    mdate       ddate |
               |-------------------------------|
            1. |   200804   2008m4   01apr2008 |
            2. |   200901   2009m1   01jan2009 |
            3. |   201003   2010m3   01mar2010 |
            4. |   201501   2015m1   01jan2015 |
            5. |   202006   2020m6   01jun2020 |
               +-------------------------------+
          numdate assigns default display formats, as implied by those results.

          Comment

          Working...
          X