Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# st: Re: Coding Question

 From "Joseph Coveney" To Subject st: Re: Coding Question Date Sun, 14 Aug 2011 23:15:21 +0900

```John Metcalfe wrote:

I have a matrix of serial measurements with some missing values as
below. I would like to write a simple code that counted each
individual as a 'conversion' (going from 0 to 1), a reversion (going
from 1 to 0), or both. Any ideas appreciated.

--------------------------------------------------------------------------------

Something like that below would work.  I'm assuming that rows are individuals
and columns are serial measurements.  (You didn't describe or show your variable
names.)

Joseph Coveney

input str8 individual_id serial_measurement1 serial_measurement2 ///
serial_measurement3 serial_measurement4 serial_measurement5
"11020189" 0         1         0         1         .
"11030107" 0         1         1         1         .
"11030206" 1         1         1         .         .
"11030207" 0         0         0         .         .
"11030265" 1         1         0         0         0
"11040003" 0         1         1         1         .
"11040079" 1         0         1         0         .
"11040225" 1         0         0         .         .
"11050079" 0         1         1         1         .
"11050203" 1         0         1         1         .
"11060278" 1         0         1         0         .
"11070069" 1         0         0         .         .
"11070195" 1         0         0         0         .
"11070229" 0         1         0         1         .
"11070245" 1         0         0         0         .
end

local deltas
forvalues interval = 2/5 {
generate int delta`interval' = ///
serial_measurement`interval' - ///
serial_measurement`=`interval'-1'
replace delta`interval' = 0 if missing(delta`interval')
local deltas `deltas' delta`interval'
}
local deltas : subinstr local deltas " " ", ", all
generate int conversion = max(`deltas')
replace conversion = 0 < conversion
generate int reversion = min(`deltas')
replace reversion = reversion < 0
generate byte both = conversion * reversion
local line_size `c(linesize)'
set linesize 200
list, noobs separator(0) abbreviate(20)
set linesize `line_size'
exit

*
*   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/
```