Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: problems with substr() (was st: handling date data )


From   "Neil Shephard" <[email protected]>
To   [email protected]
Subject   RE: problems with substr() (was st: handling date data )
Date   Tue, 18 May 2004 09:05:42 +0100

Apologies for not correcting the subject header in the previous post, I feel so stupid :-)

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


Neil Shephard
Genetics Statistician
ARC Epidemiology Unit, University of Manchester
[email protected]
[email protected]

"Contrariwise, if it was so, it might be; and if it
were so it would be; but as it isn't, it ain't. That's
logic" - Tweedledee (Alice Through the Looking Glass)

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



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index