Announcement

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

  • How do I change dates in my data set from mm/dd/yy format to just including the years?

    I need to change the dates in my dataset from a month/day/year format to just a year. I can't figure out a way to do this without it being manual. Does anyone have any advice?

  • #2
    Please show us an example of the data you are working with. You can find instructions in the FAQ, directing you to use -dataex-. Potential solutions depend crucially on your dataset.

    In the meantime, you can answer whether you only want to change the display format to show only the year, that is, the underlying date information is still kept. Or, do you want a new variable containing only the year, and therefore losing the month and day information of the date?

    Comment


    • #3
      I found away to change my original string data to date data. Now i just need to edit that variable so it's only years. Basically, the new date data is in the format 14aug2003. I just need it to say 2003

      Comment


      • #4
        If you want to create a new year variable, then

        Code:
        gen year= year(datevar)
        If you just need to change the display format of the variable:

        Code:
        format datevar %tdCY
        where you substitute "datevar" with the name of your date variable. Note that changing the display format of the variable does not turn the variable into a year variable. So do not treat it as such.

        Comment


        • #5
          Thanks so much! How would I create a dummy variable with years consisting of 2005-2010?

          Comment


          • #6
            Code:
            gen wanted= inrange(year(datevar), 2005, 2010)
            where again you substitute "datevar" with the name of your date variable.
            Last edited by Andrew Musau; 21 Mar 2023, 14:19.

            Comment

            Working...
            X