Announcement

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

  • Error "alias not allowed" when using destring

    Hi everyone,

    I'm having an issue while using destring on a pretty standard "year" variable. Here's my code and my attempt at debugging:
    Code:
    . desc date
    
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    date            str13   %13s                  Date
    
    . charlist(date)
     -0123456789Ao��
    
    . ret li
    
    macros:
                  r(chars) : "-0123456789Ao��"
               r(sepchars) : "- 0 1 2 3 4 5 6 7 8 9 A o � � "
                  r(ascii) : "32 45 48 49 50 51 52 53 54 55 56 57 65 111 177 195 "
    
    . replace date = subinstr(date,"`=char(177)'","",.)
    (10 real changes made)
    
    . replace date = subinstr(date,"`=char(195)'","",.)
    (10 real changes made)
    
    . charlist(date)
     -0123456789Ao
    
    . recast str13 date
    
    . destring date, force replace
    alias not allowed
    r(101);
    
    end of do-file
    
    r(101);
    Does anyone know why I'm having this error? I used the force option because there are some nonnumeric characters in the date variable, but this error pops up no matter how much cleaning I do.

    Thanks!

  • #2
    Thanks for the clear exposition of what you have tried to do. But without example data that produces this result, troubleshooting is generally futile.

    A couple of things come to mind. First, it is almost always a bad idea to use -force- options. You succeeded in cleaning out the non-printing characters from your variable. You could do the same with the remaining A and o, or you could use the -ignore("Ao")- option instead. This would be a safer way to proceed.

    But that probably has nothing to do with the error you are getting. The error message refers to an alias. So unless there is something truly opaque going wrong here, I would guess that the variable you are trying to -destring- is an alias of a variable in another frame. And I guess -destring- doesn't allow that? I'm not really sure about this*: aliases are new in Stata 18 and, in truth, I have yet to use them at all. But if this variable is, indeed, an alias, my thought would be either to -frget- the variable directly, rather than as an alias (or -frunalias- it), and then -destring- it, or, if it's really important to have this variable be an alias, then do the -destring-ing in the frame where the variable originated, and either -frget- or -fralias add- the resulting numeric variable.

    If this doesn't help you solve your problem, I recommend posting back with example data that reproduces your problem. Please use the -dataex- command to do that. (See -help dataex- if you are not familiar with it.)

    *Stata does not allow operations that change the values of alias variables. -destring- meets this description in the sense that it actually destroys the variable and creates a new, related one, with the same name, location in the data set, and variable label. So I would think that you cannot -destring- an alias.
    Last edited by Clyde Schechter; 18 Feb 2024, 15:37.

    Comment


    • #3
      Thank you Clyde, and sorry for the omission of the data example. Unfortunately dataex also doesn't work with my dataset (same error), but I've solved the issue. I'll share what the problem was since it appears this is quite new in Stata and I too am unfamiliar with aliases. In a nutshell, I had another variable named "alias" (as in nickname) from the raw data; this seemed to throw Stata off since changing the variable name directly in the CSV completely solved my issues.

      Seems like a very dumb mistake on my part, but at least I can keep working now!

      Comment


      • #4
        destring should turn date into a regular variable if it were an alias variable. However, the describe command shows that date is a str13 (not written in italics); also, the replace commands do not choke, which they would on alias variables. Thus, date is probably not an alias variable.

        The error message would be created if you tried to destring a variable with the name alias. Or, if you did one of many other things, like tabulate, summarize, etc. a variable with the name alias. So either the code in #1 does not represent what has been typed, exactly or something is going on under the hood of destring that I could not quickly identify.

        The only advice I can give is: if you have a variable named alias, rename it. Up-to-data Stata will let you do that.

        Comment


        • #5
          The code was copy pasted directly from the output, but indeed I did have a variable named alias. Simply renaming the variable worked, thank you!

          Comment


          • #6
            Originally posted by Andres Santos Vargas View Post
            I'll share what the problem was since it appears this is quite new in Stata and I too am unfamiliar with aliases. In a nutshell, I had another variable named "alias" (as in nickname) from the raw data; this seemed to throw Stata off
            Meaning #3 confirms my suspicion in #4.

            Originally posted by Andres Santos Vargas View Post
            Seems like a very dumb mistake on my part
            Nope. It's not dumb; and, it's not your mistake alone. There is a similar issue with datasets from older versions of Stata that had been overlooked by StataCorp when they introduced alias variables, making alias a reserved word. Also, Stata's "feature" of loading datasets that don't meet the standards (such as having valid Stata variable names) without complaining has been discussed on Statalist before. I guess it's not super high priority, and it is probably easy to make things worse, but I feel Stata should at least warn you if a dataset has invalid variable names.

            Comment

            Working...
            X