# RE: RE: RE: st: RE: problems on the string functions

 From "Nick Cox" To Subject RE: RE: RE: st: RE: problems on the string functions Date Fri, 20 Nov 2009 17:13:06 -0000

```What is happening is that -- when v3 is a string variable and not empty
--

replace v3 = subinstr(v3, "", "", 1)

zaps v3. That is, -v3- ends up empty.

I am not sure why that is happening. For example, replacing empty
strings by empty strings looks like an example of no change to me. Also
even if "" is a substring of a non-empty string, it still has a
complement.

Nick
n.j.cox@durham.ac.uk

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: 20 November 2009 16:10
To: statalist@hsphsun2.harvard.edu
Subject: RE: RE: RE: st: RE: problems on the string functions

My bug somewhere. Try

clear
input str10 (v1 v2)
ab123d 12d
efg34 f4
end
l
gen v3 = v1
qui forval i = 1/10 {
replace v3 = subinstr(v3, substr(v2, `i', 1), "", 1)  ///
if substr(v2, `i', 1) != ""
}
l

Nick
n.j.cox@durham.ac.uk

gjhxmu@sina.com

thank you for your help. However, missing value is still generated after
-capture-

. clear

. input str10 (v1 v2)

v1          v2
1. ab123d 12d
2. efg34 f4
3. end

. l

+--------------+
|     v1    v2 |
|--------------|
1. | ab123d   12d |
2. |  efg34    f4 |
+--------------+

. gen v3 = v1

.
. qui forval i = 1/10 {
.
. capture replace v3 = subinstr(v3, substr(v2, `i', 1), "", 1)
.
. }

. l

+-------------------+
|     v1    v2   v3 |
|-------------------|
1. | ab123d   12d      |
2. |  efg34    f4      |
+-------------------+

.

From: Nick Cox <n.j.cox@durham.ac.uk>
To: <statalist@hsphsun2.harvard.edu>
Subject: RE: RE: st: RE: problems on the string functions
Date: 2009-11-20 20:27:22

That is,

gen v3 = v1
qui forval i = 1/10 {
capture replace v3 = subinstr(v3, substr(v2, `i', 1), "", 1)
}

Nick
n.j.cox@durham.ac.uk

-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: 20 November 2009 09:45
To: statalist@hsphsun2.harvard.edu
Subject: RE: RE: st: RE: problems on the string functions

. help capture

Nick
n.j.cox@durham.ac.uk

gjhxmu@sina.com

My another doubt is the length of variable v2 changes among
observations.
How to decide the first number in the -substr-?
If the length for certain observations is 2, substr(v2,4,1) will cause
missing value.

From: Nick Cox <n.j.cox@durham.ac.uk>

gen v3 = v1
qui forval i = 1/10 {
replace v3 = subinstr(v3, substr(v2, `i', 1), "", 1)
}

Nick
n.j.cox@durham.ac.uk

gjhxmu@sina.com

Thank you very much, Baum and Nick.
I understood the use of -subinstr-, -word- and -strpos- with your help.
Another problem is how to deal with it if the strings excluded from
another string variable is not consecutive.

for example,
clear
input str10 (v1 v2)
ab123d 12d
efg34 f4
end

I want to get v3 as followings
v3
ab3
eg3

*
*   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/
```