Announcement

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

  • For loop over varlist -replace

    Hello
    I am trying to run a for loop over a varlist where I want to replace an observation of a variable with the subsequent one if the value of the original observation is 0.
    eg:

    replace mv1=mv2 if mv1==0
    replace mv2=0 if mv1==mv2

    Now since I have mv1-mv24, I tried a for loop. Since I am new to for loop and STATA I didn't know exactly what to find and where. My code is below:

    foreach var of varlist mv3-mv24 {
    replace `var' = `var'[_n+1] if `var'==0
    replace `var'[_n+1] = 0 if `var' = `var'[_n+1]
    }
    But I do not get what I want. Could someone help me?
    Thank You
    Sayli J

  • #2
    Hi Sayli,

    _n refers to the observation number for an individual. I don't believe your code will execute, but in sentiment, you would be replacing an individuals value with value for the next observation, which I don't believe is your intent.

    For any specific individual you want to replace their value for mv1 with the value for mv2, and so forth.

    For this type of loop, check out the forval command in Stata. This will allow you to loop over values for variables with the same base name, but with numerical distinction.

    Comment


    • #3
      So for example you want
      Code:
      1 2 0 4 5
      to become
      Code:
      1 2 4 5 0
      Your sample code, if it worked the way you intended, will removed duplicated values as well, and
      Code:
      1 2 2 4 5
      will become
      Code:
      1 2 4 5 0
      Perhaps the following (untested) code will accomplish what you want.
      Code:
      generate temp = .
      forvalues i=1/23 {
      local j = `i'+1
      replace temp = mv`i'
      replace mv`i' = mv`j' if temp==0
      replace mv`j' = 0     if temp==0
      }
      drop temp
      I have many reservations about the organization of your data, and suspect that you may do better by reshaping it from the apparent wide layout it currently is in to a long layout using the reshape command. The experienced users here generally agree that, with few exceptions, Stata makes it much more straightforward to accomplish complex analyses using a long layout of your data rather than a wide layout of the same data. Certainly this current problem would not have been as tricky.

      Comment


      • #4
        Thank You @William Lisowski and @Matt Warkentin.
        @William Lisowski the code you suggested worked and I understood it too. I agree that STATA is better with long layout however I needed the data in wide to use the nwcommands for network analysis.

        Comment


        • #5
          Sayli Javadekar The code I provided was not how I would have approached the problem, but my intent was to show how to modify your code suitably. But in creating it, I realized, and worked around, the problem I described in post #3. Unfortunately, I did not realize that your code, and mine, has the problem that
          Code:
          1 2 0 0 5
          would become
          Code:
          1 2 0 5 0
          Fixing that within the framework of the looping code is awkward. Instead, here is the code I would use for this situation.
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(id mv1 mv2 mv3 mv4 mv5)
          101 1 2 0 4 5
          102 1 2 2 4 5
          103 1 2 0 0 5
          end
          reshape long mv, i(id) j(j)
          replace j = j+25 if mv==0
          bysort id (j): replace j = _n
          reshape wide mv, i(id) j(j)
          list, clean
          which when run yields
          Code:
          . reshape long mv, i(id) j(j)
          (note: j = 1 2 3 4 5)
          
          Data                               wide   ->   long
          -----------------------------------------------------------------------------
          Number of obs.                        3   ->      15
          Number of variables                   6   ->       3
          j variable (5 values)                     ->   j
          xij variables:
                                  mv1 mv2 ... mv5   ->   mv
          -----------------------------------------------------------------------------
          
          . replace j = j+25 if mv==0
          (3 real changes made)
          
          . bysort id (j): replace j = _n
          (6 real changes made)
          
          . reshape wide mv, i(id) j(j)
          (note: j = 1 2 3 4 5)
          
          Data                               long   ->   wide
          -----------------------------------------------------------------------------
          Number of obs.                       15   ->       3
          Number of variables                   3   ->       6
          j variable (5 values)                 j   ->   (dropped)
          xij variables:
                                               mv   ->   mv1 mv2 ... mv5
          -----------------------------------------------------------------------------
          
          . list, clean
          
                  id   mv1   mv2   mv3   mv4   mv5  
            1.   101     1     2     4     5     0  
            2.   102     1     2     2     4     5  
            3.   103     1     2     5     0     0
          This is yet another example where Stata makes it easier to solve a problem by working in a long rather than wide layout.

          Comment


          • #6
            Nick Cox
            I have a variable named "year" having values from 5 to 14, and I want to replace 5 by 1980; 6 by 1981; 7 by 1982; 8 by 1983 so on and so forth. How should i write a loop in this case ?

            Hear is the example:

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str18 office byte year
            "Brazil" 5
            "Brazil" 6
            "Brazil" 7
            "Brazil" 8
            "Brazil" 9
            "Brazil" 10
            "Brazil" 11
            "Brazil" 12
            "Brazil" 13
            "Brazil" 14
            end
            Last edited by Ridwan Sheikh; 12 Feb 2023, 05:39.

            Comment


            • #7
              No loop needed. Just add 1975 to all values.

              Comment


              • #8
                Thank you Nick Cox , the code (below) worked fine.
                Code:
                replace year = year+1975
                regards,
                (Ridwan)

                Comment

                Working...
                X