Announcement

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

  • Labeling multiple variables using the foreach loop

    I am trying to learn the foreach loop command in Stata 13.0.
    I want to label multiple variables using the foreach loop but whenever I enter the command below,

    Code:
    sysuse auto.dta, clear
    foreach x of varlist make - foreign{
        label variable `x'  `x' "for survey one"
        }
    OR
    Code:
    sysuse auto.dta, clear
    foreach x of varlist make - foreign{
        label variable `x'  " `x' "  "for survey one"
        }
    I get an invalid syntax error message.
    Code:
    invalid syntax
    What could be the problem with the above command?
    I will be grateful if someone can point me to any document or resource
    regarding the above problem.
    Last edited by Joseph Mawa; 30 Jan 2019, 04:56.

  • #2
    When you try to write a loop, start out by making the inner code work for one example. Try

    Code:
    sysuse auto, clear
    label variable make make "for survey one"
    or

    Code:
    label variable make " make " "for survey one"
    where I have replaced `x' with the variable name make. Then read

    Code:
    help label variable
    to find out that the syntax you are looking for is

    Code:
    label variable varname "label"
    Note that varname is followed by exactly one string. Compare this to your examples, make it work for one case, then write the loop, replacing make with `x'.

    Best
    Daniel

    Comment


    • #3
      The problem is invalid syntax. You are providing two strings after the variable name, but label expects one string as the new variable label. This is legal.

      Code:
      sysuse auto.dta, clear
      foreach x of varlist make-foreign {
          label variable `x'  "`x' for survey one"
      }
      So, label won't concatenate (join) strings for you. You need to do that yourself.

      EDIT: Overlaps with Daniel's helpful post, which is entirely consistent with mine.

      Comment


      • #4
        Originally posted by Nick Cox View Post
        The problem is invalid syntax. You are providing two strings after the variable name, but label expects one string as the new variable label. This is legal.

        Code:
        sysuse auto.dta, clear
        foreach x of varlist make-foreign {
        label variable `x' "`x' for survey one"
        }
        So, label won't concatenate (join) strings for you. You need to do that yourself.

        EDIT: Overlaps with Daniel's helpful post, which is entirely consistent with mine.
        Thanks for the helpful response. My problem was how to concatenate the two strings in the
        label variable command.

        Comment


        • #5
          Originally posted by Joseph Mawa View Post
          My problem was how to concatenate the two strings in the
          label variable command.
          You can

          Code:
          local concatenate = "foo" + "bar"
          label variable varname "`concatenate'"
          or, in one line

          Code:
          label variable varname `= "foo" + "bar"'
          However, simpler for your purposes is the code that I hoped you would come up with and that Nick gave you.

          Best
          Daniel

          Comment


          • #6
            Originally posted by daniel klein View Post

            You can

            Code:
            local concatenate = "foo" + "bar"
            label variable varname "`concatenate'"
            or, in one line

            Code:
            label variable varname `= "foo" + "bar"'
            However, simpler for your purposes is the code that I hoped you would come up with and that Nick gave you.

            Best
            Daniel
            Thanks Daniel

            Comment

            Working...
            X