0
votes

I have many variables whose name starts with I: followed by a number n (like I:2390). These are indicator variables, with value 1 for observation n. I imported this database from other software (Oxmetrics), so they were not created in Stata. According to the Stata Manual, only letters, numbers and _ are allowed for variable names. Thus, Stata doesn't allow character : within names. Hence, I cannot run regression on them (output is I ambiguous abbreviation r(111);).

I'm trying to rename them all, but so far nothing works, perhaps because the name is invalid. For example, I tried:

rename I:2390 OUT2390

rename I# OUT#

replace I:2390 = subinstr(I:2390, "I:", "OUT", .)

The output is the same as before: I ambiguous abbreviation r(111);

Here is the output from describe, related to the specific variables in question:

I:7             double                        
I:32            double                        
I:41            double                        
I:77            double                        
I:173           double                        
I:221           double                        
I:264           double                        
I:297           double                        
I:977           double                        
I:980           double                        
I:1026          double                        
I:1232          double                        
I:1282          double                        
I:1288          double                        
I:1347          double                        
I:1429          double                        
I:1455          double                        
I:1508          double                        
I:1536          double                        
I:1709          double                        
I:1774          double                        
I:1805          double                        
I:1836          double                        
I:1851          double                        
I:1873          double                        
I:1918          double                        
I:1967          double                        
I:1980          double                        
I:2016          double                        
I:2020          double                        
I:2108          double                        
I:2109+I:2108   double                        
I:2128          double                        
I:2164          double                        
I:2173          double                        
I:2256          double                        
I:2286          double                        
I:2332          double                        
I:2345          double                        
I:3109+I:3108   double                        
I:3151          double                        
I:3211          double                        
I:3250          double                        
I:3582          double                        
I:3596          double                        
I:3604          double                        
I:3636          double                        
I:3656          double                        
I:3716          double                        
I:3718          double                        
I:3760          double                        
I:4139          double                        
I:4164          double                        
I:4172          double                        
I:4230          double                        
I:4257          double                        
I:4263          double                        
I:4282          double                        
I:4288          double                        
I:4316          double                        
I:4317-I:4316   double                        
I:4346          double                        
I:4358          double                        
I:4445          double                        
I:4644          double                        
I:4645+I:4644   double                        
I:4657          double                        
I:4679          double                        
I:4705          double                        
I:4720          double                        
I:4866          double                        
I:4899          double                        
I:4923          double                        
I:5329          double                        
I:5423          double                        
I:5717          double                        
I:5731          double                        
I:5776          double                        
I:5960          double                        
I:6103          double                        
I:6117          double                        
I:6130          double                        
I:6178          double                        
I:6182          double                        
I:6341          double                        
I:6424          double                        
I:6435          double                        
I:6441          double                        
I:6470          double                        
I:6555          double                        
I:6567          double                        
I:6668          double                        
I:6695          double                        
I:6878          double                        
I:7341          double                        
I:7349          double                        
I:7370          double                        
I:7374          double                        
I:7419          double                        
I:7449          double                        
I:7555          double                        
I:7574          double                        
I:7719          double                        
I:7723          double                        
I:7729          double                        
I:7746          double                        

Is there a way to solve this in Stata, or do I need to use other software?

2
This makes no obvious sense. The colon : is not allowed in Stata variable names. Even if it were, rename would be the way to change them; replace is for changing values of variables, not their names. See stata.com/manuals14/u.pdf If Stata allowed these variable names on input, they should not be forbidden for regression. Perhaps you are referring to something to do with Unicode. I'd recommend display of e.g. describe output to show what is true of your dataset. For example, you may be confusing variable names and labels.Nick Cox
There may be a very simple resolution to this, but a detailed answer depends on more input.Nick Cox
Note that your own example is further confused, as you appear to be trying to replace I- not I:. As said, we need to see enough describe output to see what the problem is.Nick Cox
Added output from describe. I- was a mistake (amended). I am not confusing names and labels.luchonacho
Most of the time, this is related to buggy foreign implementations of Stata dataset standards, potentially mixed with some Stata bug. See for example statalist.org/forums/forum/general-stata-discussion/general/….Roberto Ferrer

2 Answers

1
votes

The problem with the previous solution (exporting as .csv) is that you might lose the variables' labels. A possible solution is to replace all the illegal characters of your variables' names with an underline, using the code below:

foreach varname of varlist * {

local i = `i' + 1

if ( "`varname'" != ustrtoname("`varname'") ) {

    mata : st_varrename(`i', ustrtoname("`varname'") )
   }
}
0
votes

I cannot make sample data with variable name containing a colon. Try:

  1. export to csv

    outsheet using EXPORTED_FILE.csv
    
  2. modify variable name in text editor

  3. import back to Stata.