Announcement

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

  • Repeat code until 0 real changes made?

    Hi, there is a similar thread on this question, but it doesn't make sense for my case. I have filled in choice experiment data to create observations for the alternatives not chosen by the respondent and need to copy down the values of certain other variables within groups of choice sets and by person. I have 2 lines of code that work to do this and need to be repeated ~5 times to catch all the changes. It works fine manually but I want to know if there's a simpler way to code it to repeat until r(N)=0.

    Here is the code:
    bys hhid choiceset: replace choicecode=choicecode[_n-1] //
    if (choicecode==. & hhid==hhid[_n-1] & choiceset==choiceset[_n-1])
    bys hhid choiceset: replace choicecode=choicecode[_n+1] //
    if (choicecode==. & hhid==hhid[_n+1] & choiceset==choiceset[_n+1])

    I know one way is to use forval and just tell it to automatically run 5 times, but that is the same as manual and it is taking a long time to run this code and might be faster if I tell it to stop whenever there are no more changes to make.

    Thanks so much,
    Kate

  • #2
    I doubt that you need to do this in a loop. Why not back up and give a data example? This just seems a basic interpolation problem. mipolate (SSC) is an otherwise unpredictable search term for this forum to find some related posts.

    Something like this may be enough:

    Code:
    egen wanted = mean(choicecode), by(hhid choiceset) 

    Comment


    • #3
      Thanks Nick! I already have the variable so i don't want to use egen, 5 of 6 observations are missing and 1 has a value that is 0 1 or 2. Would the following work:

      bys hhid choiceset: replace choicecode=min(choicecode) if (choicecode==. & hhid==hhid[_n-1] & choiceset==choiceset[_n-1])

      Comment


      • #4
        Ah, now I see, your solution totally works by creating a new variable. Much simpler, thank you!!

        Comment


        • #5
          No; that won't work for several different reasons.

          You can simplify your code down to

          Code:
          bys hhid choiceset: replace choicecode=min(choicecode) if choicecode==.
          as within groups defined by hhid choiceset adjacent observations are necessarily identical on those variables.

          But

          1. min() as used here is not legal as it's an egen function and can't be used this way as an argument to generate.

          2. There is a function min() but it doesn't do what you want either.

          3. By selecting the observations with missing choicecode you are at the same time prohibiting Stata from looking at the others, which you need to do.

          What I am implying in #2 is one command to get a replacement value and one command to use it to replace. That's got to be simpler than a loop. But from what you say there is an even simpler solution:

          Code:
          bysort hhid choiceset (choicecode);  replace choicecode = choicecode[1]
          EDIT: Crossed with #4.
          Last edited by Nick Cox; 30 Aug 2018, 10:35.

          Comment


          • #6
            That should be a colon

            Code:
            :
            not a semi-colon

            Code:
            ;
            in #5.

            Comment


            • #7
              thanks again so much! I went with the egen code and it works great!

              Comment

              Working...
              X