Announcement

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

  • Macro quotes in string expression

    Dear Experts,

    I have a simple code:

    Code:
    unab var_of_interest: Coating1 PumpStatus EX
    
    foreach var of varlist `var_of_interest' {
        
        global var_of_interest_mult = "$var_of_interest_mult" + "*" + "`var'"
    }
    di "$var_of_interest_mult"

    Which gives me a result:

    Code:
    .di "$var_of_interest_mult"
     * Coating1 * PumpStatus * EX
    However i need to get a text string which looks like macro as end result, (see below):

    `Coating1' * `PumpStatus' * `EX'


    I tried to manipulate with ` and ' putting them to
    Code:
    global var_of_interest_mult = "$var_of_interest_mult" + "*" + "`var'"
    between the lines. as well as inserting into "*", but it reads them as macro afterwards which does not exist ("`*'" results in "" (nothing))
    The reson is that later i would like to insert it to the graph function which will multiply the slope by the macro number.

    Something like:
    Code:
    (function y = (`main') * $var_of_interest_mult, range(`x1_`i'' `x2_`i''))
    Thank you for your time in advance.

    Pavlo

  • #2
    I can't follow this. Why not just write

    Code:
    Coating1 * PumpStatus * EX
    into your code directly, as you want something very specific and you know what you want. Alternatively consider this

    Code:
    . local myexp Coating1 PumpStatus EX
    
    . local myexp : subinstr local myexp " " "*", all

    Comment


    • #3
      Hi Nick,

      The reason is that
      Code:
       Coating1 * PumpStatus * EX
      will change as variables depending which dataset i will be using and these are over 60 in the future. Therefore i would like to define these variables at the beginning of the do-file, not having to look inside the code seaching for a change.

      A piece that i gave is just a little part of what i have.

      Later in the code i will automatically generate locals `Coating1' `PumpStatus' `EX' (in this example, they will be other in another dataset) and would like to put them into graph, as shown previously

      That is why i need a string that looks like

      Code:
      `Coating1' * `PumpStatus' * `EX'
      Last edited by Paul Shlapa; 09 Dec 2022, 08:09.

      Comment

      Working...
      X