/*******************************************************************
/*    				 EXERCICE 2 
/* 		ECHANTILLONNAGE ET CALAGE DU MARGES 
/******************************************************************/


LIBNAME COMPIL     "adresse où trouver la macro Calmar";
OPTIONS SASMSTORE = COMPIL MSTORED NODATE ;

/*---------------------------------------------------*/
/* LA POPULATION :  N = 11600 individus, 11 variables*/
/*---------------------------------------------------*/
DATA population;
set compil.donnees_exo2;
run;

proc format;
value $tranche_age 
	1 = 'de 15 à 25 ans'
	2 = 'de 25 à 29 ans'
	3 = 'de 30 à 39 ans'
	4 = 'de 40 à 49 ans'
	5 = 'de 50 à 64 ans'
	6 = 'de 65 à 69 ans'
	7 = 'plus de 70 ans'
	;

value $niveau_scolaire
	1 = 'inférieur au baccalauréat'
	2 = 'supérieur au baccalauréat'
	;

value $cs
	1 = 'agriculteurs'
	2 = 'artisans, commerçants, chefs d’entreprises, professions libérales'
	3 = 'cadres'
	4 = 'professions intermédiaires'
	5 = 'employés'
	6 = 'ouvriers'
	7 = 'retraités'
	;
run; 
/*---------------------------------------------------------------------------------------------*/
/* RÉPARTITION DE LA POPULATION PAR TRANCHE D’ÂGE ET NIVEAU SCOLAIRE
/* NOMBRE MOYEN D’HEURE PAR SEMAINE CONSACRÉES À LA LECTURE, AU SPORT, PASSÉES DEVANT LA TV
/* NOMBRE MOYEN D’EXPOSITIONS VISITÉES EN UNE ANNÉE 
/* NOMBRE MOYEN DE SÉANCES DE CINÉMA EN UN AN 
/*---------------------------------------------------------------------------------------------*/
proc freq data = population;
tables trage nivsco2   ;
title 'Répartition de la population par tranche d’âge et niveau scolaire ';
format trage $tranche_age.  nivsco2  $niveau_scolaire.;
run;
proc means data = population mean  ;
var  lecture sport tele expo cinema   ;
title 'Vraies moyennes (sur la population)';
run;


/*---------------------------------------------------------------------------------------------*/
/* SÉLECTION D'UN ÉCHANTILLON DE TAILLE n =1 160 SELON UN SONDAGE ALÉATOIRE SIMPLE
/*---------------------------------------------------------------------------------------------*/
PROC SURVEYSELECT DATA     = population
				  STATS
				  METHOD   = SRS 
		          SEED     = 1 
				  SAMPSIZE = 1160
				  OUT	   = echantillon;
RUN;

/*---------------------------------------------------------------------------------------------*/
/* ESTIMATIONS À PARTIR DE L’ÉCHANTILLON DE : 
/*  - LA RÉPARTITION DE LA POPULATION PAR TRANCHE D’ÂGE ET NIVEAU SCOLAIRE 
/*  - DU NOMBRE MOYEN D’HEURE PAR SEMAINE CONSACRÉES À LA LECTURE, AU SPORT, PASSÉES DEVANT LA TV
/*  - DU NOMBRE MOYEN D’EXPOSITIONS VISITÉES EN UNE ANNÉE 
/*  - DU NOMBRE MOYEN DE SÉANCES DE CINÉMA EN UN AN */
/*---------------------------------------------------------------------------------------------*/
PROC SURVEYMEANS DATA 	= echantillon  
				 N 		= 11600
				 SUM  MEAN CLM  ;
				 VAR trage nivsco2 lecture sport tele expo cinema;
				 class trage nivsco2;
				 WEIGHT samplingweight; 
format trage $tranche_age.  nivsco2  $niveau_scolaire.;
RUN;


/*---------------------------------------------------------------------------------------------*/
/* CALAGE DE L'ÉCHANTILLON SUR LA VRAIE STRUCTURE PAR TRANCHE D’ÂGE ET NIVEAU SCOLAIRE. 
/*---------------------------------------------------------------------------------------------*/

/* Justification du choix de ces variables de calage */
title 'Justification du choix de ces variables de calage';
proc means data=population mean;
var expo cinema sport lecture;
class  nivsco2;
run;
proc means data=population mean;
var expo cinema sport lecture;
class trage ;
run;


/* les marges de calage */
DATA marges;
INPUT var $ n mar1 mar2 mar3 mar4 mar5 mar6 mar7;
CARDS;
trage 7 456 784 2212 2490 2664 926 2068 
nivsco2 2 8728 2872 . . . . .
;
run;

/* calage */
title "calage sur marges- méthode linéaire ";
%CALMAR(DATA	=echantillon,	POIDS	 =samplingweight,		IDENT	=IDENTIND,
		DATAMAR	=marges,	    M		 =1,			        
		DATAPOI	=echant_cale1,  POIDSFIN =wcale1,	           LABELPOI=methode lineaire);

title "calage sur marges- méthode raking ratio ";
%CALMAR(DATA	=echantillon,	POIDS	 =samplingweight,		IDENT	=IDENTIND,
		DATAMAR	=marges,	    M		 =2,			
		DATAPOI	=echant_cale2,   POIDSFIN =wcale2,	            LABELPOI=methode raking ratio);

title "calage sur marges- méthode logit ";
%CALMAR(DATA	=echantillon,	POIDS	 =samplingweight,		IDENT	=IDENTIND,
		DATAMAR	=marges,	    M		 =3,			        LO 		= 0.77, 	UP = 1.11, 
		DATAPOI	=echant_cale3,  POIDSFIN=wcale3,	            LABELPOI=methode logit);


/*---------------------------------------------------------------------------------------------*/
/* ESTIMATIONS À PARTIR DE L’ÉCHANTILLON CALE  DE : 
/*  - LA RÉPARTITION DE LA POPULATION PAR TRANCHE D’ÂGE ET NIVEAU SCOLAIRE 
/*  - DU NOMBRE MOYEN D’HEURE PAR SEMAINE CONSACRÉES À LA LECTURE, AU SPORT, PASSÉES DEVANT LA TV
/*  - DU NOMBRE MOYEN D’EXPOSITIONS VISITÉES EN UNE ANNÉE 
/*  - DU NOMBRE MOYEN DE SÉANCES DE CINÉMA EN UN AN */
/*---------------------------------------------------------------------------------------------*/
proc sort data =echantillon;  by IDENTIND; 
proc sort data =echant_cale1; by IDENTIND; 
proc sort data =echant_cale2; by IDENTIND; 
proc sort data =echant_cale3; by IDENTIND; 
data echant_cale; 
merge echantillon 	echant_cale1 	echant_cale2 	echant_cale3; 
by IDENTIND; 
run; 
%macro estimations (poids=);
PROC SURVEYMEANS DATA 	= echant_cale  
				 N 		= 11600
				 SUM  MEAN CLM  ;
				 VAR trage nivsco2 lecture sport tele expo cinema;
				 class trage nivsco2;
				 WEIGHT &poids.; 
format trage $tranche_age.  nivsco2  $niveau_scolaire.;
title "estimations avec pondérations =&poids.";
RUN;
%mend;

%estimations (poids=samplingweight);
%estimations (poids=wcale1);
%estimations (poids=wcale2);
%estimations (poids=wcale3);


