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

st: date conversion


From   Christopher F Baum <[email protected]>
To   [email protected]
Subject   st: date conversion
Date   Thu, 15 Jul 2004 21:00:10 -0400

Erica wrote
I have a date variable which lists dates as

MM-DD-YR

I want to remove the hyphens/dashes, but can't find anything in any
manual about this.

Also, sometimes the actual time is also included like:

MM-DD-YR HH:MM.

In this case, I want to dump the time stamp, and keep the date (without
the hyphens).


For the first problem, assuming var1 contains dates in that MM-DD-YR format,

. g v2=date(var1,"mdy",2020)


+------------------+
| var1 v2 |
|------------------|
1. | 02-11-78 6616 |
2. | 09-11-01 15229 |
3. | 08-13-51 -3063 |
4. | 07-15-04 16267 |
+------------------+

format v2 %td

+----------------------+
| var1 v2 |
|----------------------|
1. | 02-11-78 11feb1978 |
2. | 09-11-01 11sep2001 |
3. | 08-13-51 13aug1951 |
4. | 07-15-04 15jul2004 |
+----------------------+

For the second problem,

. g v4=date(substr(var3,1,9),"mdy",2020)
. format v4 %td

will drop the timestamp and return

+---------------------------------------------------+
| var1 v2 var3 v4 |
|---------------------------------------------------|
1. | 02-11-78 11feb1978 02-11-78 12:00 11feb1978 |
2. | 09-11-01 11sep2001 09-11-01 08:46 11sep2001 |
3. | 08-13-51 13aug1951 08-13-51 06:20 13aug1951 |
4. | 07-15-04 15jul2004 07-15-04 20:53 15jul2004 |
+---------------------------------------------------+

Kit

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