* Tcm : Estimation pour une entité géographique donnée de la région centre des taux de mortalitré par age
* quinquennal, et du TCM, avec possibilité de comparer ce dernier à une valeur théorique
*
* Version 2.6 : 21 janvier 2005  (corrections pour le calcul test du TCM à une valeur de référence)
* Version 2.5 : 13 janvier 2005  (corrections pour le calcul du nb de décès observés avec ICMPART)
* Version 2.4 : 11 janvier 2005  (quelques améliorations par rapport à la version 2.3)
* Version 2.3 : 6 janvier 2005  (options ICM et ICMPART, age maximal par defaut à 95)
* Version 2.2 : 16 décembre 2004
* Version 2.1 : 7 décembre 2004
* Version 2 : 22 octobre 2004
* Version 1.4 : 2 septembre 2004
* Version 1.3: 16 aout 2004
* Version 1.2: 22 juillet 2004
* Version 1.1: 16 juillet 2004
* Version 1: 16 juillet 2004
*
* Jean-benoit Hardouin, Observatoire Régional de la Santé du Centre - Orléans - France
* jean-benoit.hardouin@orscentre.fr
*
* Copyright 2004-2005 Jean-Benoit Hardouin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
************************************************************************************************************
program define tcm,rclass
version 7.0
syntax  [, s10(numlist) agemax(int 95) sexe(int 0) sexestd(int 0) anneepop(int 1999) anneepopstd(int 1999) annees(numlist) anneesstd(numlist) codegeo(int 1) codegeostd(int 0) prema(int 65) details tcmcomp(real 0) icm icmpart(numlist)]


/*********************************************************************************************
TESTS PRELIMINAIRES
*********************************************************************************************/

if `sexe'<0|`sexe'>2 {
    di in red "Vous devez indiquer un sexe parmi 0 (deux sexes confondus), 1 (hommes) ou 2 (femmes)"
    exit
}
if `sexestd'<0|`sexestd'>2 {
    di in red "Vous devez indiquer un sexe de reference parmi 0 (deux sexes confondus), 1 (hommes) ou 2 (femmes)"
    exit
}
if `anneepop'<82|(`anneepop'>99&`anneepop'<1982)|`anneepop'>1999 {
    di in red "Vous devez indiquer une annee de reference entre 1982 et 1999 [82 et 99] (compris)"
    exit
}
if `codegeo'==0 {
   di in red "Il n'est pas encore possible de travailler sur la France entiere, mais cela viendra bien assez tot"
   exit
}

local st = "$S_TIME"


/*********************************************************************************************
SI UNE BASE EST OUVERTE, ON LA SAUVEGARDE
*********************************************************************************************/

capture su *
if _rc==0 {
   tempfile tmp
   if "`details'"!="" {
       di in green "** Sauvegarde en cours"
   }
   qui save `tmp',replace
}
preserve

/*********************************************************************************************
PAR DEFAUT
*********************************************************************************************/


if "`s10'"=="" {
    local s10des Toutes les causes
    local s10 101(1)105 201(1)231  301 302 401 501 502 601 602 701(1)707 801(1)806 901(1)903 1001 1101 1201 1301 1401 1501 1600(1)1603 1701(1)1709
}
else {local s10des `s10'}

if "`annees'"=="" {
    local annees 1998(1)2000
}
local anneesdes `annees'

if "`anneesstd'"=="" {
    local anneesstd `annees'
}
local anneesdesstd `anneesstd'

/*********************************************************************************************
VERIFICATION ICM
Pour calculer un ICM, il faut que le sexe, la période et la population de référence
*********************************************************************************************/

if `sexestd'!=`sexe'&"`icm'"!="" {
   di in yellow "Le sexe de la population de reference (option sexestd) a ete modifie pour etre identique a celui de la population de travail (option sexe) et a desormais pour valeur `sexe'"
   local sexestd=`sexe'
}
if "`anneesstd'"!="`annees'"&"`icm'"!="" {
   di in yellow "La periode utilisee pour la population de reference (option anneesstd) a ete modifiee pour etre identique a celle utilisee pour la population de travail (option annees) et a desormais pour valeur `annees'"
   local anneesstd `annees'
}
if "`anneepopstd'"!="`anneepop'"&"`icm'"!="" {
   di in yellow "La population de reference (option anneepopstd) a ete modifiee pour etre identique a la population de travail (option anneepop) et a desormais pour valeur `anneepop'"
   local anneepopstd `anneepop'
}
if "`icm'"==""&"`annees'"=="`anneesstd'"&`sexe'==`sexestd' {
   di in yellow "Les options indiquees semblent etre correctes pour calculer un ICM"
   local icm icm
}
else if "`icm'"==""{
   di in yellow "Les options indiquees ne semblent pas etre correctes pour calculer un ICM : Attention en utilisant celui-ci"
}

