Announcement

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

  • Create month and year variable from format date: feb-75 mar-75....

    Hello evreyone,

    I need to create year and month variables like these:

    year month

    1975 1
    1975 2
    1975 3
    1975 4
    1975 5
    1975 6
    1975 7
    1975 8
    1975 9
    1975 10
    1975 11
    1975 12
    1976 1
    1976 2
    1976 3
    1976 4
    ....



    from the following date-variable

    feb-75
    mar-75
    apr-75
    mag-75
    giu-75
    lug-75
    ago-75
    set-75
    ott-75
    nov-75
    dic-75
    gen-76
    feb-76
    mar-76
    apr-76
    .......


    Can anyone help me?



  • #2
    It depends on whether your "date" variable is actually a Stata date variable (and, if so, which kind), or is a string variable that reads like a date to human eyes. This is a perfect example of why data examples should always be shown by using -dataex-. 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.

    Comment


    • #3
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int daten str18 OSshockBH
      5510 "-2.2917656363693"  
      5538 "-.288490430848404" 
      5569 ".311435598420019"  
      5599 ".99098860358297"   
      5630 "2.1856715744107"   
      5660 "3.36829251383065"  
      5691 "2.30033122563722"  
      5722 "3.8002382647714"   
      5752 "-9.54592687014987" 
      5783 "2.39836864956309"  
      5813 "1.74144449080349"  
      5844 "-1.23171105429079" 
      5875 "1.90686761272767"  
      5904 "1.8129746116395"   
      5935 "-.786257107888406" 
      5965 "-.350471284426156" 
      5996 "2.28427896194465"  
      6026 "2.05113478183425"  
      6057 "-.323113984547186" 
      6088 ".918258320883479"  
      6118 "3.83236303205339"  
      6149 "1.19965978531801"  
      6179 "2.4469718487251"   
      6210 "-8.2393763648536"  
      6241 "4.72525969340715"  
      6269 ".870576941122713"  
      6300 "-1.83305440315713" 
      6330 "-2.7112324122836"  
      6361 ".518512643694919"  
      6391 "-1.16484486418008" 
      6422 ".943452643930436"  
      6453 "1.98192092679608"  
      6483 ".4169719174999"    
      6514 "1.98210669590612"  
      6544 "1.95161729510135"  
      6575 "-7.9546753690539"  
      6606 "1.5232269657077"   
      6634 ".579265837181527"  
      6665 "1.08071769368751"  
      6695 "-2.0808963143209"  
      6726 "1.90230155684625"  
      6756 ".155651135706527"  
      6787 "-.0000480015397268"
      6818 "3.50164338414832"  
      6848 "1.40265734671561"  
      6879 ".384978728889617"  
      6909 "-2.09109562029141" 
      6940 "-2.78403858298231" 
      6971 ".735901355224359"  
      6999 ".895080817005614"  
      7030 ".804407762507091"  
      7060 ".5428621284926189" 
      7091 "-.460045086631502" 
      7121 ".733019964655695"  
      7152 ".0920497095565201" 
      7183 "-.656461314557291" 
      7213 ".870193778082176"  
      7244 ".50334245255982"   
      7274 "-.801193944572437" 
      7305 "-1.89501263046037" 
      7336 ".0990976053344764" 
      7365 "-2.02720995797755" 
      7396 "-1.93874988394092" 
      7426 "-.8239749439188691"
      7457 ".803016393984875"  
      7487 "-.87580213656173"  
      7518 "-.297795960257038" 
      7549 "-1.62611846600598" 
      7579 "-4.25359126817678" 
      7610 ".241182486078001"  
      7640 "1.31042967929519"  
      7671 "-.814746573429646" 
      7702 ".861906362593308"  
      7730 ".94411736722568"   
      7761 "-.377386360676566" 
      7791 "-.0462380649657607"
      7822 "-.125087104726594" 
      7852 "-1.49110356029184" 
      7883 "-1.78659974183937" 
      7914 ".299015644186533"  
      7944 ".516554953326204"  
      7975 "-1.5252065805954"  
      8005 "2.10446336519134"  
      8036 "1.00763692402313"  
      8067 "-.783098773311092" 
      8095 "-2.63561973896921" 
      8126 "-2.0361334911062"  
      8156 "1.94563488059827"  
      8187 "3.46711996700369"  
      8217 "-.0809312757408534"
      8248 "-.0226067280944257"
      8279 "-.0020319999769366"
      8309 "3.81472780656009"  
      8340 "3.10495860672668"  
      8370 "-.961141275472234" 
      8401 "-1.26238749680399" 
      8432 "-5.91106417860456" 
      8460 "2.37743283774131"  
      8491 "1.13755721226701"  
      8521 "2.11800710895874"  
      end
      format %tdMon-YY daten

      Comment


      • #4
        Here above the codes for datex

        Comment


        • #5
          Code:
          gen int year = yofd(daten)
          gen byte month = month(daten)
          I recommend you spend some time reading -help datetime- in detail. Stata has lots of useful datetime functions and features but it takes some understanding of how dates are represented internally. It will return dividends spending time to read this documentation.

          Comment


          • #6
            Although you got what you asked for, for most Stata purposes you really need something else, i.e. a monthly date variable.

            You evidently have monthly data indexed by a daily date that is the first of each month. Setting a display format to switch off display of the day doesn't change that date variable to a monthly date and to Stata, your dataset is still mostly holes, so that roughly 29/30 of the possible data are absent, namely not in the dataset.

            Here's enough of your data example to make the point.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input int daten
            5510
            5538
            5569
            5599
            5630
            5660
            5691
            5722
            5752
            5783
            end
            
            . format %tdMon-YY daten
            
            . list 
            
                 +--------+
                 |  daten |
                 |--------|
              1. | Feb-75 |
              2. | Mar-75 |
              3. | Apr-75 |
              4. | May-75 |
              5. | Jun-75 |
                 |--------|
              6. | Jul-75 |
              7. | Aug-75 |
              8. | Sep-75 |
              9. | Oct-75 |
             10. | Nov-75 |
                 +--------+
            
            . format %td daten 
            
            . gen mdate = mofd(daten)
            
            . format mdate %tm 
            
            . 
            . list 
            
                 +---------------------+
                 |     daten     mdate |
                 |---------------------|
              1. | 01feb1975    1975m2 |
              2. | 01mar1975    1975m3 |
              3. | 01apr1975    1975m4 |
              4. | 01may1975    1975m5 |
              5. | 01jun1975    1975m6 |
                 |---------------------|
              6. | 01jul1975    1975m7 |
              7. | 01aug1975    1975m8 |
              8. | 01sep1975    1975m9 |
              9. | 01oct1975   1975m10 |
             10. | 01nov1975   1975m11 |
                 +---------------------+
            Now you can tsset your dataset in terms of your new monthly date variable (if this isn't panel data) or tsset or xtset in terms of an identifier and a time variable (if it is).

            See also https://www.stata-journal.com/articl...article=dm0067

            Comment


            • #7
              Thank you all. Your codes work perfectly. I will elaborate on -datetime- and the issues you raised.

              Comment

              Working...
              X