Statalist


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

st: RE: AW: AW: RE: AW: AW: Problems with %tw when date() is used to create a date variable


From   Joachim Landström <[email protected]>
To   <[email protected]>
Subject   st: RE: AW: AW: RE: AW: AW: Problems with %tw when date() is used to create a date variable
Date   Fri, 6 Nov 2009 12:49:20 +0100

Martin,
That is an excellent defition of the problem. I think that what you wrote
below constitutes a beginning to a solution for me (rather than using the
barbarian approach of deleting observations). So I will for the moment not
be "barbaric" and will instead "put on my thinking cap" and try to implement
the ideas below into my code.

Many thanks.
Joachim
 

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Martin Weiss
Sent: Friday, November 06, 2009 11:35 AM
To: [email protected]
Subject: st: AW: AW: RE: AW: AW: Problems with %tw when date() is used to
create a date variable


<> 



It has to occur as -di mod(365,52)- yields "1", and given the intention to
start the first week of the year on the first of January, there has got to
be a problem at the end of the year. Stata`s solution is to increase the
"chunk size" of the weeks to 8, and to 9 in leap years:


****
clear*
set obs 366
gen day=date("1/1/2000", "DMY")+_n-1
format day %td
gen week=wofd(day)
format week %tw
bys week: egen daysperweek=count(day)
egen total=count(day)
table week daysperweek
****



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Martin Weiss
Gesendet: Freitag, 6. November 2009 11:26
An: [email protected]
Betreff: st: AW: RE: AW: AW: Problems with %tw when date() is used to create
a date variable


<> 

BTW, the issue is also present in the current year:


*************
clear*
set obs 365
gen day=date("1/1/2009", "DMY")+_n-1
format day %td
gen week=wofd(day)
format week %tw
bys week: egen daysperweek=count(day)
table week daysperweek
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Joachim
Landström
Gesendet: Freitag, 6. November 2009 11:12
An: [email protected]
Betreff: st: RE: AW: AW: Problems with %tw when date() is used to create a
date variable

How nice Martin! I does indeed solve my problem, but raises another
concerning years having 53 weeks.

When I convert my date values into weekly values as you proposed I end up
having duplicates of week 52 for those years that have 53 weeks (such as
e.g., 1980, 1985, 1991, 1996, 2002, and 2006). The Stata week values also
appear in duplicates so it is not just an issue of formatting. 

I presume this will pose a problem for me when I tsset the data and start to
run regressions on lagged values.

What to do with the duplicates (a side issue is of course why does not Stata
recognize the 53 week years)? Is there a workaround if needed (my dataset is
really on weekly basis so there is no option to run the analysis on daily
basis. I could of course run the analysis on monthly basis but I will then
loose too much information and hence it is also out of the question. . .

/Joachim 

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Martin Weiss
Sent: Friday, November 06, 2009 10:41 AM
To: [email protected]
Subject: st: AW: AW: Problems with %tw when date() is used to create a date
variable


<> 

See the conversion in action in the last line...

*************
clear*

inp str10 dates
"12/31/1974"
end

compress
 
gen myDate = date(dates, "MDY")
format myDate %td

list, noobs 

//wrong
di %tw myDate
//correct
di %tw wofd(myDate)
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Martin Weiss
Gesendet: Freitag, 6. November 2009 10:37
An: [email protected]
Betreff: st: AW: Problems with %tw when date() is used to create a date
variable


<> 

Not a riddle! -format-ting does not change the underlying value of the date
variable, only how it is -display-ed. To switch from daily to weekly
frequency, you want to use the conversion functions, as in

*************
help dates_and_times, mark(converting)
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Joachim
Landström
Gesendet: Freitag, 6. November 2009 10:37
An: [email protected]
Betreff: st: Problems with %tw when date() is used to create a date variable

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 create myDate using date. The string value
12/31/1974 thus becomes myDate value 5478. Finally I format the myDate value
as e.g., %td or %tw. 

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 follows.

generate myDate = date(dates, "MDY")
format myDate %td

I have also tested to use mdy() to create myDate (which takes a few more
lines of code) and find identical results.

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


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


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


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



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


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


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



*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index