[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Problems with %tw when mdy() is used to create the date value

From   Joachim Landström <[email protected]>
To   <[email protected]>
Subject   st: RE: Problems with %tw when mdy() is used to create the date value
Date   Fri, 6 Nov 2009 13:32:05 +0100

Please disregard this post. It is an obsolete issue. An updated version
already exist here.  This mail must have been floating around somewhere in
cyberspace until now. . . /Joachim

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of [iso-8859-1]
Joachim LandstrXm
Sent: Friday, November 06, 2009 9:21 AM
To: [email protected]
Subject: st: Problems with %tw when mdy() is used to create the date value

Hi all,
I have encountered a strange problem that I do not know how to solve of if
there is a bug somewhere. The problem is as follows From a string value
(such as e.g., 12/31/1974) i parse out year, month, and day. I then convert
those values to a date value (myDate) using mdy. The string value 12/31/1974
thus becomes myDate 5478. Finally I format the myDate value as e.g., %td or

When I compare myDate with the original string I find a one-to-one
correspondance such that the myDate shows the same date as do the original
string (i.e., when the original string is 12/31/1974, the myDate %td format
is 31dec1974). However when I instead format myDate as %tw I find a
completely different result. E.g., the example 12/31/1974 then becomes
2065w19. . . Something must be very wrong and I appreciate any suggestion to
solve this riddle.

The code is as below.

capture drop myDay myMonth myYear

generate myYear = substr(dates,7,10)
replace myYear = substr(dates,5,4) if length(dates)==8 replace myYear =
substr(dates,6,4) if length(dates)==9

generate firstSlash = strpos(dates,"/")

generate myMonth = substr(dates,1,2) if length(dates) == 10 replace myMonth
= substr(dates,1,2) if firstSlash == 3 replace myMonth = substr(dates,1,1)
if firstSlash == 2

generate myDay = substr(dates,4,2) if length(dates) == 10 replace myDay =
substr(dates,4,1) if length(dates) == 9 & firstSlash == 3 replace myDay =
substr(dates,3,2) if length(dates) == 9 & firstSlash == 2 replace myDay =
substr(dates,3,1) if length(dates) == 8 & firstSlash == 2

destring myMonth, replace
destring myYear, replace
destring myDay, replace 

generate myDate = mdy(myMonth, myDay, myYear)

format myDate %td

capture drop firstSlash

Joachim Landstrom
Assistant Professor
Department of Business Studies
Uppsala University, Sweden

*   For searches and help try:

*   For searches and help try:

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