Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: AW: SV: RE: Splitting a textvariable
From
"Martin Weiss" <[email protected]>
To
<[email protected]>
Subject
st: AW: SV: RE: Splitting a textvariable
Date
Wed, 12 May 2010 18:02:11 +0200
<>
So, Tomas, as Nick has already said, this is not trivial:
*************
clear*
inp byte id str50 address
1 "Stockholm xroad 12 London yroad 10"
2 "London zroad 31"
3 "Helsinki rroad 1 Oslo sroad 123 Berlin troad 13"
end
split address, gen(newaddress)
drop address
reshape long newaddress, i(id) j(addnumber)
drop if mi(newaddress)
bys id (addnumber): gen byte runningnum=sum(real(newaddress[_n-1])!=.)+1
bys id runningnum: gen byte j=sum(runningnum[_n]==runningnum[_n-1])+1
drop addnumber
reshape wide newaddress, i(id runningnum) j(j)
gen address=newaddress1+ " "+ newaddress2+ " "+ newaddress3
drop newa*
l, sepby(id)
*************
HTH
Martin
-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Tomas Lind
Gesendet: Mittwoch, 12. Mai 2010 17:24
An: [email protected]
Betreff: st: SV: RE: Splitting a textvariable
Thanks Nick and Bjarte for kind help and advice.
I´ll going to learn more about regular expressions and -strpos()- and
hopefully get it to work.
/Tomas
-----Ursprungligt meddelande-----
Från: [email protected]
[mailto:[email protected]] För Nick Cox
Skickat: den 12 maj 2010 14:14
Till: [email protected]
Ämne: st: RE: Splitting a textvariable
-inrange()- won't work as you want because the address is a string variable.
I can think of two strategies here:
1. Use -split- to split into string and numeric components; then put them
together again with a comma inserted.
2. Use regular expression functions.
3. Find the spaces with -strpos()- and stop when you reach the first space
preceded by a number.
There is some programming needed in each case.
Nick
[email protected]
Tomas Lind
I have (in principle) a dataset that looks like this with an id-variable and
a text-variable with addresses. Unfortunately there are often several
addresses in the same variable. I have to split these into one variable for
the first address, another for the second and so on.
id address
---------------------------------------------------------------
1 Stockholm xroad 12 London yroad10
2 London zroad 31
3 Helsinki rroad 1 Oslo sroad 123 Berlin troad 13
Each address ends with a number. My idea is to put in a parsing "," in the
empty space after the number (to be able to use -split parse-. Like this.
id address
----------------------------------------------------------------
1 Stockholm xroad,12 London yroad10
2 London zroad 31,
3 Helsinki rroad 1,Oslo sroad123,Berlin troad 13
One idea to do this is to use something like (if we rename the
address-variable to a shorter name, x).
replace x[i]="," if x[`i' - 1]==inrange(0, x[`i'], 9) & x[`i']==" "
I´m not sure about how to work out the details to get this into work. Any
help is welcome to put the commas into place. Other ideas are also welcome.
*
* 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/
*
* 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/