Announcement

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

  • Uploading new data sets

    Date Crude Oil Production, Norway Crude Oil Production, World Global Real Economic Activity Based on Dry Bulk Cargo Bulk Freight Rates
    (Thousand Barrels per Day) (Thousand Barrels per Day) (Index deflated with US CPI and Linearly Detrended)
    1980 January 531.777 62348.011 3.49E+01
    1980 February 570.418 62715.757 4.03E+01


    The date goes up to 2016 December.


    Adding New Variables and Transforming them from monthly to quarterly data

    The Excel Data Set that I inserted above contain three new variables that I would like to add into my VAR regression on top of what I have in Stata at the moment. However, the data that I just obtained is monthly data but I would like to add them along with my current variables (essentially merging my data) and transforming them into quarterly data by taking the average of the period (eg. average of jan to march).


    List of Variables that is already in Stata

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 date double exc long rgdp double(unp inf op)
    "Q1 1980" 4.94676666566667 339021              1.5  7.74278215223094            35.58
    "Q2 1980"      4.942499999 324697              1.4  10.2464332036316            35.45
    "Q3 1980" 4.84123333233333 315430              2.1   12.291933418694 32.7066666666667
    "Q4 1980"      5.026399999 343069              1.6  13.1147540983606            39.09
    "Q1 1981"      5.350099999 339189              1.8  14.7381242387332 37.2933333333333
    "Q2 1981" 5.68363333233333 331605                2                14            33.54
    "Q3 1981" 6.08516666566667 326427              2.4  13.5689851767389 31.7966666666667
    "Q4 1981" 5.83913333233333 346128              1.8  12.3745819397994 33.5233333333333
    "Q1 1982" 5.95133333233333 339288                2  11.8895966029724 30.6733333333333
    "Q2 1982"      6.077099999 330830              2.4  11.0423116615067 32.1066666666667
    "Q3 1982"      6.641599999 327615              2.8  10.8433734939759 31.8066666666667
    "Q4 1982"      7.146099999 348779              3.2  11.6071428571428            31.59
    "Q1 1983" 7.10993333233333 345516              3.6  9.77229601518024            29.16
    "Q2 1983" 7.16973333233333 347946              3.7   9.1078066914498            29.48
    "Q3 1983" 7.40316666566667 341769              3.8  7.78985507246374 30.2966666666667
    "Q4 1983" 7.50263333233333 364775              2.6  7.28888888888891            28.94
    "Q1 1984" 7.68896666566667 368229              3.2  6.30942091616251 29.2333333333333
    "Q2 1984" 7.74113333266667 360828              3.3  6.47359454855195 29.3133333333333
    "Q3 1984" 8.36613333266667 368434              3.6  6.13445378151261 28.1466666666667
    "Q4 1984" 8.84959999933333 387247              2.5  5.96520298260146            27.49
    "Q1 1985" 9.36676666666667 391743                3  5.69105691056911 27.3666666666667
    "Q2 1985"      8.893666666 377860              2.3  5.75999999999999            26.95
    "Q3 1985"      8.342333333 385100              3.1  5.77988915281077 27.0933333333333
    "Q4 1985" 7.78616666666667 412488                2  5.62939796716187 28.0733333333333
    "Q1 1986"           7.3197 401783                2  5.76923076923076            17.16
    "Q2 1986" 7.40296666666667 399497              1.9  6.05143721633889 12.4433333333333
    "Q3 1986"           7.3868 399205              2.3  8.00898203592812 12.4433333333333
    "Q4 1986"           7.4695 430057              1.7  8.80829015544041            14.64
    "Q1 1987" 7.03546666666667 412198              2.4  9.96363636363637 17.7066666666667
    "Q2 1987"           6.7112 413613              1.8  9.70042796005709            18.39
    "Q3 1987"           6.7302 397776              2.2  7.90020790020794            18.94
    "Q4 1987" 6.47293333333333 435545              1.9  7.41496598639454 17.7566666666667
    "Q1 1988"            6.365 419283              2.5  7.07671957671957 15.7533333333333
    "Q2 1988"            6.256 404907              2.9  7.02210663198959            16.09
    "Q3 1988"           6.8359 401254              3.4  6.61528580603723 14.0866666666667
    "Q4 1988" 6.61103333333333 429452              3.9  6.01646611779607 13.1466666666667
    "Q1 1989" 6.72326666666667 408393              4.8  4.69425571340333 17.0233333333333
    "Q2 1989" 7.00846666666667 417709 4.76666666666667  4.67800729040098            18.47
    "Q3 1989"           7.0385 407719 5.03333333333333  4.51807228915663            17.41
    "Q4 1989" 6.84776666666667 438257 4.93333333333333  4.30107526881723            18.72
    "Q1 1990" 6.53356666666667 427717 5.66666666666667  4.36578171091446 19.4633333333333
    "Q2 1990"           6.4893 416785 5.16666666666667  3.83052814857803            15.82
    "Q3 1990"            6.154 412074              5.2  3.80403458213255 25.6566666666667
    "Q4 1990"           5.8621 447813 4.83333333333333    4.524627720504               31
    "Q1 1991"           5.9725 433742              5.6  3.90050876201245            19.85
    "Q2 1991" 6.75420333333333 439794 5.23333333333333  3.80100614868644            18.33
    "Q3 1991" 6.81336666666667 427301 5.46666666666667  3.49805663520268 19.4233333333333
    "Q4 1991"           6.3917 456129              5.5  2.57534246575342 19.8666666666667
    "Q1 1992" 6.35493333333333 457330              6.1  2.33949945593033 17.5066666666667
    "Q2 1992"           6.3128 443666 5.96666666666667   2.4232633279483 19.6833333333333
    "Q3 1992" 5.79393333333333 447403                6  2.30686695278971 19.9966666666667
    "Q4 1992" 6.39633666666667 471367 5.63333333333333  2.24358974358973 18.9566666666667
    "Q1 1993"           6.9538 457344 6.26666666666667  2.60499734183944            17.99
    "Q2 1993" 6.84676666666667 451547              6.1  2.41850683491062 17.9933333333333
    "Q3 1993" 7.25126666666667 458853              6.1  2.14997378080752             16.2
    "Q4 1993" 7.32468333333333 503800 5.33333333333333  1.98537095088822 14.9633333333333
    "Q1 1994" 7.44243666666667 482274 5.63333333333333   1.2435233160622            13.87
    "Q2 1994" 7.20914333333333 486378 5.66666666666667  .975359342915802            16.27
    "Q3 1994" 6.84432666666667 474431 5.36666666666667  1.54004106776183            17.05
    "Q4 1994"          6.73443 523076 4.96666666666667  1.74180327868853 16.6033333333333
    "Q1 1995" 6.52158333333333 508034 5.53333333333333  2.66120777891503 17.2233333333333
    "Q2 1995" 6.23681333333333 492214              5.2  2.69445856634468 18.1766666666667
    "Q3 1995" 6.30330666666667 505191              4.8   2.3255813953488            16.44
    "Q4 1995" 6.27892333333333 542393              4.2  2.16515609264855 16.9766666666667
    "Q1 1996" 6.41570333333333 536283                5  .897308075772663 18.3133333333333
    "Q2 1996" 6.53549666666667 520406                5  .990099009900991 19.4866666666667
    "Q3 1996"          6.41397 536030 4.76666666666667  1.38339920948619            20.63
    "Q4 1996" 6.43406333333333 558078 4.23333333333333  1.77427304090683 23.0633333333333
    "Q1 1997"          6.62962 548270 4.26666666666667  3.06324110671939 21.0833333333333
    "Q2 1997" 7.07691666666667 566972 4.23333333333333  2.69607843137256            18.49
    "Q3 1997" 7.46047666666667 552684 3.96666666666667  2.29044834307992 18.6566666666667
    "Q4 1997"          7.12659 596533              3.2  2.22760290556901            18.84
    "Q1 1998" 7.53980666666667 588947              3.3  2.15723873441995 14.1633333333333
    "Q2 1998" 7.51393333333333 573271 3.46666666666667   2.2434367541766            13.28
    "Q3 1998" 7.63745333333333 561890              3.1  2.28680323963794 13.0033333333333
    "Q4 1998" 7.48919666666667 599781              2.5  2.32117479867363            11.85
    "Q1 1999" 7.66309333333333 587707              2.8  2.25246363209761 11.6433333333333
    "Q2 1999"           7.7982 574061 2.96666666666667  2.47432306255837            16.03
    "Q3 1999"           7.8432 581940              3.2  2.04937121564973 20.4433333333333
    "Q4 1999" 7.89219333333333 626967 3.16666666666667  2.68518518518515 23.8066666666667
    "Q1 2000" 8.22004333333333 626590              3.4  2.89123451124368 26.6166666666667
    "Q2 2000" 8.77797333333333 589911              3.2  2.91571753986331 26.7666666666667
    "Q3 2000"          8.95455 594980 3.33333333333333  3.42309447740759 29.8833333333333
    "Q4 2000"           9.2548 635181 3.03333333333333  3.11091073038775            29.67
    "Q1 2001" 8.88969333333333 631517 3.33333333333333  3.52363960749332            26.07
    "Q2 2001" 9.17891333333333 603358 3.36666666666667  3.93979637007527 26.7266666666667
    "Q3 2001"          8.99699 609647 3.56666666666667   2.5595763459841 25.2133333333333
    "Q4 2001"          8.90102 653161 3.36666666666667   2.0113686051596 19.3133333333333
    "Q1 2002" 8.90953333333333 623584 3.73333333333333   1.0340370529944 20.9233333333333
    "Q2 2002"          8.18353 636919 3.73333333333333  .468483816013621 25.2033333333333
    "Q3 2002"          7.52372 612090 3.66666666666667  1.41996557659209 26.9366666666667
    "Q4 2002"         7.318332 661001 3.53333333333333  2.22888984140592 26.7366666666667
    "Q1 2003" 7.05690666666667 647009 3.96666666666667  4.60554371002131 31.3366666666667
    "Q2 2003" 7.00835333333333 618078 4.33333333333333  2.24671470962272 26.4866666666667
    "Q3 2003" 7.33666666666667 624952 4.43333333333333  1.90920661858297 28.3833333333333
    "Q4 2003"          6.91894 666858              4.1  1.21593291404613            29.36
    "Q1 2004" 6.90666666666667 676751 4.13333333333333 -1.42682429677945            32.13
    "Q2 2004"             6.86 650667 4.33333333333333  .829187396351576 35.6266666666667
    "Q3 2004" 6.86333333333333 638736              4.5  1.20732722731057 40.5533333333333
    "Q4 2004" 6.33333333333333 691973 4.13333333333333  1.24275062137531            42.73
    end
    Sorry if it's a little messy.

    Thank you.

  • #2
    You need to double check if averaging across months is appropriate. For stock variables (in general), a sum is appropriate across the months that make up the quarter. If you're looking at a flow variable, you'll want to take the average. This will entail using Stata's collapse. You can import your data into Stata - see -help import excel-. To go from month to quarterly will depend on the format of your date variable, but here's an earlier example of how to do just that

    Code:
    gen quarter = qofd(date(date), "YMD")
    format quarter %tq
    collapse (mean) variable1 (sum) variable2 , by (quarter)
    From there you can save a tempfile and merge into your dataset on your date/quarter variable.

    Comment


    • #3
      Yes, I just realised Oil production (Stock Variable) should be a sum of 3 months while the index should be an average. While trying to save a temp file so I can merge the dataset later, I keep getting an error saying 'type mismatch'. How do I go about rectifying this issue please?


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int Date double(CrudeOilProductionWorld GlobalRealEconomicActivityBa)
       7305 62348.011  34.913651
       7336 62715.757  40.264105
       7365 61647.494  42.236642
       7396  60391.36  60.563561
       7426 59778.782  63.715754
       7457 60008.624  56.950721
       7487 59608.899  44.833068
       7518 59446.012  53.581964
       7549 58297.803  54.826769
       7579 55842.496  54.707264
       7610 56668.926  64.528623
       7640 58047.975  68.162191
       7671 57891.773  63.849176
       7702 58156.649  61.655825
       7730 58809.342  50.128833
       7761 57803.815  45.773622
       7791 56994.195  35.699931
       7822 56310.517  40.950771
       7852 55016.126  32.596962
       7883 54079.566  14.998854
       7914 54190.514  12.920053
       7944 54556.342  15.448341
       7975 53982.612  14.475943
       8005 54949.157  7.5267664
       8036 54696.333  2.9459996
       8067 54057.656 -6.4000714
       8095 52235.578  2.8206591
       8126 50937.234  10.762062
       8156 51782.807  7.6104123
       8187 53723.825 -16.736949
       8217 53304.037 -41.041029
       8248 53226.077 -43.390703
       8279  53081.15 -27.590892
       8309 54797.282 -27.028963
       8340 55385.984 -24.439834
       8370 54251.558 -19.692951
       8401 52762.757 -17.667912
       8432  49812.05 -12.797698
       8460 50735.958 -9.9127852
       8491 50958.936 -7.2289422
       8521 52658.767 -7.4396857
       8552 52868.746 -22.381617
       8582  54651.57 -33.641584
       8613 54498.585 -34.690343
       8644 55152.519 -31.677956
       8674 54891.545 -28.714398
       8705 55259.509 -21.342146
       8735 54454.589 -31.669797
       8766 54576.637 -29.809012
       8797 54927.706 -23.183593
       8826 54649.651  -18.94012
       8857  54848.69 -14.973718
       8887 54467.616 -15.613628
       8918 55744.865 -24.471786
       8948 55024.725 -29.366095
       8979 53346.397 -28.522501
       9010 53805.487 -25.383958
       9040 54216.567  -24.11736
       9071  54233.57 -16.577293
       9101 54193.563 -19.740469
       9132 52956.948 -23.301023
       9163 54426.531 -27.635959
       9191 54645.468 -25.911084
       9222  54219.59  -20.93313
       9252 52782.998 -24.960809
       9283 51324.413 -34.470393
       9313  52353.12  -50.22026
       9344 52216.159  -57.68459
       9375 54095.625 -50.139917
       9405 55779.147 -37.074529
       9436 56257.011 -36.259154
       9466 56571.921 -36.113282
       9497 55650.355 -37.871952
       9528 55660.371 -51.829724
       9556 55162.501 -55.476852
       9587 55266.682 -61.645056
       9617 56447.749 -66.486732
       9648   57220.1 -69.452396
       9678 58359.093 -81.082786
       9709 59013.237 -78.423937
       9740 54978.178 -54.388871
       9770 55334.802 -48.945979
       9801 56264.428 -51.270593
       9831 56434.726 -60.266097
       9862 55634.929 -46.674559
       9893 54938.329 -41.951425
       9921  54196.69 -35.247732
       9952  54870.27 -25.356082
       9982 55674.963 -19.612996
      10013 55375.706 -30.906031
      10043 57939.915 -33.855243
      10074 58737.602 -18.977109
      10105 58130.079 -22.529709
      10135 58325.247 -17.533316
      10166 57862.849 -12.526447
      10196 57936.912 -5.3418992
      10227 57137.658  2.6686286
      10258 57217.705  13.911968
      10287 57578.918  19.000836
      10318 57890.101  11.454389
      end
      format %td Date

      Comment


      • #4
        You'll want to do something along the lines of
        Code:
        gen quarter = qofd(Date)
        format quarter %tq
        
        gen c = 1 
        collapse (sum) c Crude* (mean) Global* , by (quarter)
        drop if c != 3 // Drop incomplete quarters
        drop c 
        
        tempfile temp 
        save `temp'
        
        use "Data.dta" , clear   // you'll need to change this 
        merge 1:1 quarter using `temp'

        Comment


        • #5
          I have never heard of getting a "type mismatch" error message when saving a data set, and I can't even imagine how that could happen. So departing from your exact words, I'll guess that after saving the new results in a tempfile and attempting to -merge- that back to your original data set you get the "type mismatch" error from the -merge- command. Am I right? This is no surprise: in creating your new quarterly results showin in #3, you created a Stata internal format numeric quarterly date variable, Date. But in the original data set that you show in #1, Date is a string variable. That is the type mismatch. Since string representations of dates are not very useful in Stata, the best solution would be to transform Date in the original data to a numeric Stata internal format quarterly variable. The particular formatting of the date variable in #1 is almost designed to maximize the difficulty of converting it, but it can be done:

          Code:
          split date, gen(d)
          destring d1 d2, ignore("Q") replace
          gen _date = yq(d2, d1)
          format _date %tq
          assert missing(date) == missing(_date)
          drop date
          rename _date date
          Also, I notice that the date variable is named date in #1, but Date in #2. Stata variable names are case sensitive, and in order for the merging of the data to run correctly, you must have the same name in both data sets. Either one is perfectly acceptable, but you must pick one and stick with it.

          Added: In the future, when describing a problem you have with code, it is usually helpful to copy/paste the exact commands and the exact Stata output you got, including error messages, from the Results window or your log file directly into the editor here. That eliminates the need for others to guess what actually happened.

          Comment


          • #6
            I'm very close to figuring it out but the only issue is with the last command.

            It reads

            "key variable quarter is str7 in master but float in using data
            Each key variable -- the variables on which observations are matched -- must be of the same generic type in the master and using datasets.
            Same generic type means both numeric or both string."

            How do I change my float variable (quarter in the using data) to string such that I can merge them?

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str7 quarter
            "Q1 1980"
            "Q1 1981"
            "Q1 1982"
            "Q1 1983"
            "Q1 1984"
            "Q1 1985"
            "Q1 1986"
            "Q1 1987"
            "Q1 1988"
            "Q1 1989"
            "Q1 1990"
            "Q1 1991"
            "Q1 1992"
            "Q1 1993"
            "Q1 1994"
            "Q1 1995"
            "Q1 1996"
            "Q1 1997"
            "Q1 1998"
            "Q1 1999"
            "Q1 2000"
            "Q1 2001"
            "Q1 2002"
            "Q1 2003"
            "Q1 2004"
            "Q1 2005"
            "Q1 2006"
            "Q1 2007"
            "Q1 2008"
            "Q1 2009"
            "Q1 2010"
            "Q1 2011"
            "Q1 2012"
            "Q1 2013"
            "Q1 2014"
            "Q1 2015"
            "Q1 2016"
            "Q2 1980"
            "Q2 1981"
            "Q2 1982"
            "Q2 1983"
            "Q2 1984"
            "Q2 1985"
            "Q2 1986"
            "Q2 1987"
            "Q2 1988"
            "Q2 1989"
            "Q2 1990"
            "Q2 1991"
            "Q2 1992"
            "Q2 1993"
            "Q2 1994"
            "Q2 1995"
            "Q2 1996"
            "Q2 1997"
            "Q2 1998"
            "Q2 1999"
            "Q2 2000"
            "Q2 2001"
            "Q2 2002"
            "Q2 2003"
            "Q2 2004"
            "Q2 2005"
            "Q2 2006"
            "Q2 2007"
            "Q2 2008"
            "Q2 2009"
            "Q2 2010"
            "Q2 2011"
            "Q2 2012"
            "Q2 2013"
            "Q2 2014"
            "Q2 2015"
            "Q2 2016"
            "Q3 1980"
            "Q3 1981"
            "Q3 1982"
            "Q3 1983"
            "Q3 1984"
            "Q3 1985"
            "Q3 1986"
            "Q3 1987"
            "Q3 1988"
            "Q3 1989"
            "Q3 1990"
            "Q3 1991"
            "Q3 1992"
            "Q3 1993"
            "Q3 1994"
            "Q3 1995"
            "Q3 1996"
            "Q3 1997"
            "Q3 1998"
            "Q3 1999"
            "Q3 2000"
            "Q3 2001"
            "Q3 2002"
            "Q3 2003"
            "Q3 2004"
            "Q3 2005"
            end

            Comment


            • #7
              You can create a quarter variable and then merge on that

              Code:
              rename quarter date
              gen quarter = quarterly(substr(date, -4, 4) + "" + substr(date, 1,2), "YQ")
              format quarter %tq
              Last edited by Justin Niakamal; 05 Feb 2019, 22:00.

              Comment

              Working...
              X