Announcement

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

  • how to use tempvar more efficiently?

    If I run syntax with tempvar in a block, Stata will generate variables such as __0000001 __0000002, I know if I run the whole do file, this will not become a problem.
    However, sometimes I need to run the syntax step by step and I do not want to generate these temporary variables, how to realize this?
    Thanks!

  • #2
    You can only use tempvar when the definition and the use are executed in one go. If you want to run a .do file line by line, then it will not work. You can run blocks of code that include both the definition and the use.

    I rarely use tempvars in a regular analysis (writing a .do file). For me the main advantage is that tempvars are guaranteed to not already exist in the data. This is extremely helpful when programming (writing an .ado file), but when doing an analysis I know what variables are in my data.

    So can you tell us why you think tempvars are useful in your case? If we know that, we can try to find a solution.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Originally posted by Maarten Buis View Post
      You can only use tempvar when the definition and the use are executed in one go. If you want to run a .do file line by line, then it will not work. You can run blocks of code that include both the definition and the use.

      I rarely use tempvars in a regular analysis (writing a .do file). For me the main advantage is that tempvars are guaranteed to not already exist in the data. This is extremely helpful when programming (writing an .ado file), but when doing an analysis I know what variables are in my data.

      So can you tell us why you think tempvars are useful in your case? If we know that, we can try to find a solution.
      Thanks, Maarten! In my case, I need to calculate some intermediate variables which I will not use when run regressions, such as the mean, the count. I do not want to keep it in my dataset in order to keep the dataset clean.

      Comment


      • #4
        I typically separate the data preparation step from the analysis step (in separate .do files). In the data preparation .do files I typically start by loading the raw data followed by throwing away all variables I don't need for that single .do file. I make as many intermediate variables I want. The important thing is to keep the .do file readable. At the end of the .do file I only keep the variables I want to keep and store the dataset (of course not overwriting the raw data)
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Originally posted by Maarten Buis View Post
          I typically separate the data preparation step from the analysis step (in separate .do files). In the data preparation .do files I typically start by loading the raw data followed by throwing away all variables I don't need for that single .do file. I make as many intermediate variables I want. The important thing is to keep the .do file readable. At the end of the .do file I only keep the variables I want to keep and store the dataset (of course not overwriting the raw data)
          During the analysis step, we may still need to calculate some intermediate variables, sometimes, too many, I think.

          Comment


          • #6
            You can drop them after you are done with them.

            Code:
            tempvar foo
            gen `foo' = something nice
            is as many lines of code as

            Code:
            gen foo = something nice
            drop foo
            ---------------------------------
            Maarten L. Buis
            University of Konstanz
            Department of history and sociology
            box 40
            78457 Konstanz
            Germany
            http://www.maartenbuis.nl
            ---------------------------------

            Comment


            • #7
              Originally posted by Maarten Buis View Post
              You can drop them after you are done with them.

              Code:
              tempvar foo
              gen `foo' = something nice
              is as many lines of code as

              Code:
              gen foo = something nice
              drop foo
              Haha, I just want to omit the drop step, for tempvar, we can also use drop ___*

              Comment


              • #8
                Originally posted by Maarten Buis View Post
                For me the main advantage is that tempvars are guaranteed to not already exist in the data.
                Unfortunately, that is not true.

                Code:
                . clear
                
                . generate __000000 = 42
                
                . tempvar tmp
                
                . generate `tmp' = 73
                variable __000000 already defined
                r(110);
                
                end of do-file

                Comment


                • #9
                  Originally posted by daniel klein View Post

                  Unfortunately, that is not true.

                  Code:
                  . clear
                  
                  . generate __000000 = 42
                  
                  . tempvar tmp
                  
                  . generate `tmp' = 73
                  variable __000000 already defined
                  r(110);
                  
                  end of do-file
                  Haha, after all, no one will name variable to " __000000"

                  Comment

                  Working...
                  X