Announcement

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

  • How to label a scalar or global variable?

    Is it possible to label a scalar or global variable?

    For example, in the code below, I would like to label Hx as "High X" and Lx as "Low X". Is there a way to do so in Stata 14?

    Code:
    sum x
    global Hx=r(mean)+r(sd)
    scalar Lx=r(mean)+r(sd)

  • #2
    As far as I know, you cannot attach labels to scalars or global macros (nor local macros, for that matter). Only variables can be labeled.

    By the way, I will just add my customary caution that the use of global macros is an inherently unsafe programming practice and should be used only when there is absolutely no alternative. While I cannot see the entire programming context in which you are working, storing some results from a previous command would usually be best done with either a scalar or a local macro. There are very few circumstances in which local macros will not serve the purpose, and those are safe. (-scalars- are semi-safe. If you give your scalar a name that matches the name of a variable in your data, you can end up producing bugs that are difficult to spot and fix. But at least you can see what variable names you need to avoid. -global-s, on the other hand, are dangerous because you have to avoid clashing with any other global macro in any other program that might happen to be running--and you don't know in advance what those are.)

    Comment


    • #3
      Clyde's right. You can't do this. You just use display alongside to say what you want.

      For related discussion of terminology see e.g. https://www.stata.com/statalist/arch.../msg01258.html

      Comment


      • #4
        Thanks Clyde and Nick! Much clearer now

        Comment


        • #5
          In addition, if one need such structure (num scalar + description),one might use a matrix with one element and assign row/colnames.
          Code:
          matrix Hx=r(mean)+r(sd)
          matrix rownames Hx = "High X"
          matrix colnames Hx = "Some other info up to 32 chars"
          
          help matrix
          help matmacfunc
          help el()

          Comment


          • #6
            thanks, bjart, your post provided me a new way to do what i wanted!

            Comment

            Working...
            X