Thanks a lot, Rafal. Works perfect. Certainly I can use wordcount. It's
easier in this case but yours certainly also works if the string variable
contains comma-separated phases or sentences.
Wanli
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Rafal Raciborski
Sent: Sunday, October 23, 2005 1:12 AM
To: [email protected]
Subject: Re: st: String function
I did not find an earlier answer either but the below works fine.
rafal
. use comma, clear
. list
+-----------------------------+
| var1 |
|-----------------------------|
1. | one, two, three |
2. | one |
3. | one, two, three three, four |
4. | one one, two two, |
5. | ,, one |
|-----------------------------|
6. | |
7. | one, two,,, |
8. | ,,,, |
9. | ,,.., |
10. | ,. .. . ,, ,-, =. |
+-----------------------------+
. gen var1copy = var1
(1 missing value generated)
. gen comma = 0
. gen length = length(var1)
. summ length
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
length | 10 10.8 8.534896 0 27
. local length = r(max)
. forvalues i = 1/`length' {
2. replace comma = comma + 1 if substr(var1,1,1)==","
3. replace var1 = substr(var1,2,.)
4. }
<snip>
. list
+-----------------------------------------------------+
| var1 var1copy comma length |
|-----------------------------------------------------|
1. | one, two, three 2 15 |
2. | one 0 3 |
3. | one, two, three three, four 3 27 |
4. | one one, two two, 2 18 |
5. | ,, one 2 7 |
|-----------------------------------------------------|
6. | 0 0 |
7. | one, two,,, 4 11 |
8. | ,,,, 4 4 |
9. | ,,.., 3 5 |
10. | ,. .. . ,, ,-, =. 5 18 |
+-----------------------------------------------------+
.
================
Rafal Raciborski
Graduate student
Department of Political Science
Emory University
301 Tarbutton Hall
1555 Dickey Drive
Atlanta, GA 30322
404-378-9826 (home)
[email protected]
http://www.roofoos.net/
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/