Announcement

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

  • Error "invalid name" using a loop

    I have a dataset with some variables (var1, var2, var3) which contains numbers but they are recorded as strings. I want to destring them using real().

    I am preparing this code, in which I transform the variable with real, generating a new one, I drop the original one, and I save the new one with the name of the original. However, the following error is produced: "2var1 invalid name". I guess the problem is something related with quotation marks but I am not able to see it.

    Code:
    local varlist var1 var2 var3
    
    foreach v of local varlist {
        gen 2`v' = real(`v')
        drop `v'
        rename 2`v' `v'
    }
    Last edited by Diego Malo; 05 Nov 2022, 08:54.

  • #2
    gen 2`v' = real(`v')
    Stata variable names cannot start with numbers. See

    Code:
    help [M-1] naming
    Last edited by Andrew Musau; 05 Nov 2022, 09:09.

    Comment


    • #3
      Variables name cannot start with a number, try `v'2 instead.

      Comment


      • #4
        You are right. Thank you both, it was that!

        Comment

        Working...
        X