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

RE: handling date data (was st: Please help!)

From   "Neil Shephard" <>
Subject   RE: handling date data (was st: Please help!)
Date   Tue, 18 May 2004 08:54:52 +0100

> 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 Shephard
Genetics Statistician
ARC Epidemiology Unit, University of Manchester

"Mistrust a man who takes games too seriously - it means
he doesn't take life seriously enough" - Montaigne

*   For searches and help try:

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