Announcement

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

  • Checking previous non-missing values

    Hi all,

    So I have a list that can take any of three values, 1, 2, 3. I want to create another variable that lists the value that has come up least recently. An example of how this will look is:

    VAR1 LeastRecent
    3 .
    1 .
    2 2
    1 3
    2 3
    3 3
    3 1
    2 1
    1 1


    So far to do this I have created an intermittent variable that does not have repeats, with the idea that I would be able to check the previous two non missing values and list out the value not specified. I am hoping for some advice on an approach to have Stata check a variable for the previous non-missing values.

    Many Thanks,
    Cydney

  • #2
    That is an interesting problem. However,

    1, When the two previous values are equal, then each of the other two values is equally plausible as a candidate. Observation 8 is a case in point.

    2. Whenever one of the previous values is missing, then again two values are equally plausible. Not tackled by the code here.

    3. Whenever both previous values are missing, then all three values are equally plausible. Not tackled by the code here.


    Code:
    clear 
    input VAR1 LeastRecent
    3 .
    1 .
    2 2
    1 3
    2 3
    3 3
    3 1
    2 1
    1 1
    end 
    
    forval j = 1/3 {
        gen tot`j' = (VAR1[_n-1] == `j') + (VAR1[_n-2] == `j') 
    }
    
    gen wanted = 3 if _n > 2 
    replace wanted = 1 if tot1 == min(tot1, tot2, tot3) & _n > 2 
    replace wanted = 2 if tot2 == min(tot1, tot2, tot3) & _n > 2 
    
    list 
    
    
         +-----------------------------------------------+
         | VAR1   LeastR~t   tot1   tot2   tot3   wanted |
         |-----------------------------------------------|
      1. |    3          .      0      0      0        . |
      2. |    1          .      0      0      1        . |
      3. |    2          2      1      0      1        2 |
      4. |    1          3      1      1      0        3 |
      5. |    2          3      1      1      0        3 |
         |-----------------------------------------------|
      6. |    3          3      1      1      0        3 |
      7. |    3          1      0      1      1        1 |
      8. |    2          1      0      0      2        2 |
      9. |    1          1      0      1      1        1 |
         +-----------------------------------------------+
    .

    Comment


    • #3
      Hi Nick,
      Thank you for your response and you do pose some interesting thinking points!

      I haven't been specific about the scenario, but here VAR1 will never be missing. As for the previous values being the same, this is what was making it difficult for me to code. I'm sorry I didn't specify well enough but in the scenario where the previous two values are the same, I actually want the program to look back another one until it finds two distinct values. The idea is that the program is finding the value that was been least recently used.

      I think the solution you have given me is absolutely perfect though and can be adapted for what I need thank you very much!

      Vest wishes,
      Cydney

      Comment

      Working...
      X