Announcement

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

  • YYYYMMDD to Stata format

    Hello,

    I read the Stata documentation on date and still have a question of conversion.
    I would like to convert YYYYMMDD to Stata %tm format.
    For example, '20150701' to '2015m7'.

    Thank you very much in advance!

  • #2
    Oh, I solved it by:

    Code:
    tostring var, replace
    gen date = date(var, "YMD")

    Comment


    • #3
      #2 is not a solution to #1 as daily dates are not monthly dates.

      Here are some other ways to do it.

      Code:
      . clear
      
      . set obs 1
      number of observations (_N) was 0, now 1
      
      . gen long var = 20150701
      
      . format var %8.0f
      
      . gen wanted1 = ym(floor(var/10000), floor(mod(var, 1000)/100))
      
      . gen wanted2 = mofd(daily(string(var, "%8.0f"), "YMD")) 
      
      . format wanted? %tm
      
      .  list
      
           +------------------------------+
           |      var   wanted1   wanted2 |
           |------------------------------|
        1. | 20150701    2015m7    2015m7 |
           +------------------------------+
      Code:
      
      
      .

      Comment


      • #4
        Oh Thank you Nick!! Always

        Comment


        • #5
          Just a followup question. If i wanted to conver "31jan2022" stored as %td to 20220131, how would i go about doing it?
          Last edited by Fahad Mirza; 02 Feb 2022, 01:38.

          Comment


          • #6
            Originally posted by Fahad Mirza View Post
            Just a followup question. If i wanted to conver "31jan2022" stored as %td to 20220131, how would i go about doing it?
            Do you mean convert it to a string variable or display the date as such? For the latter

            Code:
            di %tdCYND td(31jan2022)
            Res.:

            Code:
             di %tdCYND td(31jan2022)
            20220131
            For the former:

            Code:
            clear
            set obs 1
            gen date= td(31jan2022)
            gen wanted= string(year(date))+ cond(length(string(month(date)))==1, ///
            "0"+string(month(date)), string(month(date))) + cond(length(string(day(date)))==1, ///
            "0"+string(day(date)), string(day(date)))
            l
            Res.:

            Code:
            . gen wanted= string(year(date))+ cond(length(string(month(date)))==1, ///
            > "0"+string(month(date)), string(month(date))) + cond(length(string(day(date)))==1, ///
            > "0"+string(day(date)), string(day(date)))
            
            . l
            
                 +------------------+
                 |  date     wanted |
                 |------------------|
              1. | 22676   20220131 |
                 +------------------+
            Last edited by Andrew Musau; 02 Feb 2022, 03:08.

            Comment


            • #7
              Andrew Musau awesome! was looking for conversion to string. Should have mentioned more clearly. Thank you nonetheless for the detailed response!

              Comment


              • #8
                Consider also

                Code:
                gen wanted = string(year(date)) + string(month(date), "%02.0f") + string(day(date), "%02.0f")

                Comment


                • #9
                  You could also loop using display, Nick's code & #6 should be more efficient.

                  Code:
                  * Example generated by -dataex-. For more info, type help dataex
                  clear
                  input float date
                  22677
                  22678
                  22679
                  22680
                  22681
                  22682
                  22683
                  22684
                  22685
                  22686
                  end
                  format %td date
                  
                  gen wanted=""
                  forval i=1/`=_N'{
                       replace wanted= string(`:di %tdCYND date[`i']', "%8.0f") in `i'
                  }
                  Res.:

                  Code:
                  . l, sep(0)
                  
                       +----------------------+
                       |      date     wanted |
                       |----------------------|
                    1. | 01feb2022   20220201 |
                    2. | 02feb2022   20220202 |
                    3. | 03feb2022   20220203 |
                    4. | 04feb2022   20220204 |
                    5. | 05feb2022   20220205 |
                    6. | 06feb2022   20220206 |
                    7. | 07feb2022   20220207 |
                    8. | 08feb2022   20220208 |
                    9. | 09feb2022   20220209 |
                   10. | 10feb2022   20220210 |
                       +----------------------+

                  Comment

                  Working...
                  X