Announcement

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

  • Converting macros to a string and combining

    Hello statalisters,

    I would like to associate the mean and sd of a variable within one string macro.

    I proceed like this :

    quietly summarize age
    local mu=round(r(mean),0.1)
    local sd=round (r(sd),0.1)

    Then I would like to convert these mu sd -> string in order to do :
    local mu=string(mu) // Doesn't work
    local sd=string(sd) // Doesn't work

    local p1 "("
    local p2 ")"

    local musd `sd' `p1' `age' `p2'

    disp "`musd'"
    . 34(10) // What I want but doesn't work


    Then I would like to store `musd' in a cell of a matrix (It doesn't seem to work).

    Please help

  • #2
    Here are some techniques.

    Code:
     
    quietly summarize age 
    local mu : di %2.1f  r(mean) 
    local sd : di %2.1f r(sd) 
    di "`mu'  `sd'"
    
    local whatIwant "`: di %2.1f r(mean)'   `: di %2.1f r(sd)'"
    There are various small and large problems with your code. Here is a partial list.

    0. Local macros are in essence strings any way.

    1. Using a specific format is a better way of getting rounded results than using round() as exact decimal rounding of fractions is rarely possible (although display is often smart enough to hide that). See sources referred to by search precision.

    2. You referred to mu and sd but very likely you have no such variables or scalars. When you want to evaluate local macros you need to use paired single quotes.

    3. You can not store arbitrary strings in cells of a Stata matrix, as Stata matrices contains numeric values only. Mata is more flexible.

    Comment


    • #3
      Ok, thanks for your help.

      Comment

      Working...
      X