/*********************************************************************************************
SI LA STANDARDISATION SE FAIT SUR LA FRANCE
*********************************************************************************************/


if `codegeostd'==0 {
   use "C:\ado\personal\files\dcfrance8000long.dta", clear
   qui gen flagstd=0
   foreach i of numlist `anneesstd' {
          qui replace flagstd=1 if annee==`i'
   }
   qui keep if flagstd==1

   qui replace flagstd=0
   foreach i of numlist `s10' {
          qui replace flagstd=1 if s10==`i'
   }
   qui keep if flagstd==1

   qui replace flagstd=0
   if `sexestd'!=0 {
          qui replace flagstd=1 if sexe==`sexestd'
          qui keep if flagstd==1
   }

   qui gen n=1
   foreach i of numlist 0 1 5(5)`=`agemax'-5' {
           qui su n if age==`i' [weight=eff]
           local Nstd`i'=r(sum)
   }
   qui su n if age>=`agemax' [weight=eff]
   local Nstd`agemax'=r(sum)
}



/*********************************************************************************************
ON OUVRE LA BASE DE MORTALITE
*********************************************************************************************/

if "`details'"!="" {
    di in green "** Ouverture de la base"
}
use "C:\ado\personal\files\dccentre8000reduit.dta",replace

if "`details'"!="" {
    di in green "** Base ouverte"
}

/*********************************************************************************************
ON SELECTIONNE LES INFORMATIONS
*********************************************************************************************/


if "`details'"!="" {
    di in green "** Selection de l'information necessaire"
}



numlist "`annees'"
local nbans:word count `r(numlist)'
local mod=mod(`nbans',2)
if `mod'==0 {
  local juillet=""
}
else if `mod'==1 {
  local juillet="juillet"
}
if `codegeo'<100&"`juillet'"=="" {
  local commpoptr="Estimation INSEE au 1er janvier"
}
else if `codegeo'<100&"`juillet'"!="" {
  local commpoptr="Estimation INSEE au 1er juillet"
}
else if `codegeo'>100 {
  local commpoptr="Rétropolation entre les recencements pour"
}

numlist "`anneesstd'"
local nbansstd:word count `r(numlist)'
local mods=mod(`nbansstd',2)
if `mods'==0 {
  local juilletstd=""
}
else if `mods'==1 {
  local juilletstd="juillet"
}
if `codegeostd'<100&"`juilletstd'"=="" {
  local commpopstd="Estimation INSEE au 1er janvier"
}
else if `codegeostd'<100&"`juilletstd'"!="" {
  local commpopstd="Estimation INSEE au 1er juillet"
}
else if `codegeostd'>100 {
  local commpopstd="Rétropolation entre les recencements pour"
}


tempvar keeps10 keepsexe keepannees  keepgeo
gen `keeps10'=0
gen `keepsexe'=0
gen `keepannees'=0
gen `keepgeo'=0

if "`details'"!="" {
    di in green "** Selection en fonction du sexe"
}

if `sexe'==0 {
   qui replace `keepsexe'=1
}
else {
   qui replace `keepsexe'=1 if sexe==`sexe'
}

qui keep if `keepsexe'==1


if "`details'"!="" {
    di in green "** Selection des annees"
}

foreach i of numlist `annees' {
   qui replace `keepannees'=1 if annee==`i'
}

qui keep if `keepannees'==1

if "`details'"!="" {
    di in green "** Selection de la zone geographique"
}

if `codegeo'==1 {
   qui replace `keepgeo'=1
}

if `codegeo'>10&`codegeo'<100 {
   qui replace `keepgeo'=1 if deptdom==`codegeo'
}

if `codegeo'>1000&`codegeo'<10000&int(`codegeo'/100)!=24 {
   qui replace `keepgeo'=1 if codepct==`codegeo'
}

if int(`codegeo'/100)==24 {
   qui replace `keepgeo'=1 if ze94==`codegeo'
}

if `codegeo'>10000&`codegeo'<100000 {
   qui replace `keepgeo'=1 if comdom==`codegeo'
}

qui keep if `keepgeo'==1


if "`details'"!="" {
    di in green "** Selection des codes S10"
}

