Announcement

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

  • Assign multiple labels to multiple variables with loop

    Dear all,

    I have some variables without labels assigned to them. I am looking for a way to label them atonce instead typing 'lab var' everytime for each variable. The code I used was :

    Code:
    foreach var of varlist x y x {
    lab var `var' "K" "L" "M"
    }
    Apparently, the code did not work. Is there any possibility in this regard?

    All the best.
    Roman

  • #2
    You need to do this processing parallel lists.

    For an implemented solution see labvars, part of labutil2 from SSC.

    Best
    Daniel

    Comment


    • #3
      Many thanks Daniel. labvars is a simplistic solution rather writing a loop.
      Best
      Roman

      Comment


      • #4
        Daniel gave good advice. More discussion within http://www.stata-journal.com/article...article=pr0009

        But if there is no structure to the pairing, then as at some point you will have to type in the names and the variable labels any how, so also there is no gain in looping really.

        Quite possibly there is more structure in your problems than your example indicated, in which case the best way to exploit it would depend on the details.

        Comment


        • #5
          Thank you so much for the advice and the article link Nick. I think, I need to read the article properly. Seems like it covers a lot of details relevant to this issue.
          Best
          Roman

          Comment


          • #6
            Huge thanks to Roman for the question and Daniel for the solution! Was looking for this now.

            Comment


            • #7
              As this thread has risen to attention again I will flag that a second edition of the paper linked in #4 is scheduled to appear in Stata Journal 21(4). If that link interested you and you are reading this in 2022 or later, please look for the newer version.

              Comment


              • #8
                In the spirit of #7, the update of labvars is elabel (Klein, 2019); the latest version of the software is available from the SSC.

                Comment

                Working...
                X