As a footnote, observe that if the issue is counting "X" in values of
-kisses- such as "XX", "XXXXX", etc. then
gen nkisses = length(kisses)
will be a more direct and efficient solution so long as no other
characters are observed. -length(trim(kisses))- will protect against
accidental leading and trailing spaces.
Nick
n.j.cox@durham.ac.uk
Susan Olivia
Thanks Tirthankar,
Using Nick Cox's command is way more efficient. My earlier
attempts were very inefficient.
Tirthankar Chakravarty
There are probably many ways of doing this, but here is
way using Nick Cox's -egenmore- (SSC, Nick Winter is
attributed as the author of the -noccur()- function used
here) package:
clear*
set obs 100
g crosses = " "
local cross "x"
forv i=1/ 100 {
qui: replace crosses="`cross'" in `i'
local cross "`cross'x"
}
// ssc install egemore, replace
egen noccur = noccur(crosses), string("x")
su noccur
On Thu, Jul 16, 2009 at 2:26 AM, Susan
Olivia<olivia@primal.ucdavis.edu>
I have a variable (say number of days) and is a string
variable. This variable is represented by XXXX
(basically the number of X denotes the number of days).
I would like to create a numeric value for this variable
(i.e. 4 crosses = 4) . Is there a way I can easily do
this in Stata? I tried the 'encode' and 'destring'
commands, but these commands didn't do what I after.