Hello!
I'm trying to drop rows based on their position related to another row specified in a compound 'if' statement. For example, if I have the following data:
I'm trying to write a code that'll allow me to delete the three rows above the row with a value of 1 in flagvar (observation 6 here) and with the additional specification that a value of "****" should be in stringvar three rows above the row flagged with a 1 in flagvar. So something like
Obviously, that code doesn't work, but it's just to illustrate what I'm trying to do. In other words, I want to be able to drop the rows above a row flagged with a value of 1 in flagvar up to and including the next "****" in stringvar in descending order. If run properly, my data would then look like this:
Is there a way to do this in Stata? I do not want to make reference to specific observation numbers at all in my code because I'm writing a program that could be applied to other similar datasets.
Thanks!
I'm trying to drop rows based on their position related to another row specified in a compound 'if' statement. For example, if I have the following data:
PHP Code:
| stringvar flagvar |
|---------------------|
1. | aaaa . |
2. | bbbb . |
3. | **** . |
4. | cccc . |
5. | dddd . |
6. | **** 1 |
7. | eeee . |
8. | ffff . |
PHP Code:
drop in _n-1 _n-2 _n-3 if flagvar==1 & stringvar[_n-3]=="****"
PHP Code:
| stringvar flagvar |
|---------------------|
1. | aaaa . |
2. | bbbb . |
3. | **** 1 |
4. | eeee . |
5. | ffff . |
Thanks!
Comment