Hi
I am using Stata 15.0. I have test score data on a variety of organisations for different time periods. I am trying to export a matrix into excel for each organisation that has the organisation name, mean test score and the start and end date of the time period for which they were assessed. My variables are:
program (organisation name)
period_`program' (variable per program that specifies the number of the time period for its observations)
today (date stored in date format)
score (test score)
My code looks like this:
levelsof program, local(program_name) /*This is necessary because we keep adding new programs and I want the code to run through all program names that we add*/
foreach program in `program_name'{
levelsof period_`program', local(no) /*Store no of periods for that program name, in "no"*/
foreach no in `no' {
egen min=min(today) if period_`program'==`no'
format min %tdMonth_DD,_CCYY
sum min, mean
local min: disp %tdMonth_DD,_CCYY r(mean)
drop min
egen max=max(today) if period_`program'==`no'
format max %tdMonth_DD,_CCYY
sum max, mean
local max: disp %tdMonth_DD,_CCYY r(mean)
drop max
egen x=mean(score) & period_`program'==`no'
egen mean=max(x)
local mean=mean
drop mean x
matrix t2=J(1,3,.) /*rows defined first*/
local i=0
foreach z in `min' `max' `mean' {
local i=`i'+1
matrix t2[1,`i']=`z'
}
mat rowname t2="`program'"
mat colname t2="Start date" "End date" "Average score"
*ssc install estout
estout matrix(t2) using "$out\\`program'_`no'.xls", replace
}
}
My data is as follows:
When I run this code I get an error that says "March not found" (ie, the local value of `min' which is March 02, 2018). Can anyone tell me how Stata can store the date value in a way that can be exported into a matrix through a loop?
Thanks
Megan
I am using Stata 15.0. I have test score data on a variety of organisations for different time periods. I am trying to export a matrix into excel for each organisation that has the organisation name, mean test score and the start and end date of the time period for which they were assessed. My variables are:
program (organisation name)
period_`program' (variable per program that specifies the number of the time period for its observations)
today (date stored in date format)
score (test score)
My code looks like this:
levelsof program, local(program_name) /*This is necessary because we keep adding new programs and I want the code to run through all program names that we add*/
foreach program in `program_name'{
levelsof period_`program', local(no) /*Store no of periods for that program name, in "no"*/
foreach no in `no' {
egen min=min(today) if period_`program'==`no'
format min %tdMonth_DD,_CCYY
sum min, mean
local min: disp %tdMonth_DD,_CCYY r(mean)
drop min
egen max=max(today) if period_`program'==`no'
format max %tdMonth_DD,_CCYY
sum max, mean
local max: disp %tdMonth_DD,_CCYY r(mean)
drop max
egen x=mean(score) & period_`program'==`no'
egen mean=max(x)
local mean=mean
drop mean x
matrix t2=J(1,3,.) /*rows defined first*/
local i=0
foreach z in `min' `max' `mean' {
local i=`i'+1
matrix t2[1,`i']=`z'
}
mat rowname t2="`program'"
mat colname t2="Start date" "End date" "Average score"
*ssc install estout
estout matrix(t2) using "$out\\`program'_`no'.xls", replace
}
}
My data is as follows:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float today str16 program float(period_grow period_lesedi period_cot_lydenburg) double score 20894 "lesotho_elds" . . . 58.44 20900 "lesotho_elds" . . . 28.769999999999996 21251 "ntataise_fs" . . . 55.68 20894 "lesotho_elds" . . . 37.989999999999995 21241 "khululeka" . . . 33.39 21249 "smartstart" . . . 34.16 21147 "learn_initiative" . . . 24.619999999999997 20992 "learn_initiative" . . . 25.299999999999997 21139 "learn_initiative" . . . 44.86 21143 "learn_initiative" . . . 66.07 21270 "ntataise_fs" . . . 41.56 21265 "ntataise_fs" . . . 31.029999999999998 21248 "sikhula_sonke" . . . 44.51 21122 "smartstart" . . . 45.17999999999999 21258 "cot_lydenburg" . . 1 33.46999999999999 21109 "smartstart" . . . 43.45 20907 "smartstart" . . . 36.63999999999999 21257 "ntataise_fs" . . . 28.889999999999993 21234 "smartstart" . . . 61.30000000000001 20882 "lesotho_elds" . . . 87.52 20900 "lesotho_elds" . . . 39.83999999999999 21137 "learn_initiative" . . . 34.04 20879 "lesotho_elds" . . . 83.91999999999999 20879 "grow" 2 . . 40.78999999999999 20894 "lesotho_elds" . . . 63.510000000000005 21145 "learn_initiative" . . . 54.05 20899 "lesotho_elds" . . . 30.499999999999996 21256 "sikhula_sonke" . . . 32.489999999999995 21269 "lesedi" . 3 . 22.27 21236 "khululeka" . . . 20.68 21271 "ntataise_fs" . . . 29.789999999999996 21143 "learn_initiative" . . . 42.91 21259 "ntataise_fs" . . . 50.76 20891 "lesotho_elds" . . . 37.839999999999996 21271 "ntataise_fs" . . . 61.17999999999999 21250 "smartstart" . . . 59.879999999999995 20885 "lesotho_elds" . . . 35.94 21263 "lesedi" . 3 . 36.65 21262 "lesedi" . 3 . 28.849999999999994 21241 "khululeka" . . . 58.19999999999999 21262 "ntataise_fs" . . . 28.319999999999997 21262 "ntataise_fs" . . . 40.629999999999995 20985 "learn_initiative" . . . 13.01 21249 "cot_lydenburg" . . 1 15.290000000000001 21249 "lesedi" . 3 . 29.749999999999996 21271 "unlim_child_kzn" . . . 52.279999999999994 21249 "lesedi" . 3 . 32.910000000000004 21257 "ntataise_fs" . . . 60.17 21249 "cot_lydenburg" . . 1 34.21 21137 "learn_initiative" . . . 37.42999999999999 21250 "ntataise_fs" . . . 32.19 21257 "ntataise_fs" . . . 74.83 21144 "learn_initiative" . . . 59.41 21129 "khululeka" . . . 39.83 20900 "lesotho_elds" . . . 44.87999999999999 21250 "sikhula_sonke" . . . 63.09 21145 "learn_initiative" . . . 46.99 21249 "lesedi" . 3 . 23.31 21269 "unlim_child_kzn" . . . 19.93 20896 "lesotho_elds" . . . 51.28 20908 "smartstart" . . . 23.429999999999996 21250 "smartstart" . . . 28.169999999999998 21130 "khululeka" . . . 30.669999999999998 21251 "cot_lydenburg" . . 1 32.16 21249 "ntataise_fs" . . . 72.86 21265 "lesedi" . 3 . 20.599999999999998 21270 "cot_macassar" . . . 28.009999999999998 21271 "unlim_child_kzn" . . . 62.91 20893 "grow" 2 . . 39.53999999999999 21249 "sikhula_sonke" . . . 30.240000000000002 20891 "smartstart" . . . 58.92 21228 "smartstart" . . . 61.150000000000006 21269 "ntataise_fs" . . . 35.38 21230 "smartstart" . . . 55.19999999999999 21138 "learn_initiative" . . . 66.00999999999999 21262 "ntataise_fs" . . . 45.569999999999986 20772 "khululeka" . . . 80.36999999999999 21262 "cot_macassar" . . . 29.32 21263 "lesedi" . 3 . 46.51999999999999 21242 "khululeka" . . . 33.47 21115 "smartstart" . . . 37.78999999999999 20891 "smartstart" . . . 30.399999999999995 21250 "ntataise_fs" . . . 40.10999999999999 21256 "cot_macassar" . . . 30.75 21144 "learn_initiative" . . . 65.64 21235 "khululeka" . . . 53.459999999999994 21115 "smartstart" . . . 41.85999999999999 20893 "smartstart" . . . 27.419999999999998 21139 "learn_initiative" . . . 44.94 20899 "lesotho_elds" . . . 70.14 21115 "smartstart" . . . 50.28999999999999 20906 "grow" 2 . . 36.17 20893 "smartstart" . . . 29.38 21104 "smartstart" . . . 43.91999999999999 20901 "smartstart" . . . 47.14 21269 "unlim_child_kzn" . . . 30.02 20894 "smartstart" . . . 36.53999999999999 21145 "learn_initiative" . . . 40.65999999999999 21271 "ntataise_fs" . . . 40.540000000000006 21255 "ntataise_fs" . . . 54 end format %td today
When I run this code I get an error that says "March not found" (ie, the local value of `min' which is March 02, 2018). Can anyone tell me how Stata can store the date value in a way that can be exported into a matrix through a loop?
Thanks
Megan
Comment