Announcement

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

  • forval loop with multiple replace command

    Good day,

    I have the following loop:


    Code:
    forval j = 1/10 {
        
    replace E_FACH1 = ENG`j' if A120_0`j' == 2 & A120 = 1    
    replace E_FACH2 = ENG`j' if A120_0`j' == 2 & A120 = 1    
    replace E_FACH3 = ENG`j' if A120_0`j' == 2 & A120 = 1    
        
    }
    Basically I want the Variable E_FACH1 (which is empty at that point) the get the Value of ENG`j', when the variable A120_0`j' has the value 2. If that is the case, the second replace line should run, but without the `j' which equaled 2 in the first replace command (otherwise all Variables E_FACH would have the same value). That is obviously not the right Code for it, but I am struggling to find a way to do it.

    Any help is appreciated- if you do not understand my question, let me know. I'll try to clarify it.

    Best,
    Marius

  • #2
    This is confusing. There are 10 different values for A120_0`j'. Which one do you want to test for having the value "2"? Or do you want to make the assignment if any of the 10 equal 2? What if more than one equals 2?

    Comment


    • #3
      Fair enough, I try to be more specific. I want to test all of them for the value 2. If for example the variable A120_04 == 2, then the value of ENG4 should be saved in the variable E_FACH1. Up to here it is easy. But if there is a second A120_0`j' Variable with the Value 2, it should be saved in the Variable E_FACH2. For each Observation there can be a maximum of three A120_0`j' variables with the Value 2. Not sure if that is helpful or more confusing...

      Comment


      • #4
        This is confusing. There are 10 different values for A120_0`j'. Which one do you want to test for having the value "2"? Or do you want to make the assignment if any of the 10 equal 2? What if more than one equals 2?

        Comment


        • #5
          Something along the lines of (untested):

          Code:
          forvalues  k=1/3 {
              forvalues j=1/10 {
                   replace E_FACH_`k'=ENG`j' if A120_0`j'==2&A120=1
               }
          }

          Comment

          Working...
          X