foreach i of numlist `s10' {
   qui replace `keeps10'=1 if S10==`i'
}

qui keep if `keeps10'==1


if "`details'"!="" {
    di in green "** Selection finale"
}

qui keep if `keeps10'==1&`keepsexe'==1&`keepannees'==1&`keepgeo'==1

if "`details'"!="" {
    di in green "** Selection terminee"
}

/*********************************************************************************************
ON CHERCHE LES INTITULES DE ZONE, DE SEXE POUR LA POPULATION DE TRAVAIL
*********************************************************************************************/

qui count
local N=r(N)
qui count if age!=0
local N0=r(N)
if `codegeo'==1 {local zone region Centre}
else if `codegeo'==18 {local zone Cher}
else if `codegeo'==28 {local zone Eure-et-Loir}
else if `codegeo'==36 {local zone Indre}
else if `codegeo'==37 {local zone Indre-et-Loire}
else if `codegeo'==41 {local zone Loir-et-Cher}
else if `codegeo'==45 {local zone Loiret}
else {
     qui trouve `codegeo'
     local zone `r(trouve`codegeo')'
     if "`zone'"=="" {
        local zone Non reconnue
     }
}

if `sexe'==0 {local sexedes Deux sexes}
else if `sexe'==1 {local sexedes Hommes}
else if `sexe'==2 {local sexedes Femmes}
else {local sexedes Non reconnu}

/*********************************************************************************************
ON CHERCHE LES INTITULES DE ZONE, DE SEXE POUR LA POPULATION DE STANDARDISATION
*********************************************************************************************/


if `sexestd'==0 {local sexerefdes Deux sexes}
else if `sexestd'==1 {local sexerefdes Hommes}
else if `sexestd'==2 {local sexerefdes Femmes}
else {local sexerefdes Non reconnu}


if `codegeostd'==0 {local zonestd France metropolitaine}
else if `codegeostd'==1 {local zonestd region Centre}
else if `codegeostd'==18 {local zonestd Cher}
else if `codegeostd'==28 {local zonestd Eure-et-Loir}
else if `codegeostd'==36 {local zonestd Indre}
else if `codegeostd'==37 {local zonestd Indre-et-Loire}
else if `codegeostd'==41 {local zonestd Loir-et-Cher}
else if `codegeostd'==45 {local zonestd Loiret}
else {
     qui trouve `codegeostd'
     local zonestd `r(trouve`codegeostd')'
     if "`zonestd'"=="" {
        local zonestd Non reconnue
     }
}

/*********************************************************************************************
ENTETE DE SORTIE
*********************************************************************************************/

di in green "- POPULATION DE TRAVAIL -"
di in green "Zone geographique : " in yellow "`zone'"
di in green "Periode : " in yellow "`anneesdes'" in green " (" in yellow "`nbans' an(s)" in green ")"
di in green "Population : " in yellow "`commpoptr' `anneepop'"
di in green "Sexe : " in yellow "`sexedes'"
di
di in green "- POPULATION DE REFERENCE -"
di in green "Zone geographique : " in yellow "`zonestd' "
di in green "Periode : " in yellow "`anneesdesstd'" in green " (" in yellow "`nbansstd' an(s)" in green ")"
di in green "Population : " in yellow "`commpopstd' `anneepopstd'"
di in green "Sexe: " in yellow "`sexerefdes'"
di
di in green "Deces prematures : <" in  yellow "`prema'" in green  " ans"
di in green "S10 : " in yellow "`s10des'"
di

/*********************************************************************************************
NB DE DECES ET DE DECES PREMATURES
*********************************************************************************************/

di in green "Nombre de deces concernes : " in yellow `N' _col(55) in green "Nombre de deces concernes >0 an : " in yellow `N0'
qui count if age<`prema'
local Nprema=r(N)
local Pprema=`Nprema'/`N'
qui count if age<`prema'&age>0
local N0prema=r(N)
local P0prema=`N0prema'/`N0'
di in green "Nombre de deces prematures : " in yellow `Nprema' in green " (" in yellow %5.1f `=`Pprema'*100' in green "%)"  _col(55) in green "Nombre de deces prematures >0 an : " in yellow `N0prema' in green " (" in yellow %5.1f `=`P0prema'*100' in green "%)"

/*********************************************************************************************
TAILLE DE POPULATION DE LA POPULATION DE TRAVAIL
*********************************************************************************************/

