Announcement

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

  • graph bar loop with changing titles

    Hi,

    I'm using

    foreach x of varlist x y z {
    graph bar, over(`x', label(labsize(small))) ytitle("percent") title(`x')
    }

    and title(`x') just writes the variable names as title. I want to use the variable labels as title. How does that work?

    Thanks
    -Nick

  • #2
    if you look at "help macro" and click on "extended functions" you will see how to put the variable label into a macro; just put that in your loop, before your graph command and adjust the title option to show that new macro

    Comment


    • #3
      Ok,

      now using:

      foreach x of varlist x y z {
      local z: variable label `x'
      graph bar, over(`x', label(labsize(small))) ytitle("percent") title(`x')
      }

      but it just creates the first bar chart for variable x. After doing that I get the error message "option xxx not allowed" where xxx is a string part of the variable label of y (not the first word in the label but the third one).

      Comment


      • #4
        You should show us exactly what you typed and what Stata typed in return. Copy and paste, and don't paraphrase or interpret! The advice in the FAQ is there for good reason. The more you edit what was done, the more difficult it can be to understand the problem.

        According to your code, you don't use the macro contents in your command.

        According to your problem report, you did use the macro contents in your command.

        The other problem is (I guess) that your variable label includes a comma and so what follows the comma is being misread as a suboption call for title().

        Code:
        foreach x of varlist x y z {
             local z: variable label `x'
             graph bar, over(`x', label(labsize(small))) ytitle("percent") title("`z'")
        }
        has a higher probability of working. NB

        1. Local macro z not local macro x.

        2. Double quotation marks " " to protect the comma.

        Comment


        • #5
          The solution was: title(`z') --> title("`z'")
          Thank you so much.

          Comment

          Working...
          X