Announcement

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

  • long invalid varname

    Hello!

    I have data where the ID is a string variable str123, and I was trying to merge it to another dataset that has the same variable (with more observations).

    When I attempted to merge 1:1, I get the following error message:

    long invalid varname

    What does that mean? How can I fix it?

    Thanks,

    Lina Badawy

  • #2
    You don't give the syntax you tried -- please do read and act on https://www.statalist.org/forums/help#stata -- but my guess is that you tried to use long as (or as if it were) a variable name. long is a reserved word (for a storage type) and may not be used as a variable name.

    See 11.3 within https://www.stata.com/manuals/u11.pdf (or within the pdf documentation installed with your Stata).

    If that doesn't help, then -- as FAQ Advice #12 enjoins -- please show us the exact code you tried.

    Comment


    • #3
      Thanks Nick!

      I figured out the problem. Turns out I cannot merge while my master data has less observations than the using data. When I reversed my merging, it worked perfectly.

      Thanks a lot

      Comment


      • #4
        Well, I don't think that is true at all.

        Comment


        • #5
          Lina, Nick is correct. This is certainly not true about merging. There are no requirements for one dataset to have more rows than another.

          Comment


          • #6
            Since I just had the same issue and there is no record of the answer on this list: I am confident the issue in this case is that merge rejects the using data if there is a variable name "long" in it (which a lot of geocoded data does have). The master data can have a variable name "long" and merge with using fine, but if using data has a variable "long" then this somewhat confusing error message pops up. If you have geocoded data to merge, either rename "long" in using or reverse merge.

            Comment


            • #7
              I am surprised by #6 as the key point in this thread is that long cannot be a valid variable name, so the issue should not arise, i.e. the name would be rejected before any dataset was saved as such with long as one of the variable names.

              I can imagine problems with e.g. a variable called longitude and any attempt to use an abbreviation long.

              A way to undermine my surprise is to give evidence, such as output of describe that shows long in use as an entire variable name.

              Comment


              • #8
                While I have never encountered this exact problem, as I have never worked with data sets including longitude as best I can recall, I have at times been the recipient of would-be Stata data sets originally created in some other package and then exported, ostensibly, to a .dta file. But the other package didn't enforce Stata's restrictions on using keywords as variable names. So it is possible to have a .dta file that contains an illegal variable name like long. And even Stata does not necessarily pick this up when you -use- this kind of file, but may trip over it later with other commands that refer to that variable. My guess is that is what is going on here.

                Comment


                • #9
                  #8 seems likely to be on target here.

                  Comment


                  • #10
                    Here is how you create an example of a dataset as described in #8:

                    Code:
                    clear
                    input Long
                    42
                    end
                    tempfile valid invalid
                    save "`valid'"
                    filefilter "`valid'" "`invalid'" ,  from("Long") to("long") replace
                    use  "`invalid'" , clear
                    describe
                    Result:

                    Code:
                    . describe
                    
                    Contains data from C:\Users\klein\AppData\Local\Temp\ST_1ee0_000002.tmp
                     Observations:             1                  
                        Variables:             1                  21 Dec 2023 19:42
                    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    Variable      Storage   Display    Value
                        name         type    format    label      Variable label
                    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    long            float   %9.0g                 
                    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    Sorted by:

                    Here is how you fix the problem:

                    Code:
                    rename long foo
                    describe
                    yielding

                    Code:
                    . describe
                    
                    Contains data from C:\Users\klein\AppData\Local\Temp\ST_1ee0_000002.tmp
                     Observations:             1                  
                        Variables:             1                  21 Dec 2023 19:42
                    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    Variable      Storage   Display    Value
                        name         type    format    label      Variable label
                    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    foo             float   %9.0g                 
                    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    Sorted by: 
                         Note: Dataset has changed since last saved.
                    I created the example using Stata 17.

                    Comment

                    Working...
                    X