[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: Still having trouble coding loops
I'm still having some trouble coding loops.
I have a dataset that looks like:
hhold rd acre rent
1 1 0 0
1 2 0 0
1 3 1 1
1 4 0 0
1 5 2 2
1 6 0 0
What I want to do is recode the 0's to be equal to the value proceeding it, if that observation is equal zero and the proceeding observation is greater than zero. I also want to code all zeros that have no proceeding positive valued observations to be equal to the value of the first observation after in the sequence that is greater than zero. This is probably a little confusing so here is an example.
In rd 1 and rd 2, I would like acre to equal 1 and rent 1
In rd 4, I would like acre to equal 1 and rent to equal 1.
In rd 6, I would like acre to equal 2 and rent to equal 2.
I know how to code the first part of the problem.
Using: bysort hhold (rd): replace acre = acre[_n-1] if acre == 0 & acre[_n-1] > 0
I don't know how to code the second part. I'm assuming that I'll probably have to use a -foreach- loop, but I don't know how to program it. Any help would be very appreciated
* For searches and help try: