Announcement

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

  • Resetting Different Locals for each loop


    Hello,

    I am trying to create a loop using the following data. The 'destination', 'originau' variables, and oldau varibales are all area unit nmbers(geographic units in the analysis). I have to assign each id a new destination, based on the random numbers corresoinding to each i.d.

    For example,
    For i.d. 16, random number is 0.172498. The corresponding oldau is 505805. Thus, I have to compare the random number (0.172498) to the values in column originau505805 as the oldau for id 16 is 505805. Now, the result value I need to get is the value in the destination column, which contains the value immediately higher than the random number compared. In this case, the answer should be 99 (1 in column originau505805 is the next highest number after 0.172498).


    Again,
    for id. 22, random number 0.2044 has to be compared with column originau505300 as corresponding oldau is 505300. The answer I should get is 505500 or 505601.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long destination float(originau505300 originau505400 originau505500 originau505601 originau505805) long oldau byte newau float randomnumber byte id
    505300       0       0  .15748      0 .003846 505805 0 .172498 16
    505400       0       0  .19685      0 .003846 505805 0 .940553 17
    505500  .42235 .130435  .19685      0 .015385 505805 0 .904427 18
    505601  .42235 .130435  .19685      0 .015385 505805 0 .700794 19
    505805 .633803 .130435 .066929 .18797 .042308 505805 0 .678745 20
        99       1       1       1      1       1 505805 0 .960303 21
         .       .       .       .      .       . 505300 0 .204456 22
         .       .       .       .      .       . 505400 0  .65734 23
         .       .       .       .      .       . 505400 0 .338311 24
         .       .       .       .      .       . 505400 0 .219149 26
    end


    The code I am trying works for each combinations of id and AU seperately. But I am unable to put the command in a loop.

    [forvalues id=16/26{
    local var 505805
    if inlist(id, 22){
    local var 505300
    }
    if inlist(id, 23,24,26){
    local var 505400
    }
    summ randomnumber if id==`id'
    gen answer_`id'= destination if originau`var'> r(min)
    }
    CODE][/CODE]

    I am setting the local value separately in the loop for the different id and oldau combinations, however, Stata is taking the local value origin505805 and not the other one. I must be missing something. Any help will be appreciated.

    Thank you.

  • #2
    This appears to be what you want.

    Code:
    forvalues id=16/26 {
    
        if `id' == 22 local var = 505300
        else if inlist(`id', 23,24,26) local var = 505400
        else local var = 505805
    
        summ randomnumber if id==`id'
        gen answer_`id'= destination if originau`var'> r(min)
    
    }
    Note that you are getting confused between your local name and your variable name. It is not good programming style to have names so easily confused.

    As explained in https://www.stata.com/support/faqs/p...-if-qualifier/ the if command and the if qualifier are quite different. For example,

    Code:
    if inlist(id, 22)
    will be interpreted as

    Code:
    if inlist(id[1], 22)
    which is not what you want. And your other commands of the same kind all suffer from the same problem.

    Comment


    • #3
      Thank you so much. This works!
      According to your suggestion, I did look into the if command and qualifier, and understood what was happening with the command I was using.

      Comment

      Working...
      X