[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: bys and [_N] within replace

From   <>
To   <>
Subject   st: bys and [_N] within replace
Date   Wed, 18 Mar 2009 11:05:24 +0100

I have the following panel.

Id	Commdate	Result2		
400	05may2008	TM FAILURE	
400	29may2008	DM FAILURE	
400	05oct2008	TM Ei-svar	 
400	27oct2008	TM Ei-svar	
400	30nov2008	TM Ei-svar	
400	04jan2009	TM Ei-svar	

I would like to generate a binary variable that equals 1 if a category "TM Ei-svar" within result2 occurs. But I only want this to happen if no other category in Result2 is present within Id, either onwards are backwards in time (Commdate). Nick Cox and Martin Weiss gave excellent advice on an earlier related problem pointing me to 'bys' and using [_N] within replace statements. Nick also refered me to further free 'by' tricks at

However, I am still unable to solve the above problem. I tried

gen kun_eisvar=0
bys Id (Commdate):replace kun_eisvar=1 if ///
	(result2=="TM Ei-svar"& ///
	result2[_N]!="TM FAILURE"&result2[_N-1]!="TM FAILURE"& ///
	result2[_N]!="TM SUCCESS"&result2[_N-1]!="TM SUCCESS"& ///
	result2[_N]!="SUCCESS DM SMS 1999"&result2[_N-1]!="SUCCESS DM SMS 1999"& ///
	result2[_N]!="DM SUCCESS NY BIND"&result2[_N-1]!="DM SUCCESS NY BIND"& ///
	result2[_N]!="DM SUCCESS"&result2[_N-1]!="DM SUCCESS"& ///
	result2[_N]!="DM FAILURE"&result2[_N-1]!="DM FAILURE")

though, this does not work. See an example below. 

Id	Commdate	Result2	kun_eisvar		
400	05may2008	TM FAILURE	0 
400	29may2008	DM FAILURE	0 
400	05oct2008	TM Ei-svar	1 
400	27oct2008	TM Ei-svar	1 
400	30nov2008	TM Ei-svar	1 
400	04jan2009	TM Ei-svar	1 

Because Result2 has other categories than 'TM Ei-svar' for Id=400 this observation should not equal 1.

Any advice on where I went wrong is appreciated.

Best wishes,

*   For searches and help try:

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