Announcement

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

  • How to perform operation on each element of a local macro?

    Hi to every forumer, my best wishes for this new year to come.

    Code:
    local number 5 8 7 4 22 6 8
    I'd like to create a new local macro (or replace the existing one, I don't mind) containing the log transformation of each element of local number. Do you know how can I do this efficiently? I tried using the macro function subinstr to replace each `element' by "log(`element')" but if I do

    Code:
    di `newnumber'
    I get a series of numbers that are log-transformed, but not separated by any space. this is problematic because I want to include `newnumber' in a graph in the xline() option. Can you please help?

    Thank you.

  • #2
    The elements of a local macro are no more than they are perceived to be by users, as local macros are strings and spaces within local macros are just characters like any other. Parsing strings into elements by e.g. spaces is supported but not part of the definition of a local macro.

    That pedantry aside, it is clear what you want, but not clear to me what the best answer might be. You could just loop over the contents and create a new local macro.

    Or perhaps you're better off putting the numbers into a vector in Stata (strictly a matrix with either one row or one column) or in Mata and transforming the vector directly.

    Contrary to possible impressions, the latter method I think keeps more precision than the former, which might be important for logarithms.

    Which is better depends on what is upstream and what is downstream of the immediate question, to mix metaphors.
    Last edited by Nick Cox; 03 Jan 2024, 06:45.

    Comment


    • #3
      Nick: Thank you for clarifying. I'll try the Mata option for more precision. My first problem could be solved by simply doing:

      Code:
      local newnumber
      foreach elem of local number {
      local newnumber "`newnumber' `=log(`elem')'"
      }
      Which put the lines at the desired lines. But now, I'd like to adapt the macro in the xlabel() format. As my graph is in log and I'm interested in income, I want the label of the xline() to be in levels but the tick in log. I tried :

      Code:
      set tr on
      local U 5 8 7 9 5 2
      local transformedU ""
      
      foreach num of local U {
          local transformedU "`transformedU' `=log(`num')' `"`num'"'"
      }
      
      di "`transformedU'"
      output :
      . di "`transformedU'"
      1.6094379124341 `5"' 2.079441541679836 `"8"' 1.945910149055313 `"7"' 2.19722457733622 `"9"' 1.6094379124341 `"5"' .6931471805599453 `"2"'" invalid name

      I know it has to do with embedded quotes, but the lesson never seems to stick in my head I thought I did the right move to put `' in front of "`num'" but it doesn't give me a proper output. Can you solve this issue?

      Comment


      • #4
        Well, mylabels from the Stata Journal does this for you. The command goes back to 2003, but see

        SJ-22-4 gr0092 . . . . . . . . . . . . Speaking Stata: Automating axis labels
        (help nicelabels, mylabels, myticks if installed) . . . . . N. J. Cox
        Q4/22 SJ 22(4):975--995
        provides commands to handle two common problems with graph
        axis labels: decide in advance on some "nice" numbers to
        use on one or both axes and show particular labels on some
        transformed scale


        Code:
        mylabels 2 5 7 8 9, myscale(log(@) local(yla)
        and if your interest remains how do to do this for yourself, then the code shows how, and some other stuff too.

        But a one-liner: You might try


        Code:
         local transformedU `transformedU' `=log(`num')' `"`num'"' 
        It's certainly fiddly working out which quotation marks are sacrifices that Stata will eat (politely and benignly, but eat nevertheless) and which will pass through parsing unscathed.

        A range from 2 to 9 is a 4.5x range, and readers will strain to recognise a log scale.

        Comment

        Working...
        X