Announcement

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

  • Ambiguous abbreviation in a loop problem

    Hello all,

    i have this code,

    forvalues i=97(2)99{;
    generate Wife`i'=.;
    generate Head`i'=.;
    };

    forvalues i=97(2)99{;
    replace Wife`i'=1 if (SN`i'==2 & (Rel`i'==20 | Rel`i'==22));
    replace Head`i'=1 if SN`i'==1 & Rel`i'==10;
    };

    My problem is that it creates the Wife97.......Head99 variables fine. But when it reaches the second loop, it says that Wife is a "Ambiguous abbreviation". I can't seem to figure out why. I tried setting the 'varabbrev off' but if I do that, in the second loop it says that the variables do not exist. How can I get around this? Thank you for your help.

  • #2
    I am not clear what is happening but a report on

    Code:
    describe Wife*
    may help.

    I am clear that you should not need two loops here. Your code collapses to

    Code:
    forvalues i = 97(2)99 {
       gen Wife`i' = 1 if (SN`i'==2 & (Rel`i'==20 | Rel`i'==22))
       gen Head`i' = 1 if SN`i'==1 & Rel`i'==10 
    }
    where the question of line delimiter is here set on one side. It is a matter of taste, only, but I find this more explicit:

    Code:
     
    foreach i in 97 99 {
       gen Wife`i' = 1 if (SN`i'==2 & (Rel`i'==20 | Rel`i'==22))
       gen Head`i' = 1 if SN`i'==1 & Rel`i'==10 
    }

    Comment


    • #3
      I'll hazard a guess: in the original code, as opposed to what was typed above, there is a space between "Wife" and "`i'" so that Stata is attempting to interpret the command
      Code:
      replace Wife `i'=1 if (SN`i'==2 & (Rel`i'==20 | Rel`i'==22));
      When I simulate that, I get the same error message.

      But in any event, Nick's elegant solution is to be preferred.

      Comment

      Working...
      X