Announcement

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

  • Changing date format

    Dear Statalist,

    I'm trying to change the format of my date variables which is currently a string variable (eg., "4/24/2018") to date format.
    I tried the following code
    Code:
    format %tdCYND announcement_open_date
    and get the message "string %fmt required for string variables."

    Can anyone help me out with this?
    Here's my data example.
    Thanks.

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str10(announcement_open_date announcement_close_date)
    "4/23/2018"  "5/2/2018"  
    "11/29/2017" "12/6/2017" 
    "5/21/2018"  "6/5/2018"  
    "4/9/2018"   "4/16/2018" 
    "2/5/2018"   "2/20/2018" 
    "7/31/2018"  "8/13/2018" 
    "3/5/2018"   "3/23/2018" 
    "1/17/2018"  "12/30/2018"
    "1/12/2018"  "1/23/2018" 
    "4/27/2018"  "5/3/2018"  
    "10/12/2017" "10/18/2017"
    "1/8/2018"   "1/17/2018" 
    "5/7/2018"   "5/11/2018" 
    "10/24/2017" "11/2/2017" 
    "9/4/2018"   "9/10/2018" 
    "2/28/2018"  "3/14/2018" 
    "3/22/2018"  "3/28/2018" 
    "8/15/2018"  "12/31/2018"
    "7/10/2018"  "7/14/2018" 
    "8/21/2018"  "8/31/2018" 
    "7/31/2018"  "8/20/2018" 
    "2/1/2018"   "2/5/2018"  
    "2/12/2018"  "2/16/2018" 
    "3/8/2018"   "3/12/2018" 
    "1/12/2018"  "1/18/2018" 
    "6/11/2018"  "6/15/2018" 
    "9/18/2018"  "10/9/2018" 
    "8/27/2018"  "9/10/2018" 
    "5/11/2018"  "5/18/2018" 
    "2/14/2018"  "4/11/2018" 
    "8/1/2018"   "8/20/2018" 
    "2/15/2018"  "6/29/2018" 
    "1/2/2018"   "1/22/2018" 
    "12/1/2017"  "1/2/2018"  
    "2/13/2018"  "2/27/2018" 
    "4/3/2018"   "4/10/2018" 
    "4/16/2018"  "4/30/2018" 
    "9/8/2018"   "9/17/2018" 
    "3/7/2018"   "3/13/2018" 
    "6/22/2018"  "6/28/2018" 
    "11/8/2017"  "11/17/2017"
    "9/11/2018"  "9/14/2018" 
    "8/24/2018"  "9/5/2018"  
    "9/4/2018"   "9/17/2018" 
    "3/21/2018"  "3/30/2018" 
    "2/9/2018"   "2/28/2018" 
    "9/11/2018"  "9/18/2018" 
    "9/19/2018"  "10/3/2018" 
    "5/11/2018"  "5/21/2018" 
    "12/8/2017"  "12/31/2018"
    "9/18/2018"  "9/24/2018" 
    "4/14/2018"  "4/23/2018" 
    "4/11/2018"  "4/16/2018" 
    "4/26/2018"  "5/9/2018"  
    "8/8/2018"   "8/14/2018" 
    "11/16/2017" "12/8/2017" 
    "12/15/2017" "12/21/2017"
    "2/1/2018"   "2/14/2018" 
    "4/16/2018"  "4/20/2018" 
    "2/3/2018"   "2/16/2018" 
    "1/12/2018"  "1/19/2018" 
    "1/26/2018"  "2/2/2018"  
    "9/22/2018"  "10/9/2018" 
    "7/26/2018"  "8/1/2018"  
    "12/1/2017"  "12/11/2017"
    "10/16/2017" "10/26/2017"
    "1/12/2018"  "1/19/2018" 
    "5/10/2018"  "5/16/2018" 
    "5/18/2018"  "5/30/2018" 
    "1/24/2018"  "2/9/2018"  
    "6/14/2018"  "7/5/2018"  
    "5/1/2018"   "5/10/2018" 
    "3/28/2018"  "4/17/2018" 
    "5/14/2018"  "5/18/2018" 
    "7/13/2018"  "8/2/2018"  
    "10/5/2017"  "10/11/2017"
    "7/5/2018"   "7/16/2018" 
    "8/7/2018"   "8/20/2018" 
    "6/11/2018"  "6/15/2018" 
    "10/10/2017" "10/16/2017"
    "2/8/2018"   "2/14/2018" 
    "3/8/2018"   "3/14/2018" 
    "9/18/2018"  "9/18/2018" 
    "10/19/2017" "10/30/2017"
    "9/20/2018"  "9/26/2018" 
    "6/27/2018"  "7/9/2018"  
    "9/14/2018"  "9/25/2018" 
    "4/11/2018"  "4/13/2018" 
    "2/15/2018"  "2/20/2018" 
    "10/20/2017" "11/2/2017" 
    "9/19/2018"  "9/26/2018" 
    "5/18/2018"  "5/24/2018" 
    "4/10/2018"  "4/23/2018" 
    "7/6/2018"   "7/13/2018" 
    "9/4/2018"   "9/10/2018" 
    "4/25/2018"  "5/8/2018"  
    "10/23/2017" "11/7/2017" 
    "2/1/2018"   "2/28/2018" 
    "4/3/2018"   "4/10/2018" 
    "4/25/2018"  "5/1/2018"  
    end
    ------------------ copy up to and including the previous line ------------------


  • #2
    You've skipped a step -- before changing the display format, you need to actually convert the strings to numeric SIF dates, like so:

    Code:
    foreach v of varlist *date{
        generate new_`v' = date(`v',"MDY")
        format %tdCYND new_`v'
    }
    Note: the code above loops over both of your date variables for efficiency's sake. The important part is within the loop delimiters.
    Note 2: I strongly recommend reading through the documentation for Stata's datetime functionalities for future work with dates.
    Last edited by Ali Atia; 07 Sep 2022, 20:30.

    Comment


    • #3
      Expanding on the recommendation from Ali Atia in post #2, 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


      • #4
        Thanks!

        Comment

        Working...
        X