Announcement

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

  • Label variables with what is in the first line

    I wanna label each variable with the text in the first line of said variable.

    My data looks like this
    Varname1 Varname2
    LabelVar1 LabelVar2
    Value1Var1 Value1Var2
    Value2Var1... Value2Var2...

    My current code seems not to work.

    foreach var of varlist * {
    label variable `var' ``var'[1]'
    }

    What would I do to fix this? I do not get any error messages.

  • #2
    The line needs to be

    Code:
    label variable `var' "`=`var'[1]'"
    after which you need to

    Code:
    drop in 1
    destring, replace
    Sometimes it's easier to go back and read the data in all over again.

    The reason your code didn't work as you wished but there was no error message is fairly subtle. Here is my guess: Stata thinks that you are trying ``var'' and ignores [1] as trailing garbage. So, you are referring to a local macro that does not exist, which is allowed, and the result is an empty string, which here isn't fatal as you are just removing any variable label that happens to exist, and if there isn't one, that is fine with Stata too.
    Last edited by Nick Cox; 08 Nov 2022, 03:33.

    Comment


    • #3
      Thank you!

      Comment

      Working...
      X