Statalist


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

st: RE: problems on the string functions


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: problems on the string functions
Date   Thu, 19 Nov 2009 15:59:43 -0000

Answers embedded below. 

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

gjhxmu@sina.com

1. I want to replace the second word in a string varible with another
word. What is the right function?

NJC>>> If you're lucky, -subinstr()- might be enough. 
Otherwise, use -split- followed by -egen, concat()- 

I have looked up string functions. Unluckily, no findings.
BTW, I found -word()- was maybe useful for what I want to do. However,
missing values were always caused when I applied.
For example,

clear
input str6 v1
123
abc
2da
end
l
g v2=word(v1,2)

The result window showed "(3 missing values generated)" .

Could anyone tell me why?

NJC>>> Sure. None of these strings has more than one word. So, the
second word is empty (missing). 

2.I have two string variables v1 and v2. I want to generate a new
variable v3 as following.

v1      v2  v3
abc123 123 abc
ab45   b45 a
cdef67 f67 cde

How to deal with it?

NJC>>> 

gen v3 = substr(v1, 1, strpos(v1, v2) - 1) 




*
*   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   |   What's new   |   Site index