Bookmark and Share

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


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

Re: st: identifying how many digits string var has


From   Joerg Luedicke <joerg.luedicke@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: identifying how many digits string var has
Date   Sat, 2 Apr 2011 23:30:50 -0400

On Sat, Apr 2, 2011 at 8:38 PM, Daniel Marcelino <dmsilva.br@gmail.com> wrote:
> Dear all,
> Is there a way to identify how many digits a string var have? In the
> following example I have 18 , 14, and "missing" digits (counting "/".
> "-" and "."). So, I want to identify in var2 how many digits per line
> var1 has.
>
> gen str20 var1 = ""
> input
> "12.205.291/0001-33"
> "12.176.958/0001-17"
> "589.275.222-49"
> "409.011.140-49"
> "12.147.825/0001-12"
> "010.433.392-80"
> "07.752.894/0001-15"
> "."
> end
>
>
> I tried this loop, but it just returned me value "3" for all obs. What is wrong?
>
> foreach x of varlist var1 {
> generate byte var2 = 1 if strlen(`x')
> replace var2 = 2 if strlen(`x')
> replace var2 = 3 if strlen(`x')
> }

The string function -length- (-help string functions-) can do that for you:

gen var2=length(var1)

hth,

J.
*
*   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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index