> I am trying to take the first two characters of a string variable
> using substr command as below:
>
> name2 = substr(name,1,2)
>
> However I get an error message of " type mismatch". Any idea about how
> I can handle this? Thanks
>
Firstly a change in subject title would be more useful for recipients as the subject title is
informative as to the contents of the mail, and in this case your question has nothing to
do with handling dates (See point 2 under section 2.2 of the statlist FAQ, links for which
appear at the end of every Statalist post).
I'm guessing your trying to create a new variable which is the first two characters of the
variable name, in which case you will be using the -generate- command. When
generating string variables (I'm assuming the first two characters of the name variable
are in fact strings), you need to explicity declare your new variable (name2) as being
string, so....
. gen str2 name2 = substr(name,1,2)
...should work. If you know that all of the first two characters are numbers and you
would like name2 to be a number (unlikely from your comments), then the following
would work....
. gen name2 = real(substr(name, 1,2)
Hope that helps
Neil
Neil Shephard
Genetics Statistician
ARC Epidemiology Unit, University of Manchester
[email protected]
[email protected]
"Mistrust a man who takes games too seriously - it means
he doesn't take life seriously enough" - Montaigne
*
* 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/