0
votes

I use Stata since several years now, along with other languages like R. Stata is great, but there is one thing that annoys me : the generate/replace behaviour, and especially the "... already defined" error.

It means that if we want to run a piece of code twice, if this piece of code contains the definition of a variable, this definition needs 2 lines :

capture drop foo
generate foo =  ...

While it takes just one line in other languages such as R.

So is there another way to define variables that combines "generate" and "replace" in one command ?

1
From my point of view, Stata is protecting the integrity of your data. The additional check is a small price to pay.Roberto Ferrer
This hasn't been a problem for me, and perhaps for others, because I write my code in do-files that I run from the top, starting by clearing and reloading the data. I can see the issue when debugging by selecting a group of lines and running, editing, and rerunning until they're correct, then moving on to a subsequent group of lines. But because the group of lines are run from within a temporary do-file, local macros aren't preserved at the end of the test for use by the next group of lines submitted. This means I only rarely find it useful to submit code piece by piece.user4690969
Yes I agree that Stata is designed to run whole do-file and not pieces of do-files, the local macro is a good example, and so is the "set more off" command. I guess I should just adapt my behaviour. The problem is that some command take very long time to execute, so debugging the code after such commands is not convenient.Victor

1 Answers

0
votes

I am unaware of any way to do this directly. Further, as @Roberto's comment implies, there are reasons simply issuing a generate command will not overwrite (see: replace) the contents of a variable.

To be able to do this while maintaining data integrity, you would need to issue two separate commands as your question points out (explicitly dropping the existing variable before generating the new one) - I see this as method in which Stata forces the user to be clear about his/her intentions.

It might be noted that Stata is not alone in this regard. SQL Server, for example, requires the user drop an existing table before creating a table with the same name (in the same database), does not allow multiple columns with the same name in a table, etc. and all for good reason.

However, if you are really set on being able to issue a one-liner in Stata to do what you desire, you could write a very simple program. The following should get you started:

program mkvar
version 13

    syntax anything=exp [if] [in]

    capture confirm variable `anything'

    if !_rc {
        drop `anything'
        }   

   generate `anything' `exp' `if' `in'

end

You then would naturally save the program to mkvar.ado in a directory that Stata would find (i.e., C:\ado\personal\ on Windows. If you are unsure, type sysdir), and call it using:

mkvar newvar=expression [if] [in]

Now, I haven't tested the above code much so you may have to do a bit of de-bugging, but it has worked fine in the examples I've tried.

On a closing note, I'd advise you to exercise caution when doing this - certainly you will want to be vigilant with regard to altering your data, retain a copy of your raw data while a do file manipulates the data in memory, etc.