Announcement

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

  • graph add vertical line using value in macro variable

    I am using Stata 13
    I want to develop code to add a single vertical line on a graph using a value obtained from a dataset rather than having to hard code the position of the line. This would make the code general.
    I thought I would do it by saving a value from the data set in a macro variable and use the macro variable to substitute into the xline graph option.
    The code below fails to add a line when I use the macro variable but works when hard coded.

    Is there something else I should be doing to get this to work?

    clear
    set obs 12
    gen y = 42
    gen time = _n/y*100
    line y time, xline(7)

    global time1 7
    macro list

    line y time, xline(`time1')

    Thanks,
    Kim

  • #2
    You greated a global macro rather than than a local macro for time1.

    This should work for you.
    Code:
    line y time, xline($time1)
    Red Owl
    Stata/IC 16.0 (Windows 10, 64-bit)

    Comment


    • #3
      Great - it works!
      Use $time1 for a global macro.

      Also I took note of your aside about local vs global macro and experimented and found that
      local time2 20
      line y time, xline(`time2')
      also worked. So for a local macro we use `time2'.
      I think in testing this wasn't working for me because I was running the code in parts, i.e. execute the macro command and stop to confirm it had worked and then run the plotting command with a separate execute command. When I selected both lines in my do file and executed them together it worked.

      Thank you for your help,
      I had spent a lot of time on this,
      Kim

      Comment

      Working...
      X