Announcement

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

  • Evaluating strings in local macro

    Colleagues,

    I'm passing variable to a local macro, in order to use in the chart:
    Code:
    local grphtitle : variable label `graphstub'may13
    .
    I would like to modify content of the macro by:
    1. Removing last 13 characters
    2. Adding some string up front: Measure for ABC:
    I tried:
    Code:
    local grphtitle : substr(`grphtitle',1,length(`1')-3)
    but got an error message: substr not allowed.
    Last edited by Konrad Zdeb; 01 Sep 2014, 03:50. Reason: Code.
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

  • #2
    I see various syntax errors here.

    substr() is a function, documented as such, not an extended macro function. So the syntax requires an equals sign, not a colon.

    You are extracting from a literal string, so need quotation marks to refer to that. This code should be less wrong:

    Code:
      
    local grphtitle = substr("`grphtitle'",1,length("`1'")-3)
    Note that this code has no obvious connection to removing the last 13 characters or the other elements of your problem.

    Comment


    • #3
      Nick,

      Thank you very much for showing interest in my modest problem. It just occurred to me that I typed `1' instead of the `grphtitle' (error from the previous version of the code where I tokenized some strings). Naturally, the code:
      Code:
      local grphtitle = substr("`grphtitle'",1,length("`grphtitle'")-13)
      works like a charm. Out of curiosity, what is the difference between using : and = when defining local macro, do both force expression to be evaluated?
      Kind regards,
      Konrad
      Version: Stata/IC 13.1

      Comment


      • #4
        On = and :

        Extended macro functions have the colon syntax. Functions used similarly have the equals signs syntax.

        You have to use the colon syntax to use extended macro functions. That's always true, if not part of the definition.

        But use of functions doesn't always require an equals sign. Here are two examples of substr()without equals signs:

        Code:
        . sysuse auto
        (1978 Automobile Data)
        
        . di substr("`: var label make'", 1, 4)
        Make
        
        . local foo : di substr("`: var label make'", 1, 4)
        
        . di "`foo'"
        Make
        Last edited by Nick Cox; 01 Sep 2014, 04:28.

        Comment


        • #5
          Thanks, this clarifies a lot.
          Kind regards,
          Konrad
          Version: Stata/IC 13.1

          Comment

          Working...
          X