Announcement

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

  • How to create new variable with month and year since May, 2011 as n°1

    Dear Statalister,

    I'm trying to create a new variable derived from one date (DateACR_num) which include month and date reformated as 1,2,3 from May 2011 (=1).

    I try:

    Code:
    gen date = dofm(DateACR_num)
    format date %tm
    gen wanted = date - ym(2011, 05)
    But results are no relevant

    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(DateACR_num wanted)
    18763 570470
    18763 570470
    18765 570531
    18765 570531
    18766 570562
    18766 570562
    18767 570592
    18767 570592
    18767 570592
    18767 570592
    18767 570592
    18767 570592
    18767 570592
    18768 570623
    18770 570683
    18771 570714
    18772 570744
    18773 570775


  • #2
    Thanks for the data example. Your DateACR_num is evidently a daily date variable. That being so, pushing it through dofm() makes no sense. dofm() is for converting monthly dates to daily dates. You want the other way round, I think.


    Code:
    clear
    input float(DateACR_num wanted)
    18763 570470
    18763 570470
    18765 570531
    18765 570531
    18766 570562
    18766 570562
    18767 570592
    18767 570592
    18767 570592
    18767 570592
    18767 570592
    18767 570592
    18767 570592
    18768 570623
    18770 570683
    18771 570714
    18772 570744
    18773 570775
    end 
    
    format %td DateACR_num 
    
    gen Wanted = mofd(DateACR_num) - ym(2011, 5) 
    
    list, sepby(Wanted) 
    
         +-----------------------------+
         | DateACR~m   wanted   Wanted |
         |-----------------------------|
      1. | 16may2011   570470        0 |
      2. | 16may2011   570470        0 |
      3. | 18may2011   570531        0 |
      4. | 18may2011   570531        0 |
      5. | 19may2011   570562        0 |
      6. | 19may2011   570562        0 |
      7. | 20may2011   570592        0 |
      8. | 20may2011   570592        0 |
      9. | 20may2011   570592        0 |
     10. | 20may2011   570592        0 |
     11. | 20may2011   570592        0 |
     12. | 20may2011   570592        0 |
     13. | 20may2011   570592        0 |
     14. | 21may2011   570623        0 |
     15. | 23may2011   570683        0 |
     16. | 24may2011   570714        0 |
     17. | 25may2011   570744        0 |
     18. | 26may2011   570775        0 |
         +-----------------------------+

    Comment


    • #3
      Code:
      gen date = dofm(DateACR_num)
      format DateACR_num %td
      gen wanted = mofd(DateACR_num) // measure time in months rather than days
      gen wanted = wanted - ym(2011, 05) // center at May 2011
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Thanks to both.

        It works well.

        Have a nice day.

        Comment

        Working...
        X