Announcement

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

  • invalid syntax when referencing to variable labels in loops

    I'm trying to refer to the labels of variables (to include these in graph titles) when looping on a group of variables to generate graphs. I've tried several methods and changed several functions to plot graphs, but none of these would work. I've also read many previous posts regarding this topic, but even direct copy and past of the code referring to the value label wouldn't work.
    here's the code that calls variable labels in loops that I got from a previous post:
    "`:label variable `var'' sample1"
    (https://www.statalist.org/forums/for...iables-in-loop)

    and here's the full code that I'm using:

    local y "teacher volunteer "
    foreach var of local y {
    splitvallabels `var'
    catplot gender `var', ///
    percent(gender) ///
    var1opts(label(labsize(small))) ///
    var2opts(label(labsize(small)) relabel(`r(relabel)')) ///
    ytitle("Percent of Respondents by gender", size(small)) ///
    title("`:label variable `var'' sample1" ///
    , span size(medium)) ///
    blabel(bar, format(%4.1f)) ///
    intensity(25) ///
    asyvars
    }

    This works, but the title wouldn't show the variable label as the stata reports "invalid syntax."

    here's the graph I got:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	108.3 KB
ID:	1545699

    all good--except for the variable label part that's supposed to be included in the title.


    This is a fairly simple graph that looks at the percentage by gender who agree/disagree to the statement that they've participated in the project because they liked the teacher/the volunteer who organized it. However, the variable labels are more useful in titles than the variable names. I want this in a loop because I actually have more than two variables to loop (about 20 in total).

    Can somebody help me to figure out how I can correctly refer to the variable labels in the loop? Thanks!


  • #2
    Elsewhere you can find the idea of a minimal reproducible example to show your problem. https://stackoverflow.com/help/minim...ucible-example

    Here is one -- with the crucial information that catplot and splitvallabels are from SSC (FAQ Advice #12).


    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . catplot rep78 , title("`: label variable rep78'")
    invalid syntax
    
    . catplot rep78 , title("`: variable label rep78'")



    You had the wrong syntax for variable labels.

    In fact reference to
    https://www.statalist.org/forums/for...iables-in-loop shows that I to missed this point in #2, so there you go. Unsurprisingly, we all make mistakes.

    Then again that thread didn't include a reproducible example either, so it was easier to overlook that error when other errors were more obvious.

    Comment


    • #3
      You got the order mixed up, it should be "variable label varname", not "label variable varname".
      So:
      Code:
      title("`:variable label `var''")
      And if you want to add "sample1" to the title, which is suggested in your code in #1:
      Code:
      title("`:variable label `var'' sample1")

      Comment


      • #4
        Thanks a lot! That really helped.

        Comment

        Working...
        X