tempvar agerec
gen `agerec'=age
qui recode `agerec' 0=0  `agemax'/105=`agemax'

local trancheage 0 1 5(5)`agemax'

qui estpop , annee(`anneepop') codegeo(`codegeo') sexe(`sexe') `juillet'
foreach i of numlist 0 1 5(5)`=`agemax'-5' {
         local popr`i'=r(poprq`i')
}
local popr`agemax'=0
foreach i of numlist `agemax'(5)95 {
         local popr`agemax'=`popr`agemax''+r(poprq`i')
}

local popr=r(popr)/*-r(popr0)*/


/*********************************************************************************************
TAILLE DE POPULATION DE LA POPULATION DE STANDARDISATION
*********************************************************************************************/


qui estpop , annee(`anneepopstd') codegeo(`codegeostd') sexe(`sexestd') `juilletstd'
foreach i of numlist 0 1 5(5)`=`agemax'-5' {
         local pop`i'=r(poprq`i')
}
local pop`agemax'=0
foreach i of numlist `agemax'(5)95 {
         local pop`agemax'=`pop`agemax''+r(poprq`i')
}
local pop=r(popr)/*-r(popr0) */

/*********************************************************************************************
TABLEAU DE SORTIE
*********************************************************************************************/

di
di in green "{hline 14}{c TT}{hline 33}{c TT}{hline 33}{c TT}{hline 20}"
di in green _col(15) "{c |}" _col(21) "POPULATION DE TRAVAIL" _col(49) "{c |}" _col(55) "POPULATION DE REFERENCE" _col(83) "{c |}"
di in green "{hline 14}{c +}{hline 33}{c +}{hline 33}{c +}{hline 20}"
di in green "Tranche"  _col(15) "{c |}" _col(16) "Population"   _col(29) "Nombre"      _col(37) "Taux brut de" _col(49) "{c |}" _col(50) "Population" _col(61) "Nombre de"    _col(71) "Taux brut de" _col(83) "{c |}" _col(85) "Nb de deces"
di in green "d'age"    _col(15) "{c |}" _col(22) "`anneepop'" _col(27) "de deces"  _col(39) "mortalite*" _col(49) "{c |}" _col(56) "`anneepopstd'"     _col(65) "deces" _col(73) "mortalite*" _col(83) "{c |}"   _col(88) "attendus" _col(100) "ICM"
di in green "{hline 14}{c +}{hline 33}{c +}{hline 33}{c +}{hline 20}"

/*********************************************************************************************
CALCUL CHEZ LES 0 AN DU TAUX BRUT
*********************************************************************************************/

local tcm=0
local vartcm=0
local varicm=0
local apvp=0

/*qui count if `agerec'==0
local Nage0=r(N)
local txbrut0=`Nage0'/`popr0'*100000/`nbans'
local txstd0=`Nstd0'/`pop0'*100000/`nbansstd'
local NE0=`popr0'*`txstd0'/100000*`nbans'
local apvp0=`Nage0'*`prema'
di in green "O an" _col(15) "{c |}" _col(18) in yellow %8.0f `popr0' _col(27) %8.0f `Nage0' _col(40) %8.1f `txbrut0' _col(49) in green "{c |}" in yellow _col(52) %8.0f `pop0' _col(62) %8.0f `Nstd0' _col(74) %8.1f `txstd0' _col(83) in green  "{c |}" _col(88) in yellow  %8.1f `NE0' _col(97) %6.1f `=`Nage0'/`NE0'*100'
di in green "{dup 14:-}{c +}{dup 33:-}{c +}{dup 33:-}{c +}{dup 20:-}"
*/
/*********************************************************************************************
CALCUL POUR CHAQUE TRANCHE D'AGE DU TAUX BRUT ET DE LA CONTRIBUTION AU TCM
*********************************************************************************************/
*set trace on
if "`icmpart'"!="" {
  local icmpartmin:word 1 of `icmpart'
  local icmpartmax:word 2 of `icmpart'
  qui count if `agerec'>=`icmpartmin'&`agerec'<=`icmpartmax'
  local Npart=r(N)
}

local N0std=0
local NE=0
local NEpart=0
foreach i of numlist `trancheage' {
  qui count if `agerec'==`i'
  local Nage`i'=r(N)
  if `i'==0 {
    local tranche 0 an
  }
  else if `i'==1 {
    local tranche 1-4 ans
    local apvp1=`Nage`i''*62
    local apvp=`apvp'+`apvp1'
  }
  else if `i'==`agemax' {
    local tranche `agemax' ans et plus
  }
  else {
    local tranche `i'-`=`i'+4' ans
    if `i'<65 {
        local apvp`i'=`Nage`i''*(62.5-`i')
        local apvp=`apvp'+`apvp`i''
    }
  }
  local N0std=`N0std'+`Nstd`i''
  local txbrut`i'=`Nage`i''/`popr`i''*100000/`nbans'
  local txstd`i'=`Nstd`i''/`pop`i''*100000/`nbansstd'
  local NE`i'=`popr`i''*`txstd`i''/100000*`nbans'
  local NE=`NE'+`NE`i''
  if "`icmpart'"!="" {
    if `icmpartmin'<=`i'&`icmpartmax'>=`i' {
       local NEpart=`NEpart'+`NE`i''
    }
  }
  local icm`i'=`Nage`i''/`NE`i''*100
  *local varicm=`varicm'+`icm`i''
  local tcm=`tcm'+`txbrut`i''*`pop`i''/`pop'
  local vartcm=`vartcm'+(`pop`i''/`pop')^2*`txbrut`i''/100000/`popr`i''
  di in green "`tranche'" _col(15) "{c |}" _col(18) in yellow %8.0f `popr`i'' _col(27) %8.0f `Nage`i'' _col(40) %8.1f `txbrut`i'' _col(49) in green "{c |}" in yellow _col(52) %8.0f `pop`i'' _col(62) %8.0f `Nstd`i'' _col(74) %8.1f `txstd`i'' _col(83) in green "{c |}" in yellow _col(88) %8.1f `NE`i'' _col(97) %6.1f `icm`i''
}
di in green "{hline 14}{c +}{hline 33}{c +}{hline 33}{c +}{hline 20}"

