Generate variable containing number of characters in a string variable

harre picture harre · Aug 5, 2015 · Viewed 16.3k times · Source

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?

Answer

Nick Cox picture Nick Cox · Aug 5, 2015

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.