Announcement

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

  • Graph's naming rule and Stata reports error

    Dear Stata users,

    My question is about graph's naming rule in Stata. After many tries, I know now that there should be no blank when naming a Stata graph. I wonder is there someone can provide a complete naming rule in Stata. See below for some examples.

    Code:
    sysuse auto, clear
    label var mpg "Mileage(mpg)12To41"
    graph drop _all
    foreach v of varlist price rep78 mpg {
     local name: variable label `v'
     local nname: word 1 of `name'
     graph hbar (mean) `v', over(foreign, sort(1) descen) title("`nname'") name("`nname'")
     }
    Code:
    ( invalid name
    ) invalid name
    12To41 invalid name
    r(198);

  • #2
    I don't think I have thought about this, but I don't think that experience contradicts a guess that graph names are governed by the same rules as names in general. See [U] 11.3.

    Comment


    • #3
      Nick, thank you all the same. I think this is a trivial matters. I post this thread only because in a loop I want to use variables' label which are composed of multiple piece of word as graphs' name, and the existing naming rule leave me some (a trifle) inconvenience.
      Last edited by Chen Samulsion; 28 Oct 2018, 03:44.

      Comment


      • #4
        strtoname() exists for your purpose of taking arbitrary strings and producing legal names.

        Code:
        . di strtoname("Chen is puzzled")
        Chen_is_puzzled

        Comment


        • #5
          Dear Nick Cox, nice and humorous answer! The codes above run successfully after inserting strtoname() function. Thank you so much!
          Code:
          sysuse auto, clear
          label var mpg "Mileage(mpg)12To41"
          graph drop _all
          foreach v of varlist price rep78 mpg {
           local name: variable label `v'
           local nname=strtoname("`name'")
           graph hbar (mean) `v', over(foreign, sort(1) descen) title("`nname'") name("`nname'")
           }

          Comment

          Working...
          X