Announcement

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

  • Issue with cap drop?

    Hello,

    I have some script that I need to be able to re-run many times. The script creates some variables as it goes. I don't want to manually delete these variables before I run the script. I thought I could use capture drop - however there appears to be an issue:

    I have two variables: age and agesex. I want to drop agesex at the beginning of the script. However, if agesex is not present in the data (the data might be refreshed so the variables created in the script, have not been created) the variable age is dropped. I am guessing this is because Stata thinks that age is an abbreviation of agesex.

    Has anyone come across this before, and if so does anyone know how to go about addressing this issues in the code.

    Ideally, I want something like "if exists" that you can use in SQL.

  • #2
    A few things here. You can avoid the problem by not using a variable abbreviation, and just say -cap drop agesex-. I would think you are just using age, which would normally be ambiguous if both variables are present.

    Next, -agesex- sounds like a manually created interaction term between age and sex. If that's the case, and you will be running regression models with that term, it's better to use the factor variable notation to let Stata do that calculation for you in the background. It's also advantageous for post-estimation as it will correctly take into account the relevant variance components of age and sex.

    Third, it may be a better strategy to define these variables and save them with your analysis datasets, rather than to create and destroy them on the fly. You don't give enough details to know if that's meaningful or practical, but it's something to consider, as the current approach seems error prone.

    For some helpful commands like checking the existence or type of a variable, you may consider -ds- and -confirm-. You can read the documentation to find out more.

    Comment


    • #3
      If you want to drop agesex but not age, then the command should be -capture drop agesex-. If you want to drop both age and agesex, then you should have -capture drop age*- or, perhaps more specifically, --capture drop age agesex-. If you want to sometimes drop age, sometimes drop agesex, and sometimes drop both, then you need to spell out the specific conditions under which each applies and use -if- commands (not if options) to restrict them accordingly.

      Perhaps I do not understand what you are asking?

      Comment


      • #4
        The replies above are all very sensible and should be taken on board.

        However, to answer your question literally: the "if exists" logic can be achieved in Stata using confirm together with capture:
        Code:
        cap confirm variable agesex, exact
        if _rc==0 {
           drop agesex
        }
        What capture does, other than over-riding the usual behaviour that an error will causing the script to stop, is to capture and store the "return code" of the command it applies to. If the command does not issue an error, the return code is zero; if there is an error, the return code will be some positive (non-zero) integer. So the code fragment above is:

        - confirm whether a variable named "agesex" exists in memory (the option ", exact" means, a variable named PRECISELY "agesex", no more and no less)
        - if it exists (i.e. return code is zero), drop it from memory.

        Thanks,
        David.

        Comment


        • #5
          Just to clarify, I want to drop agesex if that variable exists. However, even though I have written cap drop agesex, when agesex is not present in the data, for some reason the variable age is being dropped, which I do not want to happen. I am not sure why this is happening, and it seems to only do it when I run the script as a whole not when I run it line by line

          Comment


          • #6
            even though I have written cap drop agesex, when agesex is not present in the data, for some reason the variable age is being dropped
            That sounds weird. I have never encountered it.

            it seems to only do it when I run the script as a whole not when I run it line by line
            This suggests that the problem has something to do with incorrect use of local macros. Local macros go out of scope in line-by-line running of a do-file, so, for all intents and purposes, they don't even exist. It is hard to imagine just what is going on. But if you post the script and some example data (use -dataex-) that exhibits the problem you are having, probably it can be figured out.

            Comment

            Working...
            X