|
ProductsPricesAustraliaNew ZealandSupportOrder form Quotation request Contact us |
Stata is a general-purpose statistical package for researchers of all disciplines. Epidemiology
Longitudinal/Panel Data
Multivariate Methods
Survey Data
Survival Analysis
Time-Series
Multiple Imputation
Structural Equation Modelling
![]() and Statistics useful across other fields In addition, Stata produces publication-quality graphics, contains extensive
data-management capabilities, and has a powerful matrix-programming language.
What's New in Stata 12?
Go here for a comprehensive
overview of what's new in Stata 12.
Stata Tip Working with Dates 3
clear
set more off
input ///
str6 id str20 date
01003 07Nov2008
01003 07Nov2008
01003 11Nov2008
01007 22Dec2008
01007 05Dec2008
01007 13Nov2007
01007 14Nov2007
01007 22Jul2006
01007 22Jul2006
01007 22Jul2006
01007 11Sep2006
01009 13Oct2005
01009 17May2006
01009 17May2006
01009 13Jan2010
01009 06Jun2010
01008 08Nov2007
01008 08Nov2007
01008 08Nov2007
01008 15Jul2009
01008 15Jul2009
01008 15Jul2009
01008 27May2010
01008 28May2010
01008 28May2010
01008 28May2010
end
l
generate date1=date(date, "DMY") //1
generate cluster=. //2
list, sepby(id)
tempvar max //3
bysort id (date1): gen `max'=_N //4
summarize `max' //5
forvalues i=1/`r(max)' { //6
bysort id (date1): replace cluster=`i' if /// //7
date1<=(date1[sum(cluster!=.)+1]+30) & cluster==.
list, sepby(id) // list command to show what is happening
// can be removed
}
list, sepby(id)
exit
Going through the above:(1) generate a new variable (date1) that takes the date in string format from date and converts this into elasped time (a numeric value) (2) generate a new variable called cluster; all values equal to missing (.) (3) Assigns name to temparory variable max (4) Using the bysort prefix, by every level of id the values of the temporary variable max is generated and filled with values of _N. Note macro subsittion single brackets around the temporary variable name. _N stands for the max number of observations. In this case, because of bysort, it is the max number of observation for each level of id. (5) The summarize command is used to obtain the max number of obseravations in all the levels of max. The summarize command has a handy return value that stores this eg. r(max). To see the other values returned by this command type: return list (after the summarize command) (6) looping over the code in the curly brackets using forvalues loop (7) using the bysort command replace the value of cluster with the looping index value (this will be the group number) if the qualifer is true ie. the start of the next cluster and within 30 days of the start of the new cluster. Breaking the qualifer down: date1<=(date1[sum(cluster!=.)+1]+30) & cluster==. cluster!=. logical statement either true of false ie if cluster does not equal (!=) a missing value (.) the observation is true and equals 1 (one) sum(cluster!=.) : sums the results of cluster!=. +1: add 1 (one) to the result of the sum(cluster!=.) date1[sum(cluster!=.)+1]:inside the square brackets (explicit subscripting) Stata has calculated the observation number of date1 that we require eg. data1[obs no]. Stata gets the data for the variable date1 (a date) and adds 30 days to this. Stata then tests if the current observation of date1 is <= to the value calculated by date1[sum(cluster!=.)+1]+30) and also that the current value of cluster if missing (.). If the statement is true the looping index value (`i') replace the current value for observation of cluster. For help on specific commands type: help and then the specific command eg. help input help generate help summarize (the saved results from the summarize command can be seen be typing: return list after the summarize command help macro help forvalues help sum() help tempvar help list Contact
details If our office is not attended, please leave your message as well as your phone number or Email address on our answering machine. You can send an Email or fax at any time and we will attend to it as soon as we can. ********************************************************************************************* Our company is:
******************************************************************************************** 'Stata is a registered trademark of StataCorp LP, College Station, TX, USA, and the Stata logo is used with the permission of StataCorp.'
|