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 on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: getting part of strings


From   Eric Booth <ebooth@ppri.tamu.edu>
To   "<statalist@hsphsun2.harvard.edu>" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: getting part of strings
Date   Sat, 26 Mar 2011 19:30:24 +0000

<>

Hi Daniel:

It appears that you sometimes want to extract 1 substring from var1 and sometimes more than one (such as obs. 2 in your example).  Also, it looks like a dash character is always your record marker even if information is missing.  Since this is consistent, I would use -split- to work with this data.  Here's an example:

***********************!
clear
inp str200 var1
"155 - VITAL DO REGO FILHO - PB - Senador"
"1111 -  - PP -  - Deputado Federal / 25888 - ATAIDES MENDES PEDROSA -PB - Deputado Estadual"
"1111 -  - PP -  - Deputado Federal / 22333 - EDNALDO PEREIRA DESANTANA - PB - Deputado Estadual"
"151 - JOSE WILSON SANTIAGO - PB - Senador"
"45123 - ANTONIO HERVAZIO BEZERRA CAVALCANTI - PB - Deputado Estadual"
"1212 - DAMIÃO FELICIANO DA SILVA - PB - Deputado Federal"
end

**using split**
replace var1 = subinstr(var1, " / ", " - ", .)
split var1, p("-")

**trim spaces in new vars**
ds var1?
foreach v in `r(varlist)' {
	replace `v'  = trim(`v')
	}


**it looks like the substr you want are in vars14, var15, var19:
l var14 var15 var19 

**grab the title or subtitle or gen an indicator if they are present**
g str50 title = var14 if  !mi(var14)
replace title = var15 if mi(title) & !mi(var15)
g str50 title2 = var19 if  !mi(var19)
l var1 title title2
**or
g titleind = 1 if !mi(var14) | !mi(var15) 
g title2ind = 1 if !mi(var19)
order *ind
***********************!

- Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
ebooth@ppri.tamu.edu
Office: +979.845.6754


On Mar 26, 2011, at 12:05 PM, Daniel Marcelino wrote:

> Dear all,
> 
> I'm dealing with a large data set which one string var is completely
> nested. I easily take off numbers from it, but I still breaking my
> head trying to figure out how can I get from var words like "PP",
> "Deputado Federal", "Senador", "Deputado Estadual". So, below a paste
> few cases.
> 
> clear
> inp str200 var1
> "155 - VITAL DO REGO FILHO - PB - Senador"
> "1111 -  - PP -  - Deputado Federal / 25888 - ATAIDES MENDES PEDROSA -
> PB - Deputado Estadual"
> "1111 -  - PP -  - Deputado Federal / 22333 - EDNALDO PEREIRA DE
> SANTANA - PB - Deputado Estadual"
> "151 - JOSE WILSON SANTIAGO - PB - Senador"
> "45123 - ANTONIO HERVAZIO BEZERRA CAVALCANTI - PB - Deputado Estadual"
> "1212 - DAMIÃO FELICIANO DA SILVA - PB - Deputado Federal"
> end
> 
> gen var2 = regexs(0) if regexm(var1, "^[0-9a-zA-Z]*")
> 
> *
> *   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/




*
*   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–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index