Announcement

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

  • How to combine char(40) and a string

    I have a standard error saved in a local variable `se'. For instance, the s.e. is .89. I want it to be (0.89) saved in another local variable. Then I can export it using outreg2. But probably because "(" is a special character, I haven't figured out a way to do so.

    I tried to generate "(0" and save it in a local variable, but I failed.

    I tried:
    1)
    local bb=char(40) +"0"
    disp `bb'

    2)
    local bb="(" +"0"
    disp `bb'

    They gave me the same error:

    "too few ')' or ']'"

    I wondered how to fix this. Thanks!
    Last edited by Kailin Gao; 06 Sep 2019, 23:31.

  • #2
    If you want to view contents of the local macro, put quotation marks around it.

    Code:
    local bb= "(0"
    di "`bb'"

    Res.:

    Code:
    . local bb= "(0"
    
    .
    . di "`bb'"
    (0

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      If you want to view contents of the local macro, put quotation marks around it.

      Code:
      local bb= "(0"
      di "`bb'"

      Res.:

      Code:
      . local bb= "(0"
      
      .
      . di "`bb'"
      (0
      Thanks Andrew! I think it works. I have a follow-up question on this:

      `se' is .015, generated from a post-estimation test.

      . if `se'<1{
      . local se="(0"+string(`se') + ")"
      . }
      . disp "`se'"
      (0.015)
      . disp `se'
      .015

      It is a little hard for me to understand why Stata displays .015 instead of
      (0.015)
      when I use
      "disp `se'". Any comments would be appreciated.

      Comment


      • #4
        If I ask you what is the solution to the following, you won't have a lot of difficulties answering.

        Code:
        local v= 5-(3+1)
        Res.:

        Code:
        . di `v'
        1
        The parentheses in your example serve the same purpose. Factor them out and all you are left with is the numerical value. If you wonder why Stata displays .015 instead of 0.015, then refer to

        Code:
        help format
        In your example, you can do the following:

        Code:
        local se= 0.015
        di %05.3f `se'
        Res.:

        Code:
        . di %05.3f `se'
        0.015
        Now, if you want the parentheses to appear as parentheses, store them as a string and apply the advice in #2 to view the contents of the local macro.

        Code:
        local se= "(0.015)"
        di "`se'"
        Res.:

        Code:
        . local se= "(0.015)"
        
        . di "`se'"
        (0.015)

        Comment


        • #5
          Thanks Andrew! It is very helpful. But I ran into the other problem.

          The codes and error are as follows:


          . disp `se' //now I successfully output the quotes thanks to your advice.
          (6.106237957240175)

          . local filename "agrecoef"

          . outreg2 using "`filename'", append adj excel addtext( Year FE,Yes , Country FE, Yes , Industry
          > FE, Yes) symbol(***, **, *) nocons dec(3) ///
          > sortvar( wgi lntotal_asset fuelexp_gdp lnpop gdpgr CPI tax ) drop(_I*) adds(median_b,`co',median
          > _se,`se')

          too many ')' or ']'
          .
          It seems that outreg2 can not export quotes. I wondered how I can deal with it. Thanks!
          Last edited by Kailin Gao; 27 Oct 2019, 09:22.

          Comment


          • #6
            outreg2 is from SSC. From the documentation, the -addstat()- option expects numbers.

            addstat(name, stats, ..) access e-class, r-class, s-class scalar statistics, or just plain numbers in new rows below the R-squared (if shown). Comma separted. The user must specify both a name and a value for the statistic. See example.
            So, to add numbers in parenthesis, specify them as text.

            Code:
            local se= "(0.015)"
            outreg2 using myfile.txt, addtext(my_se,"`se'")

            Comment


            • #7
              Originally posted by Andrew Musau View Post
              outreg2 is from SSC. From the documentation, the -addstat()- option expects numbers.



              So, to add numbers in parenthesis, specify them as text.

              Code:
              local se= "(0.015)"
              outreg2 using myfile.txt, addtext(my_se,"`se'")
              Thanks very much Andrew! That works perfect!
              Best,
              K

              Comment

              Working...
              X