Announcement

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

  • Date format using month and year

    Hi, I am trying to make a new variable called "date" using the month and year in my dataset. The variable "month" is a byte and the variable "year" is an int. I would like to make it into the format <year>m<month> and call this variable date. For example, month 01 (january) and year 2022 would become 2022m01. This variable should be a float. I'm unsure of how to convert these two variables into a single float variable with that format. Thanks.

  • #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.

    With that said, this example may start you in a useful direction.
    Code:
    . * Example generated by -dataex-. For more info, type help dataex
    . clear
    
    . input byte month int year
    
            month      year
      1. 1 2022
      2. end
    
    . generate date = ym(year, month)
    
    . format %tm date
    
    . list
    
         +-----------------------+
         | month   year     date |
         |-----------------------|
      1. |     1   2022   2022m1 |
         +-----------------------+
    
    . format %tmCCYY!mNN date
    
    . list
    
         +------------------------+
         | month   year      date |
         |------------------------|
      1. |     1   2022   2022m01 |
         +------------------------+

    Comment


    • #3
      May I see your dataset via dataex?

      Comment


      • #4
        Hi,

        I have a string variable monthyear, that is formatted as "Jan-15", "Feb-16" and so on. How can I convert it to a monthyear (or yearmonth) format in Stata? Here is an example of my dataset
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str37 bank str10 totalatms str13 num_cr_cards str18 transactionscr_atm str12(amountcr_atm num_dr_cards) str18 transactionsdr_atm str12 amountdr_atm str6 monthyear
        "BANK OF BARODA"       "7489"  "79317"   "2826"  "9.37"   "22620927"  "13522113"  "56155.3"   "Jan-15"
        "CANARA BANK"          "7845"  "108996"  "28762" "139.04" "23983462"  "17098046"  "50811.86"  "Jan-15"
        "PUNJAB NATIONAL BANK" "8186"  "141375"  "2847"  "12.48"  "35635652"  "19919295"  "87925.45"  "Jan-15"
        "UNION BANK OF INDIA"  "6631"  "77513"   "1061"  "4.74"   "17567747"  "13300420"  "42361.25"  "Jan-15"
        "STATE BANK OF INDIA"  "45732" "3058673" "64780" "282.25" "151116000" "252938382" "641663.95" "Jan-15"
        end

        Comment


        • #5
          See

          Code:
          help datetime
          from which this follows:

          Code:
          . gen mdate = monthly(monthyear, "M20Y")
          
          . format mdate %tm
          
          . 
          . list  monthyear mdate 
          
               +-------------------+
               | monthy~r    mdate |
               |-------------------|
            1. |   Jan-15   2015m1 |
            2. |   Jan-15   2015m1 |
            3. |   Jan-15   2015m1 |
            4. |   Jan-15   2015m1 |
            5. |   Jan-15   2015m1 |
               +-------------------+
          You will need extra code if any dates are 1999 or earlier.

          Comment


          • #6
            Thanks Nick Cox! It works perfectly since all years are post 2000

            Comment


            • #7
              hello, I have a question, I have a variable called date_in and it is a string that looks like this "01-02-2020 00:00:00", I need to keep only the date, but without the time, then I need to convert this to date format, so that I get 01FEB2020, I tried but I only get 2020m1 and I do not need this format, could you help me? please?

              Comment


              • #8
                =7 Please show the code you used. Here is some technique. For reasons and choices see
                Code:
                help datetime
                and help datetime display formats

                Code:
                clear
                input str20 date_in
                "01-02-2020 00:00:00"
                end 
                
                gen wanted = daily(word(date_in, 1), "DMY")
                format wanted %td
                If your dates really are the first day of each month as a daily date, you would be better off with monthly dates.

                Comment


                • #9
                  Hi: My data set has 15-Jan, 15-Feb as data representing year 2015 and then the month, I want to convert it in a format so that I can generate some calculations around difference between two dates. What should be the command to modify the date? Many thanks.

                  Comment


                  • #10
                    Your question cannot be answered based on the information provided. It is impossible to tell from a description in words whether your 15-Jan is from a string variable, or from a formatted Stata internal format date variable. And if it is the latter, we cannot tell whether it is a daily or monthly date. In order to get an answer your question, please post back with example data from your data set using the -dataex- command to do so. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

                    I also recommend that as a new user of this Forum you take time to read the Forum FAQ for excellent advice on how to get the most out of your Statalist experience. The advice about showing example data and using -dataex- for that purpose are among the many helpful tips there.

                    Comment


                    • #11
                      Another doubt, my data has an indicator on financial incentives given to facilities for hospital delivery (called intervention=1 when the scheme was launched for that facility, 0=prior to incentive commencement). Incentive start date is given in year-month format so is incentive end-date. Now, I want to draw a line plot to see how the facility delivery indicator moves one year after the intervention commencement, between 1-2 years etc and prior to intervention commencement. Note my data is monthly so if the intervention start date is Jan 2015, I need to plot the total deliveries between Jan 2015 and Jan 2016; and Jan 2016-Jan 2017 etc. For each facility this intervention commencement date is different. X axis should have years after the intervention commencement and years prior to the commencement. The data is for all the years and months from 2015-2021.

                      Comment


                      • #12
                        Thanks Clyde Schechter
                        Does the below help?
                        Code:
                        * Example generated by -dataex-. For more info, type help dataex
                        clear
                        input str6(rbf_start rbf_end)
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        "19-May" "22-Oct"
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        "18-Nov" "22-Oct"
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        "18-Jul" "22-Jun"
                        "17-Dec" "22-Nov"
                        ""       ""      
                        ""       ""      
                        "17-Dec" "22-Nov"
                        "18-Oct" "22-Sep"
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        "21-Jun" "22-Nov"
                        ""       ""      
                        "15-Dec" "22-Jul"
                        ""       ""      
                        "21-Dec" "22-Nov"
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        "18-Nov" "22-Oct"
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        "21-Jul" "22-Dec"
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        "18-Jun" "22-Jun"
                        ""       ""      
                        "21-Jul" "22-Dec"
                        "21-Jul" "22-Dec"
                        "21-Jul" "22-Dec"
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        "21-Jul" "22-Dec"
                        "21-Jul" "22-Dec"
                        "19-Jan" "22-Jun"
                        "21-Jul" "22-Dec"
                        "19-May" "22-Oct"
                        ""       ""      
                        "19-Dec" "22-Nov"
                        "19-Mar" "22-Aug"
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        "19-Feb" "22-Jul"
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        "21-Jul" "22-Dec"
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        "21-Jul" "22-Dec"
                        ""       ""      
                        "19-Feb" "22-Jul"
                        ""       ""      
                        "19-May" "22-Oct"
                        "19-Dec" "22-Nov"
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        ""       ""      
                        end
                        The numbers are years like 19 is 2019 and 21 is 2021. Data is monthly.
                        Last edited by Chaudhary Navi; 18 Jan 2024, 15:01.

                        Comment


                        • #13
                          Here is some technique:

                          Code:
                          * Example generated by -dataex-. For more info, type help dataex
                          clear
                          input str6(rbf_start rbf_end)
                          "19-May" "22-Oct"
                          "18-Nov" "22-Oct"
                          "18-Jul" "22-Jun"
                          "17-Dec" "22-Nov"
                          "17-Dec" "22-Nov"
                          "18-Oct" "22-Sep"
                          "21-Jun" "22-Nov"
                          "15-Dec" "22-Jul"
                          "21-Dec" "22-Nov"
                          "18-Nov" "22-Oct"
                          "21-Jul" "22-Dec"
                          "18-Jun" "22-Jun"
                          "21-Jul" "22-Dec"
                          "21-Jul" "22-Dec"
                          "21-Jul" "22-Dec"
                          "21-Jul" "22-Dec"
                          "21-Jul" "22-Dec"
                          "19-Jan" "22-Jun"
                          "21-Jul" "22-Dec"
                          "19-May" "22-Oct"
                          "19-Dec" "22-Nov"
                          "19-Mar" "22-Aug"
                          "19-Feb" "22-Jul"
                          "21-Jul" "22-Dec"
                          "21-Jul" "22-Dec"
                          "19-Feb" "22-Jul"
                          "19-May" "22-Oct"
                          "19-Dec" "22-Nov"
                          end
                          
                          gen sdate = monthly(rbf_start, "20YM")
                          gen edate = monthly(rbf_end, "20Ym")
                          format *date %tm 
                          
                          gen diff = edate - sdate 
                          
                          sort sdate edate 
                          
                          list 
                          
                               +-----------------------------------------------+
                               | rbf_st~t   rbf_end     sdate     edate   diff |
                               |-----------------------------------------------|
                            1. |   15-Dec    22-Jul   2015m12    2022m7     79 |
                            2. |   17-Dec    22-Nov   2017m12   2022m11     59 |
                            3. |   17-Dec    22-Nov   2017m12   2022m11     59 |
                            4. |   18-Jun    22-Jun    2018m6    2022m6     48 |
                            5. |   18-Jul    22-Jun    2018m7    2022m6     47 |
                               |-----------------------------------------------|
                            6. |   18-Oct    22-Sep   2018m10    2022m9     47 |
                            7. |   18-Nov    22-Oct   2018m11   2022m10     47 |
                            8. |   18-Nov    22-Oct   2018m11   2022m10     47 |
                            9. |   19-Jan    22-Jun    2019m1    2022m6     41 |
                           10. |   19-Feb    22-Jul    2019m2    2022m7     41 |
                               |-----------------------------------------------|
                           11. |   19-Feb    22-Jul    2019m2    2022m7     41 |
                           12. |   19-Mar    22-Aug    2019m3    2022m8     41 |
                           13. |   19-May    22-Oct    2019m5   2022m10     41 |
                           14. |   19-May    22-Oct    2019m5   2022m10     41 |
                           15. |   19-May    22-Oct    2019m5   2022m10     41 |
                               |-----------------------------------------------|
                           16. |   19-Dec    22-Nov   2019m12   2022m11     35 |
                           17. |   19-Dec    22-Nov   2019m12   2022m11     35 |
                           18. |   21-Jun    22-Nov    2021m6   2022m11     17 |
                           19. |   21-Jul    22-Dec    2021m7   2022m12     17 |
                           20. |   21-Jul    22-Dec    2021m7   2022m12     17 |
                               |-----------------------------------------------|
                           21. |   21-Jul    22-Dec    2021m7   2022m12     17 |
                           22. |   21-Jul    22-Dec    2021m7   2022m12     17 |
                           23. |   21-Jul    22-Dec    2021m7   2022m12     17 |
                           24. |   21-Jul    22-Dec    2021m7   2022m12     17 |
                           25. |   21-Jul    22-Dec    2021m7   2022m12     17 |
                               |-----------------------------------------------|
                           26. |   21-Jul    22-Dec    2021m7   2022m12     17 |
                           27. |   21-Jul    22-Dec    2021m7   2022m12     17 |
                           28. |   21-Dec    22-Nov   2021m12   2022m11     11 |
                               +-----------------------------------------------+

                          help datetime and its links I think covers everything needed,

                          Comment


                          • #14
                            Thanks Nick this worked perfectly. On my second doubt data has an indicator on financial incentives given to facilities for hospital delivery (called rbf_fin=1 when the incentive was launched for that facility, 0=prior to incentive commencement). Incentive start date is given in year-month format & so is incentive end-date. Now, I want to draw a line plot to see how the facility delivery indicator moves one year after the intervention commencement, between 1-2 years etc and prior to intervention commencement. Note my data is monthly so if the intervention start date is Jan 2015, I need to plot the total deliveries between Jan 2015 and Jan 2016; and Jan 2016-Jan 2017 etc. For each facility this intervention commencement date is different. X axis should have years after the intervention commencement and years prior to the commencement. The data is for all the years and months from 2014-2022. Sharing the kind of figure I want to replicate as well as my data. Thanks in advance for your time on this.

                            Code:
                            * Example generated by -dataex-. For more info, type help dataex
                            clear
                            input str37 facility float(sdate edate rbf_fin) int del_facil str6 month
                            "Kubolong SC"                             .   . 0   0 "Jul-17"
                            "Longpang PHC"                            .   . 0   2 "Dec-21"
                            "Khuwoboto SC"                            .   . 0   0 "Oct-14"
                            "Medziphema CHC"                        712 753 1  14 "Sep-21"
                            "Rukizu SC"                               .   . 0   0 "Mar-16"
                            "Yaong Village SC"                        .   . 0   . "Jul-19"
                            "Suruhuto PHC"                            .   . 0   . "Mar-18"
                            "Vidima SC"                               .   . 0   . "Dec-19"
                            "Waromong SC"                             .   . 0   0 "Dec-21"
                            "C Chingnyu SC"                           .   . 0   . "Jan-15"
                            "Kedima SC"                               .   . 0   0 "Jan-18"
                            "Heningkinglwa PHC"                       .   . 0   . "Nov-17"
                            "L.Longidang SC"                        706 753 1   . "Dec-20"
                            "Noksen Village SC"                       .   . 0   . "Jul-15"
                            "Langnok SC"                              .   . 0   0 "Jul-21"
                            "Khuza PHC"                               .   . 0   0 "Mar-15"
                            "Burma Camp UPHC"                         .   . 0   0 "Apr-21"
                            "Totok Chingha SC"                        .   . 0   . "Jul-20"
                            "Nokyan Village SC"                       .   . 0   0 "May-21"
                            "Khanimo SC"                            702 749 0   0 "Apr-17"
                            "Meluri Notional SC"                    695 754 1   . "Oct-21"
                            "Waromong SC"                             .   . 0   1 "Feb-21"
                            "Tsurang Valley SC"                       .   . 0   0 "Jul-21"
                            "Sathakha PHC"                          695 754 0   4 "Oct-14"
                            "Pfutsero Notional SC"                  705 752 0   0 "Apr-14"
                            "Piphema PHC"                             .   . 0   0 "Oct-21"
                            "Keyieke SC"                              .   . 0   . "Sep-18"
                            "Chungliyimsen BD"                        .   . 0   . "Feb-20"
                            "Litsami SC"                              .   . 0   . "Jun-19"
                            "CMO Office"                            737 754 0   . "Feb-19"
                            "Dzuleke SC"                              .   . 0   . "Nov-19"
                            "Noklak Notional SC"                    671 750 0   . "Oct-15"
                            "Tsiepama SC"                             .   . 0   0 "Feb-18"
                            "Buranamsang SC"                        743 754 0   1 "Apr-21"
                            "Litsami SC"                              .   . 0   0 "Jan-18"
                            "Chaoha Chingnyu SC"                      .   . 0   0 "Sep-21"
                            "Puniboto SC"                             .   . 0   0 "Mar-18"
                            "Yongnyah PHC"                          706 753 1   2 "Sep-20"
                            "Sematilla SC"                            .   . 0   . "Nov-19"
                            "Longsa PHC"                              .   . 0   0 "Jan-15"
                            "Yonchocho SC"                            .   . 0   . "Apr-15"
                            "Old Peren SC"                            .   . 0   . "Jan-20"
                            "Peren District Hospital"               738 755 0   5 "Apr-14"
                            "Xuviye SC"                               .   . 0   . "Apr-20"
                            "Rukizu SC"                               .   . 0   . "May-19"
                            "Sc. College SC"                          .   . 0   0 "Aug-20"
                            "Mission Centre SC"                     701 749 1   0 "Mar-22"
                            "Meluri Village SC"                       .   . 0   . "Dec-20"
                            "Tuensang  District Hospital"           738 755 0  44 "May-15"
                            "Dimapur District Hospital"             738 755 0 478 "Sep-15"
                            "Imkongliba Memorial District Hospital" 738 755 0  37 "Jul-14"
                            "Sheipu SC"                               .   . 0   . "Jan-22"
                            "Phek DH"                                 .   . 0  21 "Dec-14"
                            "Purana Bazar SC"                         .   . 0   0 "May-14"
                            "Mon District Hospital"                 738 755 0  55 "Oct-20"
                            "CMO Office"                            738 755 0   . "Oct-20"
                            "Chipur SC"                             708 749 1   . "Jul-19"
                            "Mon District Hospital"                 738 755 0  53 "Apr-16"
                            "Kuhoboto PHC"                          712 753 1   4 "Jul-20"
                            "Zaoanger SC"                             .   . 0   0 "Sep-17"
                            "Santak SC"                             719 754 1   0 "Jan-22"
                            "Viswema CHC Notional SC"               710 751 1   . "Oct-20"
                            "Yimyu UPHC"                              .   . 0   . "Feb-18"
                            "Chungliyimsen BD"                        .   . 0   . "Apr-22"
                            "Phuyetomi SC"                            .   . 0   0 "Dec-15"
                            "Chingkhao PHC"                           .   . 0   . "Nov-18"
                            "Tseminyu New town SC"                    .   . 0   . "May-18"
                            "Yingshikiur SC"                          .   . 0   . "Jul-18"
                            "Lengnyu SC"                              .   . 0   . "Jun-21"
                            "Longphayimsen SC"                      709 750 1   . "Mar-19"
                            "Lozaphehu PHC"                           .   . 0   0 "May-16"
                            "Khuwoboto SC"                            .   . 0   0 "Aug-15"
                            "Phekherkiema SC"                         .   . 0   . "Jan-19"
                            "Longsa PHC"                              .   . 0   . "Sep-19"
                            "Chingkao PHC"                            .   . 0   0 "Apr-14"
                            "Longkhim Notional SC"                  738 755 0   . "Sep-19"
                            "Chaoha Chingnyu SC"                      .   . 0   . "Nov-20"
                            "Noksen Village SC"                       .   . 0   . "Nov-14"
                            "Ralan SC"                                .   . 0   . "Dec-15"
                            "Mangakhi SHC"                            .   . 0   . "May-15"
                            "Dzuleke SC"                              .   . 0   . "Jan-20"
                            "Langkei SC"                              .   . 0   . "Sep-15"
                            "Kubolong SC"                             .   . 0   0 "Aug-14"
                            "Hangphoi SC"                             .   . 0   . "Jun-18"
                            "Khoio SC"                                .   . 0   . "May-14"
                            "Wakching CHC"                            .   . 0   3 "Dec-21"
                            "Yoruba PHC"                              .   . 0   1 "Aug-21"
                            "Peren District Hospital"               738 755 1   . "Sep-21"
                            "Touphema PHC"                            .   . 0   . "Jan-20"
                            "Tuli CHC"                              709 750 0   6 "Oct-16"
                            "T Chingnyu SC"                           .   . 0   . "Jun-19"
                            "Medziphema CHC"                        712 753 0  14 "Apr-14"
                            "Santak SC"                             719 754 0   . "Jan-19"
                            "Leshemi SC"                              .   . 0   . "Sep-20"
                            "Zuikhu PHC"                              .   . 0   . "Dec-21"
                            "Duncan UPHC"                             .   . 0   . "Jul-21"
                            "Midland Urban Health Centre"             .   . 0   0 "May-15"
                            "Chandmari SC"                            .   . 0   . "Aug-20"
                            "Asukiqha SC"                             .   . 0   0 "Oct-15"
                            "Yoruba PHC"                              .   . 0   0 "Jan-17"
                            end
                            format %tm sdate
                            format %tm edate
                            Click image for larger version

Name:	cg-graph.png
Views:	1
Size:	33.3 KB
ID:	1740358

                            Comment


                            • #15
                              That's a new problem and quite a big one. You have already been given the technique to calculate monthly dates, which will give you a good start.

                              Comment

                              Working...
                              X