Announcement

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

  • Exporting to Excel or Word

    How can I go about exporting my stata results to excel or word.
    Attached my do file, but I run into error of
    post: 8 expressions expected and only 3 found
    r(198)
    Please help to amend my do file to be able to run it. Attached my do file
    multiple remit unit root test-2.do
    TQ

  • #2
    The Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post, advises that the appropriate way to present code samples is as text in the body of your post, enclosed within CODE delimiters. For those who may want to respond, the contents of the do-file attached to post #1 is as follows.
    Code:
    log using c:\POE\labor\remit, replace
    
    use C:\POE\Labor\remit.dta, clear
    
    encode obs, generate(time)
    tsset time
    summarize
    
    
    ***Step 2. Now create a temporary files using
    tempname anees_uroot
    tempfile unitroots
    
    ***Step 3. Then you need to write to the temporary files as to what should be the
    *names of the headers of the table. Also you need to specify which file to use and
    *what to add to the file as headers.
    
    postfile `anees_uroot' str12 name dfuller_statistic dfuller_pvalue dfuller_lags perron_statistic pperron_rho pperron_pvalue pperron_lags using `unitroots'
    
    ***Step 4. Load the variables. You can add as many variables as you wish or that
    *you would like to use in reports. Replace the var_i with your real variables here.
    foreach var of varlist bagrm indrm laorm malrm {
    
    ***Step 5. This section conducts the tests. I have added simple ADF and PP tests
    *for unit root. Please note, you should make sure the tests stores the r() to be
    *exported.
    dfuller bagrm
    dfuller indrm
    dfuller laorm
    dfuller malrm
    local dfuller_statistic = r(Zt)
    local dfuller_pvalue = r(p)
    local dfuller_lags = r(lags)
    
    pperron bagrm
    pperron indrm
    pperron laorm
    pperron malrm
    local perron_statistic = r(Zt)
    local pperron_rho = r(Zrho)
    local pperron_pvalue = r(p)
    local pperron_lags = r(lags)
    
    
    ***Step 6. Write the results into the temporary files and close the section code.
    post `anees_uroot' ("`var'") (`dfuller_statistic') (`dfuller_pvalue')
    (`dfuller_lags') (`perron_statistic') (`pperron_rho') (`pperron_pvalue')
    (`pperron_lags')
    }
    
    ***Step 7. Close the post file as started above. You can now see the produced results in a tabular format to be copied right from Stata output window 
    *or use the next line of code to export the stored results into Excel etc.
    postclose `anees_uroot'
    clear
    use `unitroots'
    
    ***Step 8. Export the results to Excel and keep the the stored names for tests_values as variables headers. 
    *The first column named; Name will contain variable names.
    
    export excel using "C:\POE\Labor\Stata Unit Root Tests.xls", firstrow(variables)
    replace
    
    ***Step 9 Optional. Use the list command in Stata to see the results produced by above tutorial.
    list
    Page

    Comment


    • #3
      Notwithstanding any other problems that might exist, the following has a couple of errors:

      Code:
      post `anees_uroot' ("`var'") (`dfuller_statistic') (`dfuller_pvalue')
      (`dfuller_lags') (`perron_statistic') (`pperron_rho') (`pperron_pvalue')
      (`pperron_lags')
      }
      1) You cannot continue a command, such as -post- across lines without indicating continuation with "///"
      2) The "}" has no role here, and if it did, it would have to have a previous "{" with which it would be paired.

      You got the error message you did, I believe, because Stata believed your -post- command terminated after the first line.
      One way to discover the location of errors like this is to use the -set trace on- command.

      Try your code with continuation indicated:
      Code:
      post `anees_uroot' ("`var'") (`dfuller_statistic') (`dfuller_pvalue') ///
      (`dfuller_lags') (`perron_statistic') (`pperron_rho') (`pperron_pvalue') ///
      (`pperron_lags')
      }

      Comment


      • #4
        I JUST SUBMITTED A RESPONSE THAT IT TOOK ME 15 MINUTES TO RESEARCH AND WRITE, AND THE NET RESULT WAS THAT A GREEN BOX APPEARED WITH A SMALL MARK SAYING DISAPPROVED, AND APPARENTLY WITH NO OBVIOUS WAY TO RECOVER AND EDIT MY TEXT. THIS IS APPALLING.

        With that off my chest I will attempt to reconstruct my post in a subsequent post.
        Last edited by William Lisowski; 04 Dec 2017, 12:42.

        Comment


        • #5
          DuckDuckGo (my preferred Google alternative) searching for anees_uroot turned up the following two links, the second of which is I believe the source of the code presented in posts #1 and #2.

          https://www.aneconomist.com/unit-roo...ables-in-stata

          Added in edit: the second URL was the reason the post was unapproved. I've added it below in two pieces inserting spaces into the proper name that led to the unapproval. That seems to have worked.

          https://www.linkedin.com/pulse/unit-...uality-tables-
          m u h a m m a d

          You see - if you reconstruct the URL and go to the linkedin page - that linkedin induced the line wrapping that led Tinfah Chung to omit the necessary continuation characters in step 6.

          With regard to the "}" in step 6, I believe it matches the foreach in step 4 and thus is needed.

          Step 5 has a number of problems. As shown on the web sites linked above, it reads
          Code:
          dfuller `var'
          local dfuller_statistic = r(Zt)
          dfuller `var'
          local dfuller_pvalue = r(p)
          dfuller `var'
          local dfuller_lags = r(lags)
          pperron `var'
          local perron_statistic = r(Zt)
          pperron `var'
          local pperron_rho = r(Zrho)
          pperron `var'
          local pperron_pvalue = r(p)
          pperron `var'
          local pperron_lags = r(lags)
          which repeatedly runs dfuller and pperron to no good purpose; the following should have the same effect.
          Code:
          dfuller `var'
          local dfuller_statistic = r(Zt)
          local dfuller_pvalue = r(p)
          local dfuller_lags = r(lags)
          pperron `var'
          local perron_statistic = r(Zt)
          local pperron_rho = r(Zrho)
          local pperron_pvalue = r(p)
          local pperron_lags = r(lags)
          Comparing this to the code shown in post #2 we see that the code in post #2 will repeatedly save the results from the malrm models, ignoring the other three. One or the other of the two samples above should be used instead.
          Last edited by William Lisowski; 04 Dec 2017, 12:55.

          Comment


          • #6
            Aha, William Lisowski , much better response. I just seized on the continuation problem, as it was consistent with the 3 vs. 8 thing.

            Comment


            • #7
              Originally posted by William Lisowski View Post
              I JUST SUBMITTED A RESPONSE THAT IT TOOK ME 15 MINUTES TO RESEARCH AND WRITE, AND THE NET RESULT WAS THAT A GREEN BOX APPEARED WITH A SMALL MARK SAYING DISAPPROVED, AND APPARENTLY WITH NO OBVIOUS WAY TO RECOVER AND EDIT MY TEXT. THIS IS APPALLING.

              With that off my chest I will attempt to reconstruct my post in a subsequent post.
              William,

              What you are seeing is the spam filter flagging your post for moderation. I try to stay on top of this by checking the forum throughout the day (no, the forum software does not send alerts when a message has been flagged for moderation), but there have been instances when that timeline slips. Since email is more direct, simply click the 'Contact Us' button on the lower right, explain the issue, and I will be sure to clear the post as soon as possible.

              -Pete

              Comment


              • #8
                I have tried using post 3 suggestion but it doesnt work. The original code is ok. As to post 5, I used the recommended code and I got back the same error message as:
                post:
                8 expressions expected and only 3 found
                r(198);
                As for post 5, this url https://www.linkedin.com/pulse/unit-...uality-tables-
                doesn't exist.
                I will be happy to try any other suggestions to make this work.
                Tq

                Comment


                • #9
                  The details are as follows:

                  . postfile `anees_uroot' str12 name dfuller_statistic dfuller_pvalue ///
                  > dfuller_lags perron_statistic pperron_rho ///
                  > pperron_pvalue pperron_lags using `unitroots'

                  .
                  . ***Step 4. Load the variables. You can add as many variables as you wish or that
                  . *you would like to use in reports. Replace the var_i with your real variables here.
                  . foreach var of varlist bagrm indrm laorm malrm {
                  2.
                  . ***Step 5. This section conducts the tests. I have added simple ADF and PP tests
                  . *for unit root. Please note, you should make sure the tests stores the r() to be
                  . *exported.
                  . dfuller bagrm
                  3. local dfuller_statistic = r(Zt)
                  4. local dfuller_pvalue = r(p)
                  5. local dfuller_lags = r(lags)
                  6. dfuller indrm
                  7. local dfuller_statistic = r(Zt)
                  8. local dfuller_pvalue = r(p)
                  9. local dfuller_lags = r(lags)
                  10. dfuller laorm
                  11. local dfuller_statistic = r(Zt)
                  12. local dfuller_pvalue = r(p)
                  13. local dfuller_lags = r(lags)
                  14. dfuller malrm
                  15. local dfuller_statistic = r(Zt)
                  16. local dfuller_pvalue = r(p)
                  17. local dfuller_lags = r(lags)
                  18.
                  . pperron bagrm
                  19. local perron_statistic = r(Zt)
                  20. local pperron_rho = r(Zrho)
                  21. local pperron_pvalue = r(p)
                  22. local pperron_lags = r(lags)
                  23. pperron indrm
                  24. local perron_statistic = r(Zt)
                  25. local pperron_rho = r(Zrho)
                  26. local pperron_pvalue = r(p)
                  27. local pperron_lags = r(lags)
                  28. pperron laorm
                  29. local perron_statistic = r(Zt)
                  30. local pperron_rho = r(Zrho)
                  31. local pperron_pvalue = r(p)
                  32. local pperron_lags = r(lags)
                  33. pperron malrm
                  34. local perron_statistic = r(Zt)
                  35. local pperron_rho = r(Zrho)
                  36. local pperron_pvalue = r(p)
                  37. local pperron_lags = r(lags)
                  38.
                  .
                  . ***Step 6. Write the results into the temporary files and close the section code.
                  . post `anees_uroot' ("`var'") (`dfuller_statistic') (`dfuller_pvalue')
                  39. (`dfuller_lags') (`perron_statistic') (`pperron_rho') (`pperron_pvalue')
                  40. (`pperron_lags')
                  41. }

                  Dickey-Fuller test for unit root Number of obs = 26

                  ---------- Interpolated Dickey-Fuller ---------
                  Test 1% Critical 5% Critical 10% Critical
                  Statistic Value Value Value
                  ------------------------------------------------------------------------------
                  Z(t) -1.218 -3.743 -2.997 -2.629
                  ------------------------------------------------------------------------------
                  MacKinnon approximate p-value for Z(t) = 0.6659

                  Dickey-Fuller test for unit root Number of obs = 26

                  ---------- Interpolated Dickey-Fuller ---------
                  Test 1% Critical 5% Critical 10% Critical
                  Statistic Value Value Value
                  ------------------------------------------------------------------------------
                  Z(t) 0.236 -3.743 -2.997 -2.629
                  ------------------------------------------------------------------------------
                  MacKinnon approximate p-value for Z(t) = 0.9742

                  Dickey-Fuller test for unit root Number of obs = 26

                  ---------- Interpolated Dickey-Fuller ---------
                  Test 1% Critical 5% Critical 10% Critical
                  Statistic Value Value Value
                  ------------------------------------------------------------------------------
                  Z(t) -1.793 -3.743 -2.997 -2.629
                  ------------------------------------------------------------------------------
                  MacKinnon approximate p-value for Z(t) = 0.3841

                  Dickey-Fuller test for unit root Number of obs = 26

                  ---------- Interpolated Dickey-Fuller ---------
                  Test 1% Critical 5% Critical 10% Critical
                  Statistic Value Value Value
                  ------------------------------------------------------------------------------
                  Z(t) -4.499 -3.743 -2.997 -2.629
                  ------------------------------------------------------------------------------
                  MacKinnon approximate p-value for Z(t) = 0.0002

                  Phillips-Perron test for unit root Number of obs = 26
                  Newey-West lags = 2

                  ---------- Interpolated Dickey-Fuller ---------
                  Test 1% Critical 5% Critical 10% Critical
                  Statistic Value Value Value
                  ------------------------------------------------------------------------------
                  Z(rho) -2.094 -17.268 -12.532 -10.220
                  Z(t) -1.260 -3.743 -2.997 -2.629
                  ------------------------------------------------------------------------------
                  MacKinnon approximate p-value for Z(t) = 0.6471

                  Phillips-Perron test for unit root Number of obs = 26
                  Newey-West lags = 2

                  ---------- Interpolated Dickey-Fuller ---------
                  Test 1% Critical 5% Critical 10% Critical
                  Statistic Value Value Value
                  ------------------------------------------------------------------------------
                  Z(rho) 0.618 -17.268 -12.532 -10.220
                  Z(t) 0.853 -3.743 -2.997 -2.629
                  ------------------------------------------------------------------------------
                  MacKinnon approximate p-value for Z(t) = 0.9924

                  Phillips-Perron test for unit root Number of obs = 26
                  Newey-West lags = 2

                  ---------- Interpolated Dickey-Fuller ---------
                  Test 1% Critical 5% Critical 10% Critical
                  Statistic Value Value Value
                  ------------------------------------------------------------------------------
                  Z(rho) -5.211 -17.268 -12.532 -10.220
                  Z(t) -1.920 -3.743 -2.997 -2.629
                  ------------------------------------------------------------------------------
                  MacKinnon approximate p-value for Z(t) = 0.3227

                  Phillips-Perron test for unit root Number of obs = 26
                  Newey-West lags = 2

                  ---------- Interpolated Dickey-Fuller ---------
                  Test 1% Critical 5% Critical 10% Critical
                  Statistic Value Value Value
                  ------------------------------------------------------------------------------
                  Z(rho) -22.465 -17.268 -12.532 -10.220
                  Z(t) -4.492 -3.743 -2.997 -2.629
                  ------------------------------------------------------------------------------
                  MacKinnon approximate p-value for Z(t) = 0.0002
                  post: 8 expressions expected and only 3 found
                  r(198);

                  Comment


                  • #10
                    Here is your code from post #2 with the recommended changes from posts #3 and #5 made to sections 5 and 6.

                    Code:
                    log using c:\POE\labor\remit, replace
                    
                    use C:\POE\Labor\remit.dta, clear
                    
                    encode obs, generate(time)
                    tsset time
                    summarize
                    
                    ***Step 2. Now create a temporary files using
                    tempname anees_uroot
                    tempfile unitroots
                    
                    ***Step 3. Then you need to write to the temporary files as to what should be the
                    *names of the headers of the table. Also you need to specify which file to use and
                    *what to add to the file as headers.
                    
                    postfile `anees_uroot' str12 name dfuller_statistic dfuller_pvalue dfuller_lags perron_statistic pperron_rho pperron_pvalue pperron_lags using `unitroots'
                    
                    ***Step 4. Load the variables. You can add as many variables as you wish or that
                    *you would like to use in reports. Replace the var_i with your real variables here.
                    foreach var of varlist bagrm indrm laorm malrm {
                    
                    ***Step 5. This section conducts the tests. I have added simple ADF and PP tests
                    *for unit root. Please note, you should make sure the tests stores the r() to be
                    *exported.
                    dfuller `var'
                    local dfuller_statistic = r(Zt)
                    local dfuller_pvalue = r(p)
                    local dfuller_lags = r(lags)
                    pperron `var'
                    local perron_statistic = r(Zt)
                    local pperron_rho = r(Zrho)
                    local pperron_pvalue = r(p)
                    local pperron_lags = r(lags)
                    
                    ***Step 6. Write the results into the temporary files and close the section code.
                    post `anees_uroot' ("`var'") (`dfuller_statistic') (`dfuller_pvalue') ///
                    (`dfuller_lags') (`perron_statistic') (`pperron_rho') (`pperron_pvalue') ///
                    (`pperron_lags')
                    }
                    
                    ***Step 7. Close the post file as started above. You can now see the produced results in a tabular format to be copied right from Stata output window 
                    *or use the next line of code to export the stored results into Excel etc.
                    postclose `anees_uroot'
                    clear
                    use `unitroots'
                    
                    ***Step 8. Export the results to Excel and keep the the stored names for tests_values as variables headers. 
                    *The first column named; Name will contain variable names.
                    
                    export excel using "C:\POE\Labor\Stata Unit Root Tests.xls", firstrow(variables)
                    replace
                    
                    ***Step 9 Optional. Use the list command in Stata to see the results produced by above tutorial.
                    list
                    Page

                    Comment


                    • #11
                      Thks for post #10. I have already tried that and the result is shown in post #9.

                      Comment


                      • #12
                        Originally posted by tinfah chung View Post
                        Thks for post #10. I have already tried that and the result is shown in post #9.
                        No, the code you present in post #9 is not the code I presented in post #10. The code I highlighted in red in post #10 corrects the mistakes you made in steps 5 and 6 of post #9.

                        Comment


                        • #13
                          Ok I have tried the code that you present in post#10 and this is the smcl file:

                          . use C:\POE\Labor\remit.dta, clear

                          .
                          . encode obs, generate(time)

                          . tsset time
                          time variable: time, 1 to 27
                          delta: 1 unit

                          . summarize

                          Variable | Obs Mean Std. Dev. Min Max
                          -------------+---------------------------------------------------------
                          obs | 0
                          bagrm | 27 5.838519 2.811796 2.46 10.59
                          indrm | 27 517743.7 58843.31 426612.1 632823.9
                          laorm | 27 14663.37 269.4687 14099.1 15021.1
                          malrm | 27 41847.59 9859.317 26823.27 68027.2
                          -------------+---------------------------------------------------------
                          phirm | 27 9421.333 664.8832 8575.5 11555
                          slrm | 27 1.900429 1.976121 -.220403 10.34593
                          trm | 27 32441.08 15368.38 -4467.8 59848.39
                          baggdp | 27 566.9693 301.0224 284.73 1358.78
                          indgdp | 27 1711.595 1163.157 463.97 3687.95
                          -------------+---------------------------------------------------------
                          laogdp | 27 766.3763 668.2096 203.26 2353.15
                          malgdp | 27 6067.541 2885.978 2440.59 11183.96
                          phigdp | 27 1518.059 750.8822 715.14 2951.07
                          slgdp | 27 1646.605 1203.832 470.34 3844.89
                          tgdp | 27 3414.34 1589.068 1508.29 6171.26
                          -------------+---------------------------------------------------------
                          time | 27 14 7.937254 1 27

                          .
                          .
                          . ***Step 2. Now create a temporary files using
                          . tempname anees_uroot

                          . tempfile unitroots

                          .
                          . ***Step 3. Then you need to write to the temporary files as to what should be the
                          . *names of the headers of the table. Also you need to specify which file to use and
                          . *what to add to the file as headers.
                          .
                          . post `anees_uroot' ("`var'") (`dfuller_statistic') (`dfuller_pvalue') ///
                          > (`dfuller_lags') (`perron_statistic') (`pperron_rho') (`pperron_pvalue') ///
                          > (`pperron_lags')
                          post __000000 not found
                          r(111);

                          end of do-file

                          r(111);

                          . exit, clear

                          Comment


                          • #14
                            Please note that code presented in post #10 suggested no change to the command in step 3. In post #13 you replaced the postfile command in step 3 with the post command from step 6 and that is a mistake.
                            Last edited by William Lisowski; 05 Dec 2017, 05:19.

                            Comment


                            • #15
                              Ah yes, tq for your time. It is working and sorry about the confusion with step 3 and step 6 which causes me some problems esp post and postfile. Thank you once again - very helpful indeed.

                              Comment

                              Working...
                              X