Announcement

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

  • Replace multiple variables at the same time but with different values for each variable?

    Hi. Is it possible to do different kinds of replacements for multiple variables at the same time? For example, I'd like to tell Stata something like: Replace Var1=1, Var2=2, Var3=3 if Var4=="X".
    It doesn't seem that recode would work because with recode I'd have to specify, Var1 (5=1), etc., while I want to be able to do this across several variables without having to double-check which values I'm actually replacing. I hope that makes sense. Just looking to save a little time instead of having to run a bunch of replace commands. Thank you!

    -Marcos

  • #2
    Well, if your variables are really named Var1, Var2, and Var3, and you want to replace them with 1, 2, and 3 respectively, your solution is just

    Code:
    forvalues i = 1/3 {
        replace Var`i' = `i' if Var4 == "X"
    }
    But I'm guessing you'll respond that you have some more complicated situation to deal with.

    Comment


    • #3
      Thanks! I don't know really know how to use this programming language so I'm having trouble applying the basic structure you outlined. The Var1, Var2, etc. above is really m9, tu9, w9, sa9, su9 in the below case. I'd like to tell stata to replace m9, tu9 and w9 with 15; and replace sa9 and su9 with 30, IF topcode=="PB MF 29" | topcode== etc.

      | topcode m2 m4 m9 tu2 tu4 tu9 w2 w4 w9 th2 th4 th9 f2 f4 f9 sa2 sa4 sa9 su2 su4 su9 |
      |-------------------------------------------------------------------------------------------------------------------------------------
      40. | PB MF 29 12 85 0 12 72 0 12 74 0 12 120 30 12 200 30 12 205 0 12 120 0 |


      I can't seem to paste the above in here with the formatting that properly spaces the values & variable names but hopefully you get the idea. Thanks again.
      Last edited by chijuban; 29 Jun 2014, 19:45.

      Comment


      • #4
        I believe that when you write "at the same time", you mean "in one line". The answer is "No, not really". You must do it the hard way (but copy-and-paste may save time):

        Code:
        replace m9 = 15 if topcode=="PB MF 29"
        replace tu9 = 15 if topcode=="PB MF 29"
        replace w9 = 15 if topcode=="PB MF 29"
        replace sa9 = 30 if topcode=="PB MF 29"
        replace au9 = 30 if topcode=="PB MF 29"
        If you have many such corrections, you might benefit from studying the merge command and its update and replace options.


        Comment


        • #5
          Thanks. Yes, I figured I would have to do it the hard way, line by line, but I wanted to check anyway. I'll look into your suggestions.

          Comment


          • #6
            I had a similar problem recently. I generated a new variable e.g. code=1 if a there were a combination of results for specific variables. In your case, I guess it would be generate code=1 if topcode=="PB MF 29". You would then do all of the replacements and then drop the new variable. Mine looked as follows:

            generate code=1 if E251==1 & E2531==0 & E2532==0 & E2533==0 & E2534==0 & E2535==0 & E2536==0 & E2537==0 & E2538==0 & E2539==0 & E25310==0 & E25311==0 & suspectonlyj88==0
            replace E251=0 if code==1
            replace E2521=. if code==1
            replace E2522=. if code==1
            replace E2531=. if code==1
            replace E2532=. if code==1
            replace E2533=. if code==1
            replace E2534=. if code==1
            replace E2535=. if code==1
            replace E2536=. if code==1
            replace E2537=. if code==1
            replace E2538=. if code==1
            replace E2539=. if code==1
            replace E25310=. if code==1
            replace E25311=. if code==1
            drop code

            Comment


            • #7
              Code:
              local cond  E251==1 & E2531==0 & E2532==0 & E2533==0 & E2534==0 & E2535==0 & E2536==0 & E2537==0 & E2538==0 & E2539==0 & E25310==0 & E25311==0 & suspectonlyj88==0
              
              replace E251=0 if `cond' 
              
              foreach s in 21 22 31 32 33 34 35 36 37 38 39 310 311  { 
                    replace E25`s' = . if `cond' 
              }

              Comment


              • #8
                Hi, i am facing the same problem.If i have 5 variables "ab", "cd", "ef", "gh", "jk" and another variable "lm"and I want to create other 5 variables "100ab", "100cd", "100ef", "100gh", "100jk"which are derived by dividing the values of the original variables by lm, is it possible to achieve this with a single command instead of recoding each variable separately and generating another variable?
                thank you
                Last edited by Wossenseged Jemberie; 18 Apr 2018, 15:17. Reason: Changed the variable name

                Comment


                • #9
                  You cannot create variables with names like 100ab, 100cd, etc. because those are illegal variable names in Stata. All Stata variable names must begin with a letter or an underscore (_) character. So let's create, instead, ab100, cd100, etc.

                  Code:
                  foreach v of varlist ab cd ef gh jk {
                      gen `v'100 = `v'/lm
                  }

                  Comment


                  • #10
                    Thanks very much Clyde. This was really helpful. one more thing, I think I need to start reading about loops and macros for Stata. Would you give me good suggestion to start with?

                    Comment


                    • #11
                      For understanding Stata macros, read section 18 of the User's Guide [U] volume of the PDF documentation that comes with your Stata installation.

                      As for looping, most loops in Stata are controlled by -foreach-. Run -help foreach- and then click on the link to the PDF documentation for that chapter. Bear in mind that many things that, in other language, would be done using a loop, can be done without loops in Stata. So you must also be familiar with -by:- (Start witih -help by- and click on the lnk to the PDF documentation for that chapter.) There is another loop-controlling structure in Stata, -while-, but it is rarely used nowadays, so you could put off learning about that one until you come upon a situation that really requires it.

                      More generally, as you apparently did not know about the restriction on variable names, I'm assuming you are relatively new to Stata. The entire User's Guide volume is well worth reading. It will give you a good feel for Stata's approach to data management and analysis and an overview of the commonly used commands.

                      Comment


                      • #12
                        Thanks a lot

                        Comment

                        Working...
                        X