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

st: re: Creating number of days per month

From   Kit Baum <>
Subject   st: re: Creating number of days per month
Date   Mon, 2 Nov 2009 11:22:22 -0500

If the user wants separate variables per month,

inp str10(var1 var2)
"30nov2007"   "14feb2008"
"21jan2009"   "07apr2009"
"24apr2008"   "09jul2008"
"22feb2008"   "08may2008"
"01oct2008"   "16dec2008"
"13jul2009"   "28sep2009"
"04oct2008"   "20dec2008"
"07jul2009"   "22sep2009"
"03jun2008"   "19aug2008"
"11feb2009"   "30apr2009"
"31mar2007"   "17jun2007"
"01may2007"   "19jul2007"
"04apr2007"   "22jun2007"
"03jul2008"   "20sep2008"
"10jul2007"   "27sep2007"

gen date1=date(var1, "DMY")
gen date2=date(var2, "DMY")
drop var1 var2
g patientid = _n
reshape long date, i(patientid)
tsset patientid date
format date %td
l, sepby(patientid)
g mon = mofd(date)
g days = 1
collapse (count) days, by(patientid mon)
format mon %tm
l patientid mon days, sepby(patientid)

// if you want separate variables per month:
tab mon, g(M)
local nmo `r(r)'
su mon, mean
local monr `r(min)'
forv i=1/`nmo' {
	qui replace M`i' = M`i' * days
	rename M`i' mon`monr'
	local ++monr
// creation of varnames in YYMM format left as a further enhancement
// these variables could be collapsed by patient to end up with wide- format data

Kit Baum   |   Boston College Economics & DIW Berlin   |
An Introduction to Stata Programming |
   An Introduction to Modern Econometrics Using Stata  |

*   For searches and help try:

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