Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: RE: AW: Categorising dates
From
Nick Cox <[email protected]>
To
"'[email protected]'" <[email protected]>
Subject
st: RE: AW: Categorising dates
Date
Tue, 24 Aug 2010 15:39:47 +0100
As the putative author of -tostring-, I must protest mildly at this use of -tostring-, on two quite different grounds.
1. Style and efficiency
=======================
If you are working with a numeric variable, are inclined to allow force, and wish only to generate a single string variable, you can and should get there directly with e.g.
gen mydays = string(mydates, "%tdMonth")
which replaces a call to an .ado which is dozens of lines long with a single line of code with exactly the same effect.
-tostring- is a convenience command which is, literally, convenient when (a) you have two or more variables and/or (b) a desire to be prudent because you are worried about loss of information in conversion. If neither applies, calling up -tostring- is unnecessary.
2. Respecting the problem
=========================
For problems like Sara's the user is almost always better off with numeric date variables assigned appropriate date formats.
Nick
[email protected]
Martin Weiss
clear*
//generate data
set obs 50
gen mydates=date("23 Sep 09", "DM20Y")+_n-26
format mydates %tdMon_dd,_CCYY
//Get day of week
tostring mydates, gen(mydays) format(%td_Dayname) force
//Get month
tostring mydates, gen(mymonth) format(%tdMonth) force
//see result
l, noo
*************
sara khan
I have a list of daily dates inthe format, for example, 23 Sep 09, and
need to create two variables. One is to categorise the days into
weekly data (so week commencing on a Monday). The second is to create
a variable cataegorsing the daily data into monthly data.
I would be grateful for advice on how to do this.
*
* 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/