Announcement

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

  • Including Quote Characters in STATA Calls from MATA

    Is there a way to include quote characters when passing variables from MATA to STATA? For instance, if I have
    Code:
    mata:
    a = "ABC"
    stata(sprintf("gen b=%s", a))
    I want the STATA code that is executed to be:
    Code:
    gen b="ABC"
    But it is currently:
    Code:
    gen b=ABC
    I have tried including the quotes in sprintf() by escaping with \, but this does not work.

  • #2
    Try something like the following.

    . local line_size `c(linesize)'

    . set linesize 80

    . mata:
    ------------------------------------------------- mata (type end to exit) ------
    : argument = char(34) + "ABC" + char(34)

    : sprintf("generate str b = %s", argument)
    generate str b = "ABC"

    : end
    --------------------------------------------------------------------------------

    . set linesize `line_size'

    .

    Comment


    • #3
      Compound double quotation marks apparently work in Mata as they do in Stata.
      Code:
      . mata:
      ------------------------------------------------- mata (type end to exit) ------
      : argument = `""ABC""'
      
      : sprintf("generate str b = %s", argument)
        generate str b = "ABC"
      
      : end
      --------------------------------------------------------------------------------
      
      .
      See
      Code:
      help m2_exp##remarks5

      Comment


      • #4
        Use st_addvar() and st_sstore() for the particular example. In general, calling Stata from Mata is rather slow and - as you have observed - sometimes inconvenient.

        Comment

        Working...
        X