Announcement

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

  • How to convert a variable in the format "day, month, year" to another variable in the format "month, year"?

    Hello,

    I would like to convert a "date" variable in is in the format "day, month, year" to another variable that gives me the information in the "month, year" format. Can you help me with this issue?
    I send the code below.
    Thank you in advance.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float date str25 country float risk
    21915 "afghanistan"                   0
    21915 "albania"                       0
    21915 "algeria"                       0
    21915 "andorra"                       0
    21915 "angola"                        0
    21915 "argentina"                     0
    21915 "aruba"                         0
    21915 "australia"                     0
    21915 "austria"                       0
    21915 "azerbaijan"                    0
    21915 "bahamas"                       0
    21915 "bahrain"                       0
    21915 "bangladesh"                    0
    21915 "barbados"                      0
    21915 "belarus"                       0
    21915 "belgium"                       0
    21915 "belize"                        0
    21915 "benin"                         0
    21915 "bermuda"                       0
    21915 "bhutan"                        0
    21915 "bolivia"                   62.04
    21915 "bosniaandherzegovina"          0
    21915 "botswana"                   5.56
    21915 "brazil"                        0
    21915 "brunei"                        0
    21915 "bulgaria"                      0
    21915 "burkinafaso"                   0
    21915 "burundi"                       0
    21915 "cambodia"                      0
    21915 "cameroon"                      0
    21915 "canada"                        0
    21915 "capeverde"                     0
    21915 "centralafricanrepublic"        0
    21915 "chad"                          0
    21915 "chile"                         0
    21915 "china"                         0
    21915 "colombia"                      0
    21915 "comoros"                       .
    21915 "congo"                         0
    21915 "costarica"                     0
    21915 "cotedivoire"                   0
    21915 "croatia"                       0
    21915 "cuba"                          0
    21915 "cyprus"                        0
    21915 "czechrepublic"                 0
    21915 "democraticrepublicofcongo"     0
    21915 "denmark"                       0
    21915 "djibouti"                      0
    21915 "dominica"                      0
    21915 "dominicanrepublic"             0
    21915 "ecuador"                       0
    21915 "egypt"                         0
    21915 "elsalvador"                    0
    21915 "eritrea"                       0
    21915 "estonia"                       0
    21915 "eswatini"                      0
    21915 "ethiopia"                      0
    21915 "faeroeislands"                 0
    21915 "fiji"                          0
    21915 "finland"                       0
    21915 "france"                        0
    21915 "gabon"                         0
    21915 "gambia"                        0
    21915 "georgia"                       0
    21915 "germany"                       0
    21915 "ghana"                         0
    21915 "greece"                        0
    21915 "greenland"                     0
    21915 "guam"                          0
    21915 "guatemala"                     0
    21915 "guinea"                        0
    21915 "guyana"                        0
    21915 "haiti"                         0
    21915 "honduras"                      0
    21915 "hongkong"                  13.89
    21915 "hungary"                       0
    21915 "iceland"                       0
    21915 "india"                         0
    21915 "indonesia"                     0
    21915 "iran"                          0
    21915 "iraq"                          0
    21915 "ireland"                       0
    21915 "israel"                        0
    21915 "italy"                         0
    21915 "jamaica"                       0
    21915 "japan"                         0
    21915 "jordan"                        0
    21915 "kazakhstan"                    0
    21915 "kenya"                         0
    21915 "kiribati"                      0
    21915 "kosovo"                        0
    21915 "kuwait"                        0
    21915 "kyrgyzrepublic"                0
    21915 "laos"                          0
    21915 "latvia"                        0
    21915 "lebanon"                       0
    21915 "lesotho"                       0
    21915 "liberia"                       0
    21915 "libya"                         0
    21915 "lithuania"                     0
    end
    format %td date

  • #2
    Code:
    gen monthyear = mofd(date)
    format %tm monthyear
    
    . list in 1/5,noobs
      +-------------------------------------------+
      |      date       country   risk   monthy~r |
      |-------------------------------------------|
      | 01jan2020   afghanistan      0     2020m1 |
      | 01jan2020       albania      0     2020m1 |
      | 01jan2020       algeria      0     2020m1 |
      | 01jan2020       andorra      0     2020m1 |
      | 01jan2020        angola      0     2020m1 |
      +-------------------------------------------+

    Comment


    • #3
      I interpret the question as wanting a monthly date from a daily date, which you can do as

      Code:
      gen month = mofd(date)
      format month %tmMon-CCYY

      Edit: Crossed with #2

      Comment


      • #4
        It worked! Thank you very much for your help

        Comment

        Working...
        X