In a survey dataset I have a string variable (type: str244
) with qualitative responses. I want to count the number of characters in each response/string and generate a new variable containing this number.
Using the egenmore
I have already counted the number of words using nwords
, but I cannot find the counterpart for counting characters.
EXAMPLE:
egen countvar = nwords(stringvar)
where countvar
is the new variable name and stringvar
is the string variable.
Does such an egen
function exist for counting characters?
There is no egen
function because there has long [sic] been a function strict sense to do this. In recent versions of Stata, the function is called strlen()
but the older name length()
continues to work:
. sysuse auto
(1978 Automobile Data)
. gen l1 = length(make)
. gen l2 = strlen(make)
. su l?
Variable | Obs Mean Std. Dev. Min Max
-------------+---------------------------------------------------------
l1 | 74 11.77027 2.155257 6 17
l2 | 74 11.77027 2.155257 6 17
See help functions
and (e.g.) this tutorial column.