I didn't know this -inlist- command and it is definitely very useful!!!
Thanks a lot!!!
Adrian
> -----Original Message-----
> From: Nick Winter [mailto:[email protected]]
> Sent: Friday, November 21, 2003 3:16 PM
> To: [email protected]
> Subject: Re: Replace
>
>
> Hi,
>
> As I understand it, aside from the "PC" complication, your task is to
> propagate values of status forward through the data for each
> country. This
> can be accomplished by:
>
> . bysort country (date1): replace status=status[_n-1] ///
> if status=="" & inlist(status[_n-1],"M","W","NM")
>
> What this does:
>
> . bysort country (date1):
>
> executes the command within country, with cases sorted by
> date1 within country.
>
> replace status=status[_n-1]
>
> replaces status with the value of status in the prior
> observation, but only...
>
> if status=="" & inlist(status[_n-1],"M","W","NM")
>
> if status is blank, and the prior observation for status is M, W, or
> NM. Because Stata executes the -replace- from top to bottom,
> each blank
> will be replaced in order, and the value of M/W/MN will
> propagate through
> the observations. Because of teh -bysort- prefix, values will not
> propagate into the next country.
>
> But this leaves you with the "PC" issue: with observations of
> "PC", you
> want to look *forward* to the next non-blank, grab that, then
> continue as
> above.
>
> Here's my take on that:
>
> . bysort country (date1): replace status = "X" ///
> if status=="" & inlist(status[_n-1],"PC","X")
>
> . gen negdate = -date1
> . bysort country (negdate): replace status = status[_n-1] ///
> if (status=="X" | status=="PC")
> inlist(status[_n-1],"M","W","NM")
>
> . bysort country (date1): replace status=status[_n-1] ///
> if (status=="" | & inlist(status[_n-1],"M","W","NM")
>
>
> . replace status="" if status=="X"
> . drop negdate
>
> What this does:
>
> First, fill in with "X" all the blank observations that
> follow "PC" (logic
> here is the same as the propagation above).
>
> Then, reverse the date sort order (by sorting on the negative
> of the date
> variable), and propagate M/W/NM through the "X" values (and
> the PC values).
>
> Then, do the "simple" propagation, as above.
>
> Then, delete any remaining "X" values. (There will be
> remaining "X" values
> in any cases where a country has a "PC" that is *not*
> followed ever by a
> M/W/NM at any point.)
>
> -Nick Winter
>
>
> At 02:09 PM 11/21/2003 -0500, you wrote:
> >Dear Statapeeps,
> >
> >I need to replace some values of a variable that meet certain
> >conditions. Although I know it's not possible to make direct
> references
> >to rows using the -replace- command, this is what I want to do:
> >
> >replace status = status[`j'] if status[`j'] == "NM" |
> status[`j'] == "W"
> >in `i'
> >
> >I tried to use a local variable to avoid using status[`j'] in the
> >command line but it didn't work. Like this:
> >
> > local sta = status[`j']
> > replace status = "`sta'" if
> >status[`j']=="NM"|status[`j']=="W" in `i'
> >
> >You will find my whole script below.
> >
> >Do you know how I can get past this problem?
> >
> >Here I present a chunk of my dataset so that you understand
> the problem
> >better. You don't need to read the rest if you already
> understood what I
> >want to do from the command line above.
> >
> >The data is organized in a panel format by country-date.
> What I need is
> >to fill in the values of 'status' whenever they are missing and they
> >meet certain conditions.
> >
> >Suppose I start a loop that sweeps through 'status' row by row. Most
> >observations in 'status' are missing and suddenly, in
> observation [i] I
> >find one cell that is not missing.
> >(a) If status[i] = "M"/"W"/"NM", the subsequent rows
> (observations [j],
> >where j gets different values) should be "M"/"W"/"NM" whenever
> >country[i]=country[j] and date1[i]=date1[j].
> >
> >(b) If, however, status[i] = "PC" then I need to look at the next
> >non-empty cell to decide what "PC" is going to be in [i]. PC
> is going to
> >be converted into "M" except if a subsequent value is "W" or "NM". A
> >value in row [j] is considered subsequent if [j] > [i] and
> >country[i]=country[j] and date1[i]=date1[j].
> >
> >The data looks like this:
> >
> > country date date1 date2 status
> > 401. | Armenia 1997m5 1995m6 1999m12 |
> > 402. | Armenia 1997m6 1996m2 1999m12 PC |
> > 403. | Armenia 1997m7 1996m2 1999m12 |
> > 404. | Armenia 1997m8 1996m2 1999m12 |
> > 405. | Armenia 1997m9 1996m2 1999m12 |
> > |------------------------------------------------------|
> > 406. | Armenia 1997m10 1996m2 1999m12 |
> > 407. | Armenia 1997m11 1996m2 1999m12 |
> > 408. | Armenia 1997m12 1996m2 1999m12 |
> > 409. | Armenia 1998m1 1996m2 1999m12 |
> > 410. | Armenia 1998m2 1996m2 1999m12 W |
> > |------------------------------------------------------|
> > 411. | Armenia 1998m3 1996m2 1999m12 |
> > 412. | Armenia 1998m4 1996m2 1999m12 |
> > 413. | Armenia 1998m5 1996m2 1999m12 |
> > 414. | Armenia 1998m6 1996m2 1999m12 |
> > 415. | Armenia 1998m7 1996m2 1999m12 |
> > |------------------------------------------------------|
> > 416. | Armenia 1998m8 1996m2 1999m12 |
> > 417. | Armenia 1998m9 1996m2 1999m12 |
> > 418. | Armenia 1998m10 1996m2 1999m12 |
> > 419. | Armenia 1998m11 1996m2 1999m12 |
> > 420. | Armenia 1998m12 1996m2 1999m12 PC |
> > |------------------------------------------------------|
> > 421. | Armenia 1999m1 1996m2 1999m12 |
> > 422. | Armenia 1999m2 1996m2 1999m12 |
> > 423. | Armenia 1999m3 1996m2 1999m12 |
> > 424. | Armenia 1999m4 1996m2 1999m12 |
> > 425. | Armenia 1999m5 1996m2 1999m12 |
> > |------------------------------------------------------|
> > 426. | Armenia 1999m6 1996m2 1999m12 |
> > 427. | Armenia 1999m7 1996m2 1999m12 |
> > 428. | Armenia 1999m8 1996m2 1999m12 |
> > 429. | Armenia 1999m9 1996m2 1999m12 |
> > 430. | Armenia 1999m10 1996m2 1999m12 M |
> > |------------------------------------------------------|
> > 431. | Armenia 1999m11 1996m2 1999m12 |
> > 432. | Armenia 1999m12 1996m2 1999m12 |
> > 433. | Armenia 2000m1 . . |
> > 434. | Armenia 2000m2 . . |
> >
> >And after running my script it should look like this:
> >
> > country date date1 date2 status
> > 401. | Armenia 1997m5 1995m6 1999m12 |
> > 402. | Armenia 1997m6 1996m2 1999m12 W |
> > 403. | Armenia 1997m7 1996m2 1999m12 W |
> > 404. | Armenia 1997m8 1996m2 1999m12 W |
> > 405. | Armenia 1997m9 1996m2 1999m12 W |
> > |------------------------------------------------------|
> > 406. | Armenia 1997m10 1996m2 1999m12 W |
> > 407. | Armenia 1997m11 1996m2 1999m12 W |
> > 408. | Armenia 1997m12 1996m2 1999m12 W |
> > 409. | Armenia 1998m1 1996m2 1999m12 W |
> > 410. | Armenia 1998m2 1996m2 1999m12 W |
> > |------------------------------------------------------|
> > 411. | Armenia 1998m3 1996m2 1999m12 W |
> > 412. | Armenia 1998m4 1996m2 1999m12 W |
> > 413. | Armenia 1998m5 1996m2 1999m12 W |
> > 414. | Armenia 1998m6 1996m2 1999m12 W |
> > 415. | Armenia 1998m7 1996m2 1999m12 W |
> > |------------------------------------------------------|
> > 416. | Armenia 1998m8 1996m2 1999m12 W |
> > 417. | Armenia 1998m9 1996m2 1999m12 W |
> > 418. | Armenia 1998m10 1996m2 1999m12 W |
> > 419. | Armenia 1998m11 1996m2 1999m12 W |
> > 420. | Armenia 1998m12 1996m2 1999m12 M |
> > |------------------------------------------------------|
> > 421. | Armenia 1999m1 1996m2 1999m12 M |
> > 422. | Armenia 1999m2 1996m2 1999m12 M |
> > 423. | Armenia 1999m3 1996m2 1999m12 M |
> > 424. | Armenia 1999m4 1996m2 1999m12 M |
> > 425. | Armenia 1999m5 1996m2 1999m12 M |
> > |------------------------------------------------------|
> > 426. | Armenia 1999m6 1996m2 1999m12 M |
> > 427. | Armenia 1999m7 1996m2 1999m12 M |
> > 428. | Armenia 1999m8 1996m2 1999m12 M |
> > 429. | Armenia 1999m9 1996m2 1999m12 M |
> > 430. | Armenia 1999m10 1996m2 1999m12 M |
> > |------------------------------------------------------|
> > 431. | Armenia 1999m11 1996m2 1999m12 M |
> > 432. | Armenia 1999m12 1996m2 1999m12 M |
> > 433. | Armenia 2000m1 . . |
> > 434. | Armenia 2000m2 . . |
> >
> >This is my script and I don't know what the problem is:
> >
> > local m = _N
> > forv i = 1/`m' {
> > if status[`i'] != "" {
> > if status[`i'] == "PC" {
> > local j = `i'+1
> > while status[`j'] == "" &
> >country[`j']==country[`i'] & date1[`i']==date1[`j'] {
> > local sta = status[`j']
> > replace status = "`sta'" if
> >status[`j']=="NM"|status[`j']=="W" in `i'
> > replace status = "M" if
> >status[`j']=="M"|(date1[`j']!=date1[`i'] &
> country[`j']!=country[`i'] in
> >`i'
> > }
> > }
> > else {
> > local j = `i'+1
> > local k = 0
> > while status[`j'] == "" &
> >country[`i']==country[`j'] & date1[`i']==date1[`j'] {
> > local sta = status[`i']
> > replace status =
> "`sta'" in `j'
> > local j = `j'+1
> > local k = `k'+1
> > }
> > }
> > local i = `i'+`k'
> > }
> > }
> >
> >Thanks a lot,
> >Adrian
> >
> >*
> >* 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/
>
> --------------------------------------------------------
> Nicholas Winter t 607.255.8819
> Department of Government f 607.255.4530
> 308 White Hall [email protected]
> Cornell University falcon.arts.cornell.edu/nw53
> Ithaca, NY 14853-4601
>
> *
> * 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/