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

Re: st: RE: The last four digits

From   Ernest Berkhout <[email protected]>
To   [email protected], <[email protected]>
Subject   Re: st: RE: The last four digits
Date   Tue, 09 Dec 2003 10:58:42 +0100

At 01:41 9-12-2003, Daniel R Sabath wrote:
I just wrote some code for this for someone else, so it's fresh on the

/* assumes nacimi is numeric and of fixed length */
gen nacimi-1  = real(substr(string(nacimi),1,4))
gen nacimi-2  = real(substr(string(nacimi),-4,4))
That is not a completely foolproof solution, as I don't think that the default behaviour for float-variables will be to display leading zero's. The second line is sufficient to extract the 4 last digits, as was the problem at hand.
But to elaborate a bit on this, if the objective would be to split something like the following variable:


where you want the last 4 digits in a variable "year" and the remaining part in a variable "date" an alternative to the substr-string-approach is as follows:

gen date = int(nacimi/10000)
gen year = mod(nacimi/10000)

There is no restriction as to how many digits there are before the last 4 digits.

Ernest Berkhout
SEO Amsterdam Economics
University of Amsterdam

Room 3.08
Roetersstraat 29
1018 WB Amsterdam
The Netherlands

tel.:+ 31 20 525 1657
fax:+ 31 20 525 1686
A statistician: someone who insists
on being certain about uncertainty

* For searches and help try:

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