/* Nom : Prof de Math */
/* Date : 18 Octobre 2024 */
/* Examen : SAS */

/* EXERCICE 1 */
/* 1)  Les observations commencent à la ligne 2, les valeurs sont séparées par , */

/* 2) Importer le fichier heart_failure_clinical_records_dataset.csv */
proc import datafile='C:\Users\Gabriela Ciuperca\M2PRO\logiciel_SAS\exam_an2024\heart_failure_clinical_records_dataset.csv'
    out=heart_failure
    dbms=csv
    replace;
    getnames=yes;
run;

/* ou avec INFILE */
data heart_failure;
infile 'C:\Users\Gabriela Ciuperca\M2PRO\logiciel_SAS\exam_an2024\heart_failure_clinical_records_dataset.csv' 
firstobs=2  DLM=",";
input age anaemia creatinine_phosphokinase diabetes ejection_fraction 
high_blood_pressure 
platelets serum_creatinine serum_sodium sex smoking time DEATH_EVENT;
run;
/* le tableau contient 299 observations et 13 variables */
/* OU */
 /* Compter le nombre total d'observations et de variables */
proc contents data=heart_failure;
run;
/* 3) Afficher les 5 premières observations du tableau heart_failure */
proc print data=heart_failure (obs=5);
run;
/*  OU */
data reduit;
  set heart_failure;
  if _N_<6;
  proc print data=reduit;
  run;

/* 4) Diviser le tableau en deux nouveaux tableaux */
data hf_age hf_no_age;
    set heart_failure;
    if age > 0 then do;
        drop age;
        output hf_age;
    end;
    else do;
        drop age;
        output hf_no_age;
    end;
run;

/* Nombre d'observations et de variables pour chaque tableau: 
hf_age: 299 obs, 12 var
hf_no_age: 0 obs */
 
/* 5) Analyse descriptive univariée sur age, serum_creatinine, ejection_fraction, et platelets */
proc means data=hf_age mean median min max std;
    var  serum_creatinine ejection_fraction platelets;
run;

/* 6) Calcul du coefficient de corrélation de Pearson */
proc corr data=hf_age pearson;
    var serum_creatinine ejection_fraction;
run;

/* 7) Test de normalité et histogrammes avec densité normale ajustée */
proc univariate data=hf_age normal;
    var serum_creatinine ejection_fraction;
    histogram serum_creatinine ejection_fraction / normal;
run;

/* 8) Création de nouvelles variables log_creatinine et log_ejection_fraction */
data hf_age;
    set hf_age;
    log_creatinine = log(serum_creatinine);
    log_ejection_fraction = log(ejection_fraction);
run;

/* Test de normalité pour log_creatinine et log_ejection_fraction */
proc univariate data=hf_age normal;
    var log_creatinine log_ejection_fraction;
    histogram log_creatinine log_ejection_fraction / normal;
run;

/* 9) Moyenne des variables log_creatinine et log_ejection_fraction */
proc means data=hf_age mean;
    var log_creatinine log_ejection_fraction;
run;

/* 10) Création de la variable ejection_category */
data hf_age;
    set hf_age;
    if ejection_fraction < 25 then ejection_category = 'faible';
    else if ejection_fraction < 50 then ejection_category = 'moyen';
    else ejection_category = 'élevé';
run;

/* Analyse descriptive pour la variable ejection_category */
proc freq data=hf_age;
    tables ejection_category;
run;

/* Test d'égalité des proportions */
proc freq data=hf_age;
    tables ejection_category / chisq;
run;

/* EXERCICE 2 */
/*  Utilisation de la procédure TRANSPOSE */
proc sort data=heart_failure;
 by DEATH_EVENT;
run;
  
proc transpose data=heart_failure out=hf_transposed;
    var serum_creatinine ejection_fraction platelets;
    by DEATH_EVENT;
run;
proc print data=hf_transposed;
run;

/* EXERCICE 3 */
/* 1) Test de t pour comparer ejection_fraction entre DEATH_EVENT = 1 et DEATH_EVENT = 0 */
proc ttest data=heart_failure;
    class DEATH_EVENT;
    var ejection_fraction;
run;
/* 2) Boxplot */
 proc sort data=heart_failure;
  by DEATH_EVENT;
proc boxplot data=heart_failure;
  plot ejection_fraction*DEATH_EVENT / BOXCONNECT=mean ;
  run;
/* 3) Proc means */
  Proc sort data=heart_failure ;
    by time;
	run;
Proc means data=heart_failure ;
  var serum_creatinine ejection_fraction;
  by time;
  output out=heart_failure2 mean=serum2 ejection2 ;
  run;
  proc print data=heart_failure2 ;
  run;
  /* 4) Graphiques */
  
 proc gplot data=heart_failure2;
	plot (serum2 ejection2)*time / overlay  legend;
	symbol1 interpol=j v=plus color=blue; 
	symbol2 interpol=j v=circle color=red; 
run;

/* EXERCICE 4 */
/* 1) Macro-programme stat_description */
options symbolgen mprint mlogic;
%macro stat_description(tab, V1,V2);
proc sort data=&tab;
  by &V2; 
  Run;
    proc means data=&tab mean noprint;
        var &V1;
		by &V2;
	output out=sortie mean=moy;
%mend stat_description;

/* Tester sur heart_failure */
%stat_description(heart_failure, creatinine_phosphokinase,age);
  
/*  2)  */
%macro plot_evol(tab1, V3, V4);
   proc gplot data=&tab1;
	plot &V3*&V4 / legend;
	symbol1 interpol=j v=plus color=blue; 
run;
%mend plot_evol;

/* Tester sur sortie */
%plot_evol(sortie,moy,age);
