# RE: st: replace in paneldata at specific dates

 From To Subject RE: st: replace in paneldata at specific dates Date Mon, 16 Mar 2009 14:27:09 +0100

```Nick, thank you for pointing me to this article. Very helpful.

Best wishes,
Alexander

-----Opprinnelig melding-----
Fra: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] På vegne av Nick Cox
Sendt: 16. mars 2009 14:16
Til: statalist@hsphsun2.harvard.edu
Emne: RE: st: replace in paneldata at specific dates

If such -by:- tricks are new to you, then you might want to read

SJ-2-1  pr0004  . . . . . . . . . . Speaking Stata:  How to move step
by: step
Q1/02   SJ 2(1):86--102                                  (no
commands)
explains the use of the by varlist : construct to tackle
a variety of problems with group structure, ranging from
simple calculations for each of several groups to more
advanced manipulations that use the built-in _n and _N

There is a version accessible at

http://www.stata-journal.com/sjpdf.html?articlenum=pr0004

Nick
n.j.cox@durham.ac.uk

Alexander.Severinsen@telenor.com

Thank you, Martin and Nick. I was wrong in my description of the
problem. If, within id, result equals SUCCESS later than NA according to
comm_date, then NA does not change. Thus, changing the last part of
Martins code,

...result[_N]!="SUCCESS"

solved it.

Thanks a lot for both pointing me to 'bys' and [_N]. Very neat! Looks
easy, but I've spent the entire morning trying to figure this one out...

Nick Cox

It does no harm to insist that observations are ordered by date within
identifier:

bys id (comm_date): replace result = "DM SUCCESS" if result=="NA" &
concept=="veil" & abonaa=="FRIHET" & result[_N]=="SUCCESS"

Looking through very recent threads would uncover several examples of
the same technique.

Nick
n.j.cox@durham.ac.uk

Martin Weiss

********clear*

inp id str15 result str15 concept str15 abonaa str15 comm_date
300 Ei-svar veil FRIHET 30mar2008
300 Ei-svar veil FRIHET 19may2008
300 NA veil FRIHET 10jun2008
400 Ei-svar veil FRIHET 30mar2008
400 Ei-svar veil FRIHET 19may2008
400 NA veil FRIHET 10jun2008
400 FAILURE veil FRIHET 03nov2008
400 FAILURE veil FRIHET 17dec2008
400 SUCCESS veil FRIHET 14jan2009
end

bys id: replace result = "DM SUCCESS" if result=="NA" & concept=="veil"
& abonaa=="FRIHET" & result[_N]=="SUCCESS"

l, noo sepby(id)
*********

Alexander.Severinsen@telenor.com

> I have a panel of data that looks like this,
>
> id	result	consept	abonaa	comm_date
> 400	Ei-svar	veil		FRIHET	30mar2008
> 400	Ei-svar	veil		FRIHET	19may2008
> 400	NA		veil		FRIHET	10jun2008
> 400	FAILURE	veil		FRIHET	03nov2008
> 400	FAILURE	veil		FRIHET	17dec2008
> 400	SUCCESS	veil		FRIHET	14jan2009
>
> I would like to recode the variable result = "DM SUCCESS" if
result==NA,
> concept="veil" and abonaa="FRIHET", but only if the last comm_date for
every
> member of the panel is not equal to SUCCESS in the variable result. In
> this case result = SUCCESS for comm_date=14jan2009, thus result="NA"
stays
> equal to NA.
>
> However, in this example,
>
> id	result	consept	abonaa	comm_date
> 300	Ei-svar	veil		FRIHET	30mar2008
> 300	Ei-svar	veil		FRIHET	19may2008
> 300	NA		veil		FRIHET	10jun2008
>
> result=NA occurs at the last comm_date for id=300 thus I want to
recode NA
> = "DM SUCCESS"

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```