Announcement

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

  • multiple foreach for replacing value

    Hi,

    I want to use 2 foreach. Basically, I want to get age of the specific person(self_age) in (varlist1) from the varlist2 of the specific person (var1==1).

    But, I am not getting all the age in the rows. Is the multiple foreach working?

    gen self_age=.
    foreach var1 of varlist age_a age_b age_c age_d {
    foreach var2 of varlist name_code_a name_code_b name_code_c name_code_d {
    replace self_age= `var1' if `var2'==1
    }
    }


    The problem is it is replacing the age of ONLY "age_d" and "name_code_d" and others are replacing as ".". How can I can fix it not replacing the first ages.
    Last edited by Rezoanul Hoque; 30 Apr 2021, 10:44.

  • #2
    Off-topic here (nothing to do with Mata) but my guess is that may be closer to what you need:


    Code:
    gen self_age=.
    
    foreach x in a b c d { 
        replace self_age = age_`x' if name_code_`x'==1
    }

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Off-topic here (nothing to do with Mata) but my guess is that may be closer to what you need:


      Code:
      gen self_age=.
      
      foreach x in a b c d {
      replace self_age = age_`x' if name_code_`x'==1
      }
      Actually, I am giving an example of a b c d. but in my case, the variable names for
      age : B_1_5_01 B_1_5_02 B_1_5_03 B_1_5_04 B_1_5_05 B_1_5_06 B_1_5_07 B_1_5_08 B_1_5_09 B_1_5_10 B_1_5_11 B_1_5_12 B_1_5_13 B_1_5_14 B_1_5_15
      names of the persons: B_1_4_1_ B_1_4_10 B_1_4_11 B_1_4_12 B_1_4_13 B_1_4_14 B_1_4_15 B_1_4_16 B_1_4_17 B_1_4_18 B_1_4_19 B_1_4_00 B_1_4_01 B_1_4_02 B_1_4_03 B_1_4_2_ B_1_4_20 B_1_4_21 B_1_4_22 B_1_4_23 B_1_4_24 B_1_4_25 B_1_4_26 B_1_4_27 B_1_4_28 B_1_4_29 B_1_4_04 B_1_4_05 B_1_4_06 B_1_4_07

      age vars are not in equal number as names.

      Comment


      • #4
        I count 15 age variables and 30 name variables. With some guidance on how they relate to each other, I don't see how we can help you further.

        Comment


        • #5
          The first comment made by Nick is that you posted in the wrong place. Your problem is in Stata while this is for Mata. Please post your question in right forum.
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment


          • #6
            Originally posted by Maarten Buis View Post
            The first comment made by Nick is that you posted in the wrong place. Your problem is in Stata while this is for Mata. Please post your question in right forum.
            ok noted. im posting on mata.

            Comment


            • #7
              Originally posted by Nick Cox View Post
              I count 15 age variables and 30 name variables. With some guidance on how they relate to each other, I don't see how we can help you further.
              Thanks Nick. I think 🤔 I'm doing some mistake somewhere. Your idea will work if I had all the a b c d.

              Comment

              Working...
              X