Announcement

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

  • Replacing observations of the n+k'th variable after n is equal to 1

    Hello everyone.

    I have a problem with creating my Stata commands. I want to create commands to execute this:

    if a9x2round_1 == 1 then
    p2_2 = 1 if index_2 == 9
    p2_3 = 1 if index_3 == 9
    ...
    p2_60 = 1 if index_60 == 9


    if a9x2round_2 == 1 then
    p2_3 = 1 if index_3 == 9
    p2_4 = 1 if index_4 == 9
    ...
    p2_60 = 1 if index_60 == 9


    if a9x2round_5 == 1 then
    p2_6 = 1 if index_6 == 9
    p2_7 = 1 if index_7 == 9
    ...
    p2_60 = 1 if index_60 == 9

    Notice that when the a9x2round_n is 1, then the values of the p2_(n+k)'s must be 1 ---> when a9x2round1 is 1, the value of p2_2, p2_3, and so forth must be 1

    How do I create the commands using just one loop, if possible? Because there are numerous variables in my data, I want the codes to be short.
    Thank you very much in advance.
    Last edited by Rayinda Putri; 04 May 2022, 11:29.

  • #2
    You need to provide a data example with the relevant variables as what you explain may be clear to you but to a large degree not to others (including myself). See FAQ Advice #12 which explains how to do this using the dataex command.

    Comment


    • #3
      Dear Andrew,
      Sorry for the unclear question and here I have the data.
      Code:
      .
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str4 name byte a9x2round_1 double p2_1 byte(index_1 a9x2round_2) double p2_2 byte(index_2 a9x2round_3) double p2_3 byte(index_3 a9x2round_4) double p2_4 byte(index_4 a9x2round_5) double p2_5 byte index_5
      "Mike" 0 .5 9 0 .4 9 0 .8 9 0 .2 9 0 .5 9
      "Jim"  0 .3 9 0 .4 9 1 .5 9 0 .4 9 0 .5 9
      "Theo" 0 .3 9 1 .9 9 0 .5 9 0 .1 9 0 .2 9
      "Sam"  0 .1 9 0 .6 9 0 .5 9 0 .2 9 0 .4 9
      "Luke" 0 .8 9 0 .1 9 0 .1 9 1 .7 9 0 .3 9
      end
      for each n where n = 1,2,3,4,5 and k = 1,2,3,4
      Once a9x2round_n == 1 then all of the p2_n+k must be replaced by 1.
      For example, based on the above data,
      a9x2round_2 for Theo is equal to 1, therefore the value of p2_3, p2_4, and p2_5 of Theo must be replaced by 1,
      a9x2round_3 for Jim is equal to 1, therefore the value of p2_4 and p2_5 of Jim must be replaced by 1,
      etc.

      I want the data to look like this:
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str4 name byte a9x2round_1 double p2_1 byte(index_1 a9x2round_2) double p2_2 byte(index_2 a9x2round_3) double p2_3 byte(index_3 a9x2round_4) double p2_4 byte(index_4 a9x2round_5) double p2_5 byte index_5
      "Mike" 0 .5 9 0 .4 9 0 .8 9 0 .2 9 0 .5 9
      "Jim"  0 .3 9 0 .4 9 1 .5 9 0  1 9 0  1 9
      "Theo" 0 .3 9 1 .9 9 0  1 9 0  1 9 0  1 9
      "Sam"  0 .1 9 0 .6 9 0 .5 9 0 .2 9 0 .4 9
      "Luke" 0 .8 9 0 .1 9 0 .1 9 1 .7 9 0  1 9
      end
      Notice that in this data, all of the indexes equal 9. However, I want to be able to execute it for all other indexes as well. There are 60 indexes in my data.
      Can you please tell me what Stata commands I need to use to obtain it?

      I have tried using these commands but they didn't work:

      Code:
      local n = 4
      local k = 5
      while `n' >= 1 & `k' >= 1 & `n' < `k'        {
          while a9x2round_`n' == 1    {
          replace p2_`k' = 1 if index_`k' == 9
      }
          local n = `n'-1
          local k = `k'-1
      }
      Thank you very much in advance. I will very much appreciate your help.
      Last edited by Rayinda Putri; 04 May 2022, 18:13.

      Comment


      • #4
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str4 name byte a9x2round_1 double p2_1 byte(index_1 a9x2round_2) double p2_2 byte(index_2 a9x2round_3) double p2_3 byte(index_3 a9x2round_4) double p2_4 byte(index_4 a9x2round_5) double p2_5 byte index_5
        "Mike" 0 .5 9 0 .4 9 0 .8 9 0 .2 9 0 .5 9
        "Jim"  0 .3 9 0 .4 9 1 .5 9 0 .4 9 0 .5 9
        "Theo" 0 .3 9 1 .9 9 0 .5 9 0 .1 9 0 .2 9
        "Sam"  0 .1 9 0 .6 9 0 .5 9 0 .2 9 0 .4 9
        "Luke" 0 .8 9 0 .1 9 0 .1 9 1 .7 9 0 .3 9
        end
        
        ds p2_*
        local last= ustrregexra("`r(varlist)'", ".*\_+([^\_+]+$)", "$1")
        forval i=1/`last'{
            local j `=`i'+1'
            forval k=`j'/`last'{
                qui replace p2_`k'=a9x2round_`i' if a9x2round_`i'==1
            }
        }
        Res.:

        Code:
        . l name a9x2round_1 p2_1 a9x2round_2 p2_2 a9x2round_3 p2_3 a9x2round_4 p2_4 a9x2round_5 p2_5
        
             +------------------------------------------------------------------------------------------------+
             | name   a9x2ro~1   p2_1   a9x2ro~2   p2_2   a9x2ro~3   p2_3   a9x2ro~4   p2_4   a9x2ro~5   p2_5 |
             |------------------------------------------------------------------------------------------------|
          1. | Mike          0     .5          0     .4          0     .8          0     .2          0     .5 |
          2. |  Jim          0     .3          0     .4          1     .5          0      1          0      1 |
          3. | Theo          0     .3          1     .9          0      1          0      1          0      1 |
          4. |  Sam          0     .1          0     .6          0     .5          0     .2          0     .4 |
          5. | Luke          0     .8          0     .1          0     .1          1     .7          0      1 |
             +------------------------------------------------------------------------------------------------+
        
        .

        Comment


        • #5
          Thank you very much Andrew!!!

          Comment

          Working...
          X