/*********************************************************************************************
TAUX BRUT GLOBAL
*********************************************************************************************/

local txbrut=`N'/`popr'*100000/`nbans'
local txbrutstd=`=`N0std'/`pop'*100000/`nbansstd''
local icm=`N'/`NE'*100
*local A=`varicm'
local varicm=`icm'^2/`N'
di in green "Total" _col(15) "{c |}" in yellow _col(18) %8.0f `popr' _col(27) %8.0f `N' _col(40) %8.1f `txbrut' _col(49) in green "{c |}" in yellow _col(52) %8.0f `pop' _col(62) %8.0f `N0std' _col(74) %8.1f `=`N0std'/`pop'*100000/`nbansstd'' in green _col(83) "{c |}" in yellow _col(88) %8.1f `NE' _col(97) %6.1f `icm'
di in green "{hline 14}{c BT}{hline 33}{c BT}{hline 33}{c BT}{hline 20}"

di in green "APVP=" _col(18) in yellow %10.1f `apvp'

/*********************************************************************************************
TCM
*********************************************************************************************/


local tcmicmoins=(`tcm'/100000-1.96*sqrt(`vartcm'))*100000
local tcmicplus=(`tcm'/100000+1.96*sqrt(`vartcm'))*100000
local icmicmoins=`icm'*(1-1/(9*`N')-1.96/(3*sqrt(`N')))^3
local icmicplus=(`N'+1)/`NE'*(1-1/(9*(`N'+1))+1.96/(3*sqrt(`N'+1)))^3*100

di in green "TCM" _col(20) in yellow %8.2f `tcm' _col(35) in green "Ecart type" _col(45) in yellow %8.2f `=sqrt(`vartcm')*100000' _col(61) in green "[" in yellow %8.2f `tcmicmoins' in green ";" in yellow %8.2f `tcmicplus' in green "]"
di in green "ICM" _col(20) in yellow %8.2f `icm'  _col(35) in green "Ecart type" _col(45) in yellow %8.2f `=sqrt(`varicm')'        _col(61) in green "[" in yellow %8.2f `icmicmoins' in green ";" in yellow %8.2f `icmicplus' in green "] (methode de Byar)"
local Zicm=(abs(`N'-`NE')-.5)/sqrt(`NE')
local pvalueicm=1-norm(abs(`Zicm'))
di in green "H0: ICM=100" _col(35) "Z=" in yellow %6.4f `Zicm' _col(45) in green "p=" in yellow %6.4f `pvalueicm'

/*********************************************************************************************
ICM PARTIEL
*********************************************************************************************/

if "`icmpart'"!="" {
  di in green "{dup 103:-}"
  if `icmpartmax'==`agemax' {
    local icmpartmaxdis "`icmpartmax' et plus"
  }
  else {
    local icmpartmaxdis=`icmpartmax'+4
  }
  di in green "ICM Partiel pour les `icmpartmin'/`icmpartmaxdis'"
  di in green "Deces observes : " _col(20) in yellow %8.0f `Npart'
  di in green "Deces attendus : " _col(20) in yellow %8.2f `NEpart'
  local icmpart=`Npart'/`NEpart'*100
  local varicmpart=`icmpart'^2/`Npart'
  local icmparticmoins=`icmpart'*(1-1/(9*`Npart')-1.96/(3*sqrt(`Npart')))^3
  local icmparticplus=(`Npart'+1)/`NEpart'*(1-1/(9*(`Npart'+1))+1.96/(3*sqrt(`Npart'+1)))^3*100
  local Zicmpart=(abs(`Npart'-`NEpart')-.5)/sqrt(`NEpart')
  local pvalueicmpart=1-norm(abs(`Zicmpart'))
  di in green "ICM (`icmpartmin'/`icmpartmaxdis')" _col(20) in yellow %8.2f `icmpart' _col(35) in green "Ecart type" _col(45) in yellow %8.2f `=sqrt(`varicmpart')' _col(61) in green "[" in yellow %8.2f `icmparticmoins' in green ";" in yellow %8.2f `icmparticplus' in green "] (methode de Byar)"
  di in green "H0: ICM(`icmpartmin'/`icmpartmaxdis')=100" _col(35) "Z=" in yellow %6.4f `Zicmpart' _col(45) in green "p=" in yellow %6.4f `pvalueicmpart'
  return scalar ICMpart=`icmpart'
  return scalar ZICMpart=`Zicmpart'
  return scalar pvalueICMpart=`pvalueicmpart'
}


di in green "{hline 103}"
di in green "*: par an pour 100 000 habitants"
*set trace on


/*********************************************************************************************
COMPARAISON DU TCM (TEST)
*********************************************************************************************/

if `tcmcomp'!=0 {
   local Z=(`tcm'-`tcmcomp')/100000/(sqrt(`vartcm'))
   local pvalue=1-norm(abs(`Z'))
   di
   di in green "Comparaison du TCM a la valeur theorique (`tcmcomp') : Z= " in yellow %8.4f `Z' in green " p= " in yellow %6.4f `pvalue'
   return scalar ZTCM=`Z'
   return scalar pvalueTCM=`pvalue'
}

/*********************************************************************************************
SORTIES
*********************************************************************************************/

return scalar N0=`N0'
return scalar APVP=`apvp'
return scalar Popref=`popr'
return scalar TCM=`tcm'
return scalar TCMICplus=`tcmicplus'
return scalar TCMICmoins=`tcmicmoins'
return scalar ICM=`icm'
return scalar ICMICmoins=`icmicmoins'
return scalar ICMICplus=`icmicplus'
return scalar N0prema=`N0prema'
return scalar ZICM=`Zicm'
return scalar pvalueICM=`pvalueicm'
return scalar txbrut=`txbrut'
return scalar txbrutstd=`txbrutstd'
tempname matsave
numlist "0 1 5(5)`agemax'"
local j `r(numlist)'
local l:word count `j'
matrix define `matsave'=J(`l',9,0)
forvalues i=1/`l' {
    local k:word `i' of `j'
    matrix `matsave'[`i',1]=`k'
    matrix `matsave'[`i',2]=`Nage`k''
    matrix `matsave'[`i',3]=`popr`k''
    matrix `matsave'[`i',4]=`txbrut`k''
    matrix `matsave'[`i',5]=`pop`k''
    matrix `matsave'[`i',6]=`Nstd`k''
    matrix `matsave'[`i',7]=`txstd`k''
    matrix `matsave'[`i',8]=`NE`k''
    matrix `matsave'[`i',9]=`icm`k''
}
matrix colnames `matsave'="classe d'age" "nb deces" "pop" "taux brut" "pop reference" "nb deces reference" "taux brut reference" "deces attendus" "icm"
return matrix donnees `matsave'

/*********************************************************************************************
RECUPERATION SI BESOIN DE LA BASE INITIALE
*********************************************************************************************/

restore,not
qui drop _all
if "`tmp'"!="" {
   if "`details'"!="" {
       di
       di in green "** Restauration"
   }
   qui use `tmp'
}
if "`details'"!="" {
    capture qui elapse `st'
    di in green "** Temps d'execution : " in yellow "$S_elap " in green "secondes"
}

end