Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: Still having trouble coding loops


From   Jeff Rosen <[email protected]>
To   [email protected]
Subject   Re: st: RE: Still having trouble coding loops
Date   Mon, 17 Jul 2006 17:26:23 -0400

Thank you very much with the detailed response. I didn't think about sorting the dataset backwards. I'll take a look at the information you cited for more help.

Thanks again,

Jeff

At 05:13 PM 7/17/2006, you wrote:

What look like loops in (al)most any other language
are not necessarily loops in any language that
is vectorised.

As I understand it, for each household,

1. You want non-initial sequences of zero to be
replaced by the previous non-zero observed.

2. You want initial sequences of zeros to be replaced
by the first non-zero observed.

When you say "proceeding" I take it that you mean "preceding".

The second is indeed trickier than the first, but
there is a kind of mirror symmetry here which should
suggest that essentially the same approach would
work.

These are variations on a more common problem
in which we have not zero, but missing, as the
problematic value to be fudged. There is a long-winded
discussion at

FAQ     . . . . . . . . . . . . . . . . . . . . . . . Replacing missing values
        2/03    How can I replace missing values with previous or
                following nonmissing values?
                http://www.stata.com/support/faqs/data/missing.html

I am going to assumed that -rd- is a sequence
variable. So, for #1, we can go

bysort hhold (rd) : replace acre = acre[_n-1] if acre == 0  & _n > 1

That's not quite what you had. Note with this that I took
care with the initial condition. When under -by:- we look
at acre[1], the first value in each block, the previous
value, acre[0], will always be missing, which counts as positive.
We don't want that missing to over-write any initial 0, so
no replacements are allowed for _n of 1.

On the other hand, with this, initial sequences of 0 will otherwise
be unchanged, which we don't mind, so the condition which you had

... if acre[_n-1] > 0

is not needed for those other observations.

For #2, the "reverse time" trick explained in the
FAQ referred to above seems the simplest. Put each household sequence
back to front

gsort hhold - rd

Now it's the same idea, almost:

by hhold : replace acre = acre[_n-1] if acre == 0

If I got the logic right, there will be no initial zeros
to replace, as these would have been final zeros previously
and replaced by the previous cascade of changes,
but if you like to put it there the extra condition

... if _n > 1

should do no harm:

by hhold : replace acre = acre[_n-1] if acre == 0 & _n > 1

Then we sort back:

sort hhold rd

In related territory are other FAQs such as

FAQ     . . . . . . . . . . . . . . . First and last occurrences in panel data
        12/05   How can I identify first and last occurrences
                systematically in panel data?
                http://www.stata.com/support/faqs/data/firstoccur.html

FAQ     . . . . . . . . . . . . . . . . . .  Dropping spells of missing values
        9/03    How can I drop spells of missing values at the
                beginning and end of panel data?
                http://www.stata.com/support/faqs/data/dropmiss.html

which may be useful for spelling out basic techniques for future problems.

Nick
[email protected]

JEFFREY ROSEN

> 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:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index