Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: changing ddmmyyyy to yyyymm


From   Neil Shephard <nshephard@nhs.net>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: changing ddmmyyyy to yyyymm
Date   Mon, 15 Sep 2008 18:13:23 +0100

Fabian Brenner wrote:
Dear all,
I have the following problem:
Variable "date" has the following form "11301998" (the format is str8 %8s). I want to create a new variable called "calculation_date" (format: str6 %9s). Calculation date should have the form "199811".

(that means changing date from ddmmyyyy to yyyymm)
The following should do the trick, you just rip out substrings of the year, the month and day separetly and then combine them...

gen newdate = substr(date, -4, .) + substr(date, 3, 2) + substr(date, 1, 2)

Note you'll get tripped up if any of the dates are less than eight charters long, for example if there are days < 10 that aren't padded by a leading zero.

Neil

***********************************************************************
This message may contain confidential and privileged information.
If you are not the intended recipient you should not disclose, copy
or distribute information in this e-mail or take any action in reliance
on its contents. To do so is strictly prohibited and may be unlawful.
Please inform the sender that this message has gone astray before
deleting it. Thank you.

2008 marks the 60th anniversary of the NHS. It's an opportunity to pay
tribute to the NHS staff and volunteers who help shape the service, and
celebrate their achievements.

If you work for the NHS and would like an NHSmail email account, go
to: www.connectingforhealth.nhs.uk/nhsmail
***********************************************************************

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