capture program drop calcscore
program calcscore,rclass
syntax varlist, PARTition(numlist integer >0) [CALCmethod(string) SCOrename(string)]

local y = 1
	
*di "{bf:Calculs des scores}"	
		
local C = 0
foreach z in `partition' {
	local C = `C' + `z'
}
	
local nbvars : word count `varlist'
if `C' != `nbvars' {
	di in red "The sum of the numbers in the partition option is different from the number of variables precised in varlist"
	exit
}

if "`scorename'" != "" {
	local P:word count `partition'
	local S:word count `scorename'
	if `P'!=`S' {
		di in red "The number of score names given is different from the number of dimensions in the partition option" 
		exit 119
	}
	foreach sco in `scorename' {
		capture confirm variable `sco'
		if !_rc {
			di in red "`sco' is a variable of the dataset. Choose another name"
			exit 119
        }
    }
}

/*
local cpt = 0
if "`sum'" != "" {
	local cpt `cpt' + 1
}
if "`mean'" != "" {
	local cpt `cpt' + 1
}
if "`stand'" != "" {
	local cpt `cpt' + 1
}
if `cpt'>1 {
	di in red "You must choose between mean, sum or stand (the options are exclusive)"
	exit 119
}
*/

local i = 1
foreach x in `partition' {
	tokenize `varlist'
	if `i' == 1 local s = `x'
	else local s = `s' +`x'
	
	local liste = ""
	forvalues w = `y'/`s' {
		local liste `liste' ``w'' 
	}
	
	tempvar nonmiss
	qui egen `nonmiss' = rownonmiss(`liste')
			
	if "`scorename'" != "" {
		tokenize `scorename'
		local sc = "``i''"
		}
	else local sc = "Dim`i'"
	
/*	if "`calc_method'" == "" {
		local calc_method = "mean"
	}
	
	if "`calc_method'" != "sum" & "`calc_method'" != "mean" {
		di in red "The calc_method option is invalid. Choose mean or sum."
		exit 119
	}
	
	if "`calc_method'" == "sum" {
		qui egen `sc' = rowmean(`liste') if `nonmiss' >= `x'/2
		if "`calc_stand'" != "" {
			local maxs = 0
			foreach var in `liste' {
				qui levelsof `var', local(levels)
				local max = 0
				foreach l in `levels' {
					if `l'>`max' local max = `l'
				}
				local maxs = `maxs' + `max'
			}
			di "`sc' : `maxs'"
		    qui replace `sc' = `sc'*`nonmiss'*100/`maxs'
		}
		else qui replace `sc' = `sc'*`nonmiss' 
	}
	else if "`calc_method'" == "mean" {
		qui egen `sc' = rowmean(`liste') if `nonmiss' >= `x'/2
	}
*/
	if "`calcmethod'" == "" local calcmethod = "mean"

	if "`calcmethod'" != "mean" & "`calcmethod'" != "sum" & "`calcmethod'" != "stand" {
		di in red "option calcmethod incorrectly specified (choose among mean, sum and stand)"
		error 198
	}
	
	if "`calcmethod'" == "sum" {
		qui egen `sc' = rowmean(`liste') if `nonmiss' >= `x'/2
		qui replace `sc' = `sc'*`nonmiss'
	}
	
	else if "`calcmethod'" == "stand" {
		
		qui egen `sc' = rowmean(`liste') if `nonmiss' >= `x'/2
		qui replace `sc' = `sc'*`nonmiss'
		
		tempvar min max
		egen `min' = min(`sc')
		egen `max' = max(`sc')
		
		
		/*
		foreach var in `liste' {
			/*qui levelsof `var', local(levels)
			local max = 0
			foreach l in `levels' {
				if `l'>`max' local max = `l'
			}
			local maxs = `maxs' + `max'*/
			
			local max = max(`levels')
		}
		
		*/
		*di "max : "`max'
		*di "min : "`min'
		
		qui replace `sc' = (`sc'-`min')/(`max'-`min')*100
	}
	
	else {
		qui egen `sc' = rowmean(`liste') if `nonmiss' >= `x'/2
	}

local `i++' 
local y = `s'+1
}
end

*calcscore ioc1-ioc37, partition(4 4 7 3 3 4 7 5) scorename(HA PSE W BCC AC AE LI MOC) calcmethod(stand)
*calcscore x1-x40, partition(5 5 5 5 5 5 5 5) calcmethod(stand)
*calcscore sf36_3q_intenses sf36_3q_moderees sf36_3q_soulever sf36_3q_etages sf36_3q_etage sf36_3q_pencher sf36_3q_15km sf36_3q_500m sf36_3q_100m sf36_3q_douche  sf36_4q_limite_temps_travail sf36_4q_moins_choses sf36_4q_type_travail sf36_4q_effort  sf36_7q_intensite_douleurs sf36_8q_douleurs_physiques  sf36_1q sf36_11q_malade sf36_11q_porte_bien sf36_11q_degrade sf36_11q_excellente_sante  sf36_9q_enthousiaste sf36_9q_energie sf36_9q_epuise sf36_9q_fatigue  sf36_6q_vie_sociale sf36_10q_etat_mental  sf36_5q_limite_temps_travail sf36_5q_moins_choses sf36_5q_accomplies_soigneusement  sf36_9q_nerveux sf36_9q_triste sf36_9q_calme sf36_9q_maussade sf36_9q_heureux, partition(10 4 2 5 4 2 3 5) scorename(PF RP BP GH VT SF RE MH) calcmethod(mean)
