Announcement

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

  • Save dataset into a *.dat file using local macro and string function "ustrregexra"

    Hello. I wanted to make a Stata data file and name the file using local macro and ustrregexra; I wanted to switch "-" to "_" using ustrregexra.

    Here are my codes:

    -----
    sysuse auto
    local x ustrregexra("BSI-TRJ-IBC","-","_")
    di `x'
    save "`x'.dta", replace

    -----

    And I got an error message: invalid 'BSI-TRJ-IBC'

    I would like to know what I missed. Thank you for your time!

  • #2
    Code:
    sysuse auto, clear
    local x  = ustrregexra("BSI-TRJ-IBC","-","_")
    di "`x'"
    save "`x'", replace
    In your code, with no = in the -local x ...- command, local macro x is not set to BSI_TRJ_IBC. It is set to ustrregexra("BSI-TRJ-IBC", "-", "_"). That works well in the -display- function since -display- anticipates getting fed expressions and it evaluates them. But save does not expect expressions, and -save ustrregexra("BSI-TRJ-IBC", "-", "_")- is a syntax error. (Unfortunately the error message is not very informative about what's wrong.) When you use -local x = some_expression-, you cause the expression to be evaluated and then the result of the evaluation is stored in local macro x, which is what you want and need here.

    Comment


    • #3
      I really appreciate you kind and detailed explanation. I need to be careful to test my codes using -di-. Thank you again!

      Comment

      Working...
      X