Table des mati`eres
Transcription
Table des mati`eres
Table des matières 1 SAS 1.1 Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 Environnement de programmation . . . . . . . . . . . . . . . . . . 1.3 Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.4 Création d’une table . . . . . . . . . . . . . . . . . . . . . . . . . . 1.5 Procédure print . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.6 Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.7 Procedures graphiques . . . . . . . . . . . . . . . . . . . . . . . . . 1.7.1 La procédure gplot . . . . . . . . . . . . . . . . . . . . . . . 1.7.2 Table d’annotation . . . . . . . . . . . . . . . . . . . . . . . 1.7.3 Définition des types de traits symbolx . . . . . . . . . . . . 1.7.4 Trace des axes . . . . . . . . . . . . . . . . . . . . . . . . . 1.7.5 Parametres graphiques globaux . . . . . . . . . . . . . . . . 1.8 Création de documents, images, Ods . . . . . . . . . . . . . . . . . 1.8.1 Sortie print vers html . . . . . . . . . . . . . . . . . . . . . 1.8.2 Sortie gplot vers gif,png,ps . . . . . . . . . . . . . . . . . . 1.8.3 Sorties multiples . . . . . . . . . . . . . . . . . . . . . . . . 1.9 Exemples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.9.1 Inclusion de programme sas . . . . . . . . . . . . . . . . . . 1.9.2 Création d’une table à partir d’un fichier . . . . . . . . . . 1.9.3 Premère sortie graphique . . . . . . . . . . . . . . . . . . . 1.9.4 Procédure graphique . . . . . . . . . . . . . . . . . . . . . . 1.9.5 Procédure graphique : superposition . . . . . . . . . . . . . 1.9.6 Utilisation d’une table dans une librairie . . . . . . . . . . . 1.9.7 Utilisation de procédures . . . . . . . . . . . . . . . . . . . 1.9.8 Data : lecture formattée . . . . . . . . . . . . . . . . . . . . 1.9.9 Data : infile . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.9.10 Data : concaténation de lignes . . . . . . . . . . . . . . . . 1.9.11 Data : concaténation de colonnes . . . . . . . . . . . . . . . 1.9.12 Data : Concaténation de colonnes, correspondance de lignes 1.9.13 Data :Concaténation de colonnes de longueur différentes . . 1.9.14 Data :Labels . . . . . . . . . . . . . . . . . . . . . . . . . . 1.9.15 Data :Format et date . . . . . . . . . . . . . . . . . . . . . 1.9.16 Data :Rename . . . . . . . . . . . . . . . . . . . . . . . . . 1.9.17 Calcul . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.9.18 Initialisation . . . . . . . . . . . . . . . . . . . . . . . . . . 1.9.19 Génrarateur aléatoire . . . . . . . . . . . . . . . . . . . . . 1.9.20 Boucle DO tabulation . . . . . . . . . . . . . . . . . . . . . 1.9.21 SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.9.22 Tableau . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.9.23 OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.9.24 Création de plusietrs tables . . . . . . . . . . . . . . . . . . 1.9.25 Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.9.26 Fonction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.9.27 Diagramme en baton . . . . . . . . . . . . . . . . . . . . . . 1.9.28 Graphique 3d . . . . . . . . . . . . . . . . . . . . . . . . . . 1.9.29 Axis,goptions . . . . . . . . . . . . . . . . . . . . . . . . . . 1.9.30 Sortie gif html . . . . . . . . . . . . . . . . . . . . . . . . . 1.9.31 Sortie mode transparent . . . . . . . . . . . . . . . . . . . . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 4 4 4 4 5 6 6 6 7 7 8 8 9 9 9 10 11 11 11 11 12 12 13 14 14 15 16 16 16 17 17 17 18 18 19 19 20 20 20 21 21 21 22 22 23 23 24 25 2 TABLE DES MATIÈRES 1.9.32 Sortie Postscript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.9.33 Procédure INSIGHT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 26 Chapitre 1 SAS 3 Université de CAEN 1.1 28 septembre 2011 M2-IAD MI5 Statistiques Web Le site SAS http://support.sas.com. 1.2 Environnement de programmation On notera les quatres fenêtres essentielles : – la fenêtre nommée Log, elle contient les sorties de l’exécution – la fenêtre nommée editor contient le programme – la fenêtre nommée results contient les résultats demandés par le programme – la fenêtre explorateur permettant de parcourir les données gérées par SAS. Les ordres ne sont pas exécutés par un retour chariot (SCILAB,R) mais pas une sélection de la zone de programme à exécuter puis à une soumission au module de calcul de SAS. Les données sont presque uniquement des matrices appelées tables. Les tables sont sauvegardées dans des librairies. Par défaut la librairie de travail s’appelle work. 1.3 Instructions Les instructions peuvent être des – des instructions globales – une création de table ou étape de calcul – appel d’une procédure – des macros Toutes ses instructions se terminent pas des points virgules. appel à une procédure création d’une table définition de raccourci définition d’une librairie titre global inclusion de fichier inclusion de fichier definition librairie defaut 1.4 Exemples importants proc nom data=table options;instruction;[ins.;];run; data nom_de_la_table;instuctions;run; filename nom "C:\users\francois\test.txt"; libname travail "C:\users\francois\SAS\3-jan-03" title "nom du travail"; %include 'nom_complet_du_fichier'; %include raccourci(fichier); options user='nom repertoire'; Création d’une table Les variables d’une table(colonnes de la matrice) sont soit numériques soient des caractères. L’instruction data permet de créer une nouvelle table à partir – d’un fichier – de données incluses dans le programme – de calculs fait à partir d’une ancienne table C’est une boucle implicite sur les lignes du flot d’entrée. La nouvelle table contient toutes les variables utilisées dans l’instruction data. A chaque nouvelle exécution une nouvelle ligne est crée. Pour créer une table on utilisera la syntaxe suivante : data [librairie.]ma_table [[lib.]nom_des_autres_tables] instructions options; run; La syntaxe est : Syntaxe librairie nom de la librairie ma_table nom de la table crée nom_des_autres_tables_crees noms des autres tables [instructions [options]]; instructions de la procédure http://www.math.unicaen.fr/~kauffman/cours 4 [email protected] Université de CAEN 28 septembre 2011 M2-IAD MI5 Statistiques Instructions entree-sortie de la commande data données dans le programme cards donnees; à la fin données dans le programme datalines donnees;à la fin lecture input x1 [$][n] [x--y] [xx [$] [x--y]] [ [@] [@@] ]; x1 nom de la première variable lue xx autres variables $ la x1 est alphanumérique (format de lecture) n on lit n caractères valeur de x1 x--y on lit la valeur de x1 entre la colonne x et la colonne y @ on ne passe pas à la ligne dans le flot d’entree @@ répetition de la lecture jusqu’à la fin de la ligne nom etendu de variables label var_1='nom etendu 1' var_2='nom etendu2 ..'; sortie defaut la nouvelle table contient toutes les variables lues par defaut sortie table output [une autre_table] entree tables (par lignes) set [lib.]anc_table[(options)] [[lib.]anc_table2] lib nom de la librairie anc_table nom de l’ancienne table [lib.]anc_table2] concatene les tables par lignes options selection d’observations where=((var >x) and( var ='X') selection de variables keep= var1 var2 var3 suppression de variables drop var1 var2 var3 entree fichier infile input [delimiter=c firstobs=y obs=z ...]; input variable filename delimiter=c caractère délimitant les variables (blanc) firstobs=y première ligne lue obs=z dernière ligne lue fusion tables (par colonnes) merge table_1 table_2 [by var_1[var_2]] les variables var_x doivent correspondre Instructions de calcul de la commande data valeur manquante numérique . valeur manquante caractère ”” création de la variable variable=x; opérateur logique =,AND,OR,NOT,&,,ˆ| (test) test si de la variable vaut 1 ou 0 test if test then instruction;[else instruction;end;] instruction composée do;inst_1;inst_2;...;end; boucle do i=debut to fin [by step];inst_1;inst_2;...;end; switch select; when (test) inst1;...;otherwise inst2;end; tableau array t{entier} var_1-var_x vary met les variables de var 1 à var x et var y dans t ayant 4 colonnes renomage de variables rename ancien_nom=nouveau_nom ...; initialisation d’une variable retain x1 val1 [x2 val2]; suppression de variables drop var_1 ... valeur précédente lag(var_1) fonctions usuelles sin,sqrt,.. fonctions statistiques normal(seed),...voir SAS Functions afficher dans la fen. log put "variable=" variable; afficher toutes les variables put _all_; lecture formattée y=put(x,format); 1.5 Procédure print Pour imprimer une table en selectionnant des observations (options), si la table n’est pas spécifiée la table imprimée est la dernière utilisée. proc print [data=librairie.table(options)]; title "Titre"; run; http://www.math.unicaen.fr/~kauffman/cours 5 [email protected] Université de CAEN 1.6 28 septembre 2011 M2-IAD MI5 Statistiques Format Permet d’associer un libelle long à une ou des valeurs ou un intervalle devaleurs de variables. On définit des formats par la procedure format. On asssocie alors des formats d’entree ou de sortie à des variables d’une table. proc format; value format1 val_1='libelle long 1' val_2='libelle long 2' ...; value format2 val_1='autre libelle long 1' val_2='autre libelle long 2' ...; value format3 low-5='Tres basse' 6-10='Medianne' 21-high='Tres haute'; run; Instructions de formattage entree/sortie dans data exemples de format ”01JAN60” var_date date7. ”01JAN60 :00 :20 :34” var_date_time datetime16. format de lecture informat var_1 format_1. var_2 format_2. ...; format d’affichage format var_1 format1. var_2 format2. ; 1.7 1.7.1 Procedures graphiques La procédure gplot C’est la procedure graphique haute résolution arguments arguments de la procédure gplot donnée data=lib.objet librairie de sortie gout=catalogue.nom Instructions instructions de la procedure gplot titre title 'titre du graphique'; trace y(x) plot var_y1*var_x1='label1' [idem] label1 caractère : affichage de ce caractère variable qualitative, label des points entier : tracé du trait avec le symbol symbol.label1 ; options de plot / annotation anno=lib.table superposition OVERLAY trait vertical VREF=abscisse trait horizontal HREF=ordonnee axe des x HAXIS= borne_inf TO borne_sup BY pas axe des x HAXIS= val_1 val_2 val_3 val_4 axe des x HAXIS= axis axe des y VAXIS=... nom du graphique description='essai' couleur du fond cframe=SALMON couleur des axes caxis=BLUE couleur du texte ctext=GREEN Exemple proc gplot data=librairie.table; title 'titre'; plot var_y*var_x /options; run; http://www.math.unicaen.fr/~kauffman/cours 6 [email protected] Université de CAEN 1.7.2 28 septembre 2011 M2-IAD MI5 Statistiques Table d’annotation Les données de cette table permettent de tracer d’autres graphiques dans le graphique en cours. En particulier dessiner des annotations en certains points. Chaque ligne de cette table correspond à une instruction graphique dont le nom est la valeur de la variablefunction. Tous les autres éléments de la même ligne sont des arguments de cette fonction graphique. function xsys ysys x y text color size table d’annotation nom de la fonction move déplacement sans trait au point (x,y) draw tracé du point précédent au point courant (x,y) label affichage de text en (x,y) symbol affichage de text en (x,y) échelle des x 2 échelle absolue échelle des y 2 échelle absolue position courante en x position courante en y chaine à afficher en (x, y) couleur d’affichage texte ou trait taille du texte affiché Exemple data table.anno; set table.cercle; retain xsys ysys '2'; informat function \$6.; function='move';x=0;y=0;output; function='draw';x=u1;y=u2;color='red';line=3;output; function='label';x=u1;y=u2;text=_NAME_;color='red';output; function='symbol';x=u1;y=u2;text='CIRCLE';size=5;color='red';output; keep function xsys ysys x y text color line size; run; On travaille avec les échelles du graphiques xsys=2 ysys=2. xsys=2 On utilise l’échelle du graphique en x. ysys=2 On utilise l’échelle du graphique en y. move On se déplace en x=0,y=0 sans tracer de trait draw on trace un trait jusqu’au point (x,y) de couleur rouge, avec le type de trait 3 label on trace le texte contenu dans texte au point (x, y) avec la couleur rouge. symbol on trace le symblo contenu dans texteau point (x, y) avec la couleur rouge d’une taille 5. 1.7.3 Définition des types de traits symbolx Syntaxe la définition des types de traits type de trait x symbolx options; type de trait x defaut symbolx; couleur de la ligne ci=BLUE epaisseur du trait width=1 type de trait pointille line=(1 continu,>=2 pointille) couleur des symboles color=RED symbole utilise v=SQUARE taille des symboles height=1 tracé entre les points interpol=(NONE,JOIN) Exemple symbol1 width=1 v=SQUARE http://www.math.unicaen.fr/~kauffman/cours 7 [email protected] Université de CAEN 28 septembre 2011 M2-IAD MI5 Statistiques interpol=join color=RED ci=BLUE; PROC GPLOT DATA=SINUS; PLOT Y*X=1; /* symbol1 utilisé */ TITLE 'Graphe de y=xsin(2x);test symbol'; run; 1.7.4 Trace des axes Syntaxe la définition des types de traits axe numero i axisi options ; epaisseur du trait width=1 marque sur les axes order= xinf to xsup by pas marque sur les axes order= (x1 x2 x3 x4 x5) trace de la legende label=(angle=90 rotate=0) tirets intermédiares minor=none couleur de l’axe color=RED Exemple axis1 width=5 order=(0 2.5 5) label=(angle=0 rotate=0) minor=none color=PURPLE; PROC GPLOT DATA=SINUS; PLOT Y*X/haxis=axis1; TITLE 'Graphe de y=xsin(2x);test axis'; run; PROC GPLOT DATA=SINUS; PLOT Y*X/ haxis=0 to 5 by 2.5 vaxis=-4 to 4 by 2; TITLE 'Graphe de y=xsin(2x);test axis'; run; 1.7.5 Parametres graphiques globaux Syntaxe http://www.math.unicaen.fr/~kauffman/cours 8 [email protected] Université de CAEN 28 septembre 2011 M2-IAD MI5 Statistiques goptions : parametres graphiques globaux syntaxe goptions options; reset goptions reset=all; reinitialisation d’un parametre goptions ftext=; affichage des parametres proc goptions;run; fonte des caractères ftext=SWISS couleur des caractères ctext=RED hauteur de la fonte htext=1 device device=(win,gif,png,pslepsfc,html) affichage des devices proc gdevice c=sashelp.devices; device en cours proc goptions option=device;run; nom fichiers ou repertoire out gsfname=fileref sortie ecran display pas de sortie ecran nodisplay fond image gif transparent transparency bord image (border,noborder) ods :repertoire racine html path="repertoire" ods :fichier racine html body=fileref 1.8 1.8.1 Création de documents, images, Ods Sortie print vers html /* attention bug dans SAS v8.2 SAS.CFG 1- option -PATH mettre la ligne sasaext0/dbcs/sasexe apres /sasroot/core/sasexe 2- options -NODBCS au lieu de DBCS */ data test; x=1;y="bonjour ";output; x=2;y="au revoir";output; run; /* sortie print -> html */ ods html body="i:\users\francois\tex\stat\SAS\html\test.htm"; proc print data=test;run; ods html close; 1.8.2 Sortie gplot vers gif,png,ps Dans les fichiers postscript il faut supprimer les lignes filename gifname 'I:\users\francois\tex\stat\sas\tmp\4-jan-2003\essai.gif'; filename pngname 'I:\users\francois\tex\stat\sas\tmp\4-jan-2003\essai.png'; filename psname 'I:\users\francois\tex\stat\sas\tmp\4-jan-2003\essai.ps'; goptions device=gif gsfname=gifname; proc gplot data=sinus; plot y*x/description="Essai Gif"; run; goptions device=png gsfname=pngname; proc gplot data=sinus; plot y*x/description="Essai png"; run; http://www.math.unicaen.fr/~kauffman/cours 9 [email protected] Université de CAEN 28 septembre 2011 M2-IAD MI5 Statistiques goptions device=pslepsfc gsfname=psname; proc gplot data=sinus; plot y*x/description="Essai ps"; run; quit; /*ferme les fichiers*/ 1.8.3 Sorties multiples /* greplay */ filename psdir "I:\users\Francois\TEX\stat\SAS\ps"; goptions hsize=20cm vsize=20cm gsfname=psdir device=pslepsfc; proc greplay igout=graphique nofs; replay _ALL_; run; quit; /* goptions reset */ goptions reset=all; http://www.math.unicaen.fr/~kauffman/cours 10 [email protected] Université de CAEN 1.9 28 septembre 2011 M2-IAD MI5 Statistiques Exemples Vérifier la bonne exécution, utiliser l’explorateur pour voir la nouvelle table dans la librairie WORK. /* Création d'une table nommée class dans la librairie temporaire WORK les variables name sex sont du type chaine de caractères caractère dollar suivant le nom. On peut utiliser datalines aulieu de cards boucle impicite sur la lecture des variables name...weight après avoir lu les variables on passe à la ligne dans le flot d'entree */ data classe; input name $ sex $ age height weight; put name weight; /* affichage log */ cards; JOHN M 12 59.0 99.5 JAMES M 12 57.3 83.0 ALFRED M 14 69.0 112.5 WILLIAM M 15 66.5 112.0 JEFFREY M 13 62.5 84.0 run; /* affichage des types des variables */ proc contents data=classe; /* affichage des données */ proc print data=classe; title 'Création d run; 1.9.1 une table'; Inclusion de programme sas /* Ordre d'inclusion et procedure MEAN */ %include 'i:\users\francois\tex\stat\SAS\exemples\data-input.sas'; Proc means data=CLASS; title 'Inclusion de programme'; run; 1.9.2 Création d’une table à partir d’un fichier Le séparateur de champs est un espace. /* création d'une table à partir d'un fichier name $ : name est une variable de type chaine de caractères */ filename indata 'i:\users\francois\tex\stat\SAS\data.raw'; data class; infile indata; input name $ sex $ age height weight; run; proc print; title 'Création dune table à partie d un fichier'; run; 1.9.3 Premère sortie graphique /* graphique alphanumérique et windows */ libname save 'i:\users\francois\tex\stat\SAS\tmp'; http://www.math.unicaen.fr/~kauffman/cours 11 [email protected] Université de CAEN 28 septembre 2011 M2-IAD MI5 Statistiques data males; set save.class; /* copie de la table class */ if sex='M'; /* selection de sex*'M' */ /* utilisation de la table par defaut */ proc print; title 'dernière table utilisée males'; run; proc plot; plot weight*height=sex; title 'plot alphanumérique table pardefaut'; run; proc gplot; plot weight*height=sex; title 'plot table pardefaut'; run; 1.9.4 Procédure graphique /* PROCEDURE GRAPHIQUE CHART DIAGRAMME EN BATON PLOT DIAGRAME (X,Y) */ libname save 'c:\users\francois\tex\stat\SAS\tmp'; PROC CHART DATA=SAVE.CLASS; VBAR HEIGHT; title 'Histogramme en baton vertical de la variable Height'; run; PROC CHART DATA=SAVE.CLASS; HBAR HEIGHT; title 'Histogramme en baton horizontal de la variable Height'; run; PROC PLOT DATA=SAVE.CLASS; plot WEIGHT*HEIGHT $ sex; title 'height->weight en fonction du sexe'; run; PROC GCHART DATA=SAVE.CLASS; VBAR HEIGHT; title 'Histogramme en baton vertical de la variable Height'; run; PROC GCHART DATA=SAVE.CLASS; HBAR HEIGHT; title 'Histogramme en baton horizontal de la variable Height'; RUN; PROC GPLOT DATA=SAVE.CLASS; plot WEIGHT*HEIGHT= sex; title 'weight~height en fonction du sexe'; run; 1.9.5 Procédure graphique : superposition /* \begin{verbatim} http://www.math.unicaen.fr/~kauffman/cours 12 [email protected] Université de CAEN 28 septembre 2011 M2-IAD MI5 Statistiques Graphiques */ DATA NOTES; INPUT NOM$ NOTE1 NOTE2 NOTE3 SEXE$; LABEL NOTE1='statistique' NOTE2='A.données' NOTE3='Mémoire'; CARDS; HELENE 18.5 9 17 F SOPHIE 13.5 12 16 F CATHY 11 8 17 F MATHIEU 10.5 13 14 M EVA 19 18 19 F LAURE 17.5 18.5 19 F OSCAR 11.5 12 16 M OMAR 14.5 15.5 16 M MARC 10 4 14 M PASCAL 9 8.5 16 M GUILLETTE 14.5 13.5 14 F ANDRE 9 6.5 15 M MICHEL 8.5 11 14 M ; RUN; TITLE 'Sorties graphiques alphanumérique par défaut'; PROC PLOT DATA=NOTES; PLOT NOTE1*NOTE2; RUN; TITLE 'Sorties graphiques'; PROC GPLOT DATA=NOTES; PLOT NOTE1*NOTE2; RUN; /* LIGNE DE REFERENCE y=12 x=12 */ TITLE 'TRACE DE DEUX TRAITS'; PROC GPLOT DATA=NOTES; PLOT NOTE1*NOTE2=SEXE / HREF=12 VREF=12; TITLE 'Sorties par défaut'; RUN; TITLE 'SUPERPOSITION ALPHANUMERIQUE'; PROC PLOT DATA=NOTES; PLOT NOTE1*NOTE2='A' NOTE3*NOTE1='M' /OVERLAY VAXIS=0 TO 20 BY 5 HAXIS=0 TO 20 BY 5; TITLE 'Supperposition'; RUN; TITLE 'SUPERPOSITION GRAPHIQUE'; PROC GPLOT DATA=NOTES; PLOT NOTE1*NOTE2='A' NOTE3*NOTE1='M' /OVERLAY VAXIS=0 TO 20 BY 5 HAXIS=0 TO 20 BY 5; TITLE 'Supperposition'; RUN; /* \end{verbatim} */ 1.9.6 Utilisation d’une table dans une librairie /* http://www.math.unicaen.fr/~kauffman/cours 13 [email protected] Université de CAEN 28 septembre 2011 M2-IAD MI5 Statistiques TABLE SAUVEGARDEE DANS UNE LIBRAIRIE */ libname save 'i:\users\francois\tex\stat\SAS\tmp'; proc print data=save.class; title 'Table sauvegardée dans la librairie save'; run; proc means data=save.class; var height weight; title 'Moyenne de la table save.class'; run; 1.9.7 Utilisation de procédures /* PROCEDURE SORT MEANS */ libname save 'i:\users\francois\tex\stat\SAS\tmp'; PROC SORT DATA=save.CLASS; BY SEX; /* TRI SEX PUIS AGE */ PROC SORT DATA=SAVE.CLASS; BY SEX AGE; RUN; PROC PRINT; title 'TRI sexe puis age'; RUN; PROC MEANS DATA=save.CLASS; VAR HEIGHT WEIGHT; BY SEX; /* moyenne par sexe */ LABEL SEX='Sexe'; RUN; /* on utilise la table par défaut*/ PROC GCHART; VBAR HEIGHT; title 'Histograme de height'; run; proc print; title 'Table par défaut'; run; 1.9.8 Data : lecture formattée /* lecture entre le x_eme et le y-eme caractere d'une ligne format= vide ou $, colonnes=1-20 */ data thisone; input name $ 1-20 age city $ 29-37 state $ ; datalines; Oliver Schabenberger 33 Lansing MI John T. Smith 37 New York NY run; proc print data=thisone; title 'Lecture avec des champs spécifiés'; http://www.math.unicaen.fr/~kauffman/cours 14 [email protected] Université de CAEN 28 septembre 2011 M2-IAD MI5 Statistiques run; data erreur; input name $ 1-15 age city $ 29-37 state $ ; datalines; Oliver Schabenberger 33 Lansing MI John T. Smith 37 New York NY run; proc print data=erreur; title 'Une erreur dans la largeur des champs'; run; /* tant que la ligne n'est pas finie on lit (x,y,z), et on sort (x,y,z) */ data XYZ; input x y z @@; datalines; 1 1 12.4 1 2 11.3 1 3 1.4 2 1 2.1 2 2 19.4 1 3 10.0 run; proc print data=XYZ; title 'replication sur la meme ligne'; run; /* replication sur la meme ligne Obs x y z 1 2 3 4 5 6 1 1 1 2 2 1 1 2 3 1 2 3 12.4 11.3 1.4 2.1 19.4 10.0 4 */ 1.9.9 Data : infile data essai; infile cards delimiter=',' /* , séparateur de champs */ firstobs=3 /* la troisième ligne est la première lue*/ obs=5; /* dernière ligne lue est la 5 -ième */ input prenom$ age; cards; francois,42 etienne,10 agnes,12 etienne,11 paul,16 samuel,1 dominique,41 proc print data=essai; title 'lecture d un fichier avec infile'; run; /* lecture d un fichier avec infile 19 15:11 Monday, January 5, 2004 Obs prenom age 1 agnes 12 2 etienne 11 3 paul 16 */ http://www.math.unicaen.fr/~kauffman/cours 15 [email protected] Université de CAEN 1.9.10 28 septembre 2011 Data : concaténation de lignes data growth1; input block trait pousse @@; /* lecture 3 par 3*/ year = 1997; datalines; 1 1 7.84 2 1 8.69 3 1 8.11 4 1 7.74 5 1 8.35 data growth2; input block trait pousse @@; datalines; 1 4 6.64 2 4 6.57 3 4 6.78 4 4 6.54 5 4 6.48 data growth; set growth1 growth2; run; proc print data=growth; title 'réunion des deux tables'; run; /* réunion des deux tables 15:11 Monday, January 5, 2004 Obs block trait pousse year 1 1 1 7.84 1997 6 1 4 6.64 . */ 1.9.11 M2-IAD MI5 Statistiques 22 Data : concaténation de colonnes DATA table1; /* la table 1 ne contient qu'une seule ligne */ INPUT id1 $ x1 ; cards; table1 1.1 DATA table2; /* la table 2 ne contient que deux lignes */ INPUT id2 $ x2 x3; cards; table2 1.2 1.3 table2 2.2 2.3 data table;merge table1 table2; proc print data=table; title 'Concaténation de colonnes corrrespondance ligne à ligne'; run; /* Obs id1 x1 id2 x2 x3 1 table1 1.1 table2 1.2 1.3 2 . table2 2.2 2.3 */ 1.9.12 Data : Concaténation de colonnes, correspondance de lignes filename spacing1 'i:\users\francois\tex\stat\data\spacing1.txt'; filename spacing2 'i:\users\francois\tex\stat\data\spacing2.txt'; data spacing1; infile spacing1;input P space rep yield96; data spacing2; infile spacing2;input P space rep yield97; data spacing; /* concatenation des colonnes */ merge spacing1 spacing2; by p space rep; /* correpondance des lignes p et space et rep */ proc print data=spacing; title 'Concaténation de colonnes correpondance: p space rep'; run; /* Concaténation de colonnes correpondance: p space rep Obs P space rep yield96 yield97 1 0 40 1 57 35 2 0 40 2 58 28 3 0 40 3 . 29 4 0 80 1 57 38 http://www.math.unicaen.fr/~kauffman/cours 16 [email protected] Université de CAEN 17 18 1.9.13 25 25 28 septembre 2011 120 120 2 3 57 53 M2-IAD MI5 Statistiques . 28 */ Data :Concaténation de colonnes de longueur différentes data rabbits; infile 'i:\users\francois\tex\stat\data\data-merge-lapin.txt'; input Treat Rabbit day0 day4; data means; input treat day0mn day4mn; datalines; 1 -0.24 1.38 2 -0.58 -0.52 3 -0.68 0.24 data ecart; merge rabbits means; by treat; dev0 = day0 - day0mn; dev4 = day4 - day4mn; proc print data=ecart; title 'Fusion de colonnes'; run; /*Obs Treat Rabbit day0 day4 day0mn day4mn dev0 dev4 1 1 1 -0.3 -0.2 -0.24 1.38 -0.06 -1.58 2 1 2 -0.5 2.2 -0.24 1.38 -0.26 0.82 3 1 3 -1.1 2.4 -0.24 1.38 -0.86 1.02*/ 1.9.14 Data :Labels data test; label x1 = 'Numero individu' x2 = 'variable tres interessante' x3 = 'encore plus'; input x1 x2 x3; datalines; 1 1 1 1 1 2 proc print data=test label;title 'Utilisation des labels'; run; /* variable Numero tres encore Obs individu interessante plus 1 1 1 1 2 1 1 2 3 1 1 3 */ 1.9.15 Data :Format et date /* lecture de dates françaises */ options dflang="french"; data time; input date eurdfde7.; /* largeur de champs fixe 7 caractères*/ sasdate = date; /* meme nombre mais affichage flottant*/ format date mmddyy8.; cards; 01JAN60 12SEP97 16MAY98 16MAI98 proc print; http://www.math.unicaen.fr/~kauffman/cours 17 [email protected] Université de CAEN 28 septembre 2011 M2-IAD MI5 Statistiques title 'Pb et manipulation de dates'; run; /* Pb et manipulation de dates 18 15:11 Monday, January 5, 2004 Obs date sasdate 1 01/01/60 0 2 09/12/97 13769 3 . . 4 05/16/98 14015 */ 1.9.16 Data :Rename /* \begin{verbatim} PROCEDURE DATA rename: ancienne_variable=nouvelle_variable */ libname save 'c:\users\francois\tex\longitudinal\SAS'; data rename1; set save.class(rename=(name=nom height=hauteur weight=poids)); run; proc print data=rename1; title 'renommage de variables lors de la copie'; run; data rename2; set save.class; rename name=nom height=hauteur weight=poids; run; proc print data=rename2; title 'renommage de variables après la copie'; run; /* \end{verbatim} */ 1.9.17 Calcul data essai; input x @@; retain moins 0 plus 0 nul 0 total 0; if (x<0) then moins=moins+1; if (x>0) then plus=plus+1; if (x=0) then nul=nul+1; total=total+x; keep x moins plus nul; cards; 1 -1 2 3 4 5 6 -2 -3 title 'Initialisation de variables'; proc print;run; /* Initialisation de variables 12:29 Tuesday, January 6, 2004 Obs x moins plus nul 1 1 0 1 0 2 -1 1 1 0 3 2 1 2 0 4 3 1 3 0 5 4 1 4 0 6 5 1 5 0 */ http://www.math.unicaen.fr/~kauffman/cours 18 [email protected] Université de CAEN 1.9.18 28 septembre 2011 M2-IAD MI5 Statistiques Initialisation /* \begin{verbatim} DATA : INITIALISATION DE VARIABLES, BOUCLES FREQ : table de fréquences */ data spike; input rating $ tx; datalines; apparent 1 discrete 2 apparent 3 discrete 4 discrete 5 discrete 6 none 7 discrete 8 discrete 9 apparent 10 ;; run; proc sort data=spike; by rating; run; data countem; set spike; /* copie de la table spike */ by rating; /* tri par la variable rating */ retain cnt 0 howmany 0; /* initialisation cnt=howmany=0 */ if first.rating then cnt=1; else cnt+1; if first.rating then howmany+1; if last.rating then output; run; proc freq data=spike; tables rating; title 'Tableau des fréquences de la variable qualitative rating'; run; proc print data=countem; title 'Comptage dans une instruction data'; run; /* \end{verbatim} */ 1.9.19 Génrarateur aléatoire /* x \in N(12,sqrt(3)) 8923 germe de la fonction */ data Gauss; do i = 1 to 100; z = rannor(8923); p = 1 - Probnorm(z); x = z*Sqrt(3) + 12; output; end; run; http://www.math.unicaen.fr/~kauffman/cours 19 [email protected] Université de CAEN 1.9.20 28 septembre 2011 M2-IAD MI5 Statistiques Boucle DO tabulation data sinus; DO X=0 TO 5 BY 0.05; Y=X*sin(2*X); OUTPUT; END; PROC GPLOT DATA=SINUS; PLOT Y*X='.'; TITLE 'Graphe de y=xsin(2x)'; run; 1.9.21 SELECT data essai; set class; select ; when ( weight < 60 ) type = 'leger'; when ( weight < 90 ) type = 'moyen'; otherwise type = 'lourd'; end; title 'Exemple d utilisation de select'; data essai1; input dep @@; length region $20.; select(dep); when ( 50,14,61 )region='basse normandie'; when ( 76,27 ) region='haute normandie'; otherwise region='hors-sein'; end; cards; 50 14 61 76 27 title 'Cas multiples'; proc print;run; /* Cas multiples 12:29 Tuesday, January 6, 2004 Obs dep region 1 50 basse normandie 2 14 basse normandie 3 61 basse normandie 4 76 haute normandie 5 27 haute normandie */ 1.9.22 63 Tableau /* remplacer les 9999 par . */ DATA essai; input id sex $ age inc x y z @@; array vars{3} x y z; do i=1 to 3; if vars{i} = 9999 then vars{i} = .; end; cards; 1 F 35 17 7 2 2 17 M 50 14 5 5 3 33 F 49 M 24 14 7 5 7 65 F 52 9 4 7 7 81 M 2 F 34 17 6 5 3 18 M 40 14 9999 5 2 34 50 M 35 17 5 7 9999 ; title 'Tableau monodimensionel'; proc print;run; /* Obs id sex age inc x y http://www.math.unicaen.fr/~kauffman/cours 45 6 44 11 F 47 z 20 7 2 7 7 7 7 6 6 9999 6 i [email protected] Université de CAEN 8 9 10 1.9.23 18 34 50 28 septembre 2011 M F M 40 47 35 14 6 17 . 6 5 5 . 7 2 6 . 4 4 4*/ OUTPUT /* lag valeur précédente */ data essai; input n @@; delta1=lag(n); delta2=lag2(n); delta3=lag3(n); cards; 1 2 3 4 5 6 title 'Fonction retard: lag'; proc print;run; /* Fonction retard: lag 12:29 Tuesday, January 6, 2004 Obs n delta1 delta2 1 2 3 4 5 6 1 2 3 4 5 6 . 1 2 3 4 5 . . 1 2 3 4 1.9.24 M2-IAD MI5 Statistiques 95 delta3 . . . 1 2 3 */ Création de plusietrs tables filename fichier 'i:\users\francois\tex\stat\data\annee.txt'; data y1998 y1999 y2000 yXXXX; infile fichier; input year X; select (year); when (1998) output y1998; when (1999) output y1999; when (2000) output y2000; otherwise output yXXXX; end; run; data total; infile fichier; input year X; run; proc print data=y1998;run; 1.9.25 Conversion /* conversion numerique alpha numérique */ data test; input s $ ; x = 0; /* x est numérique */ x = s; /* conversion alphanumérique vers numérique */ datalines; 1 http://www.math.unicaen.fr/~kauffman/cours 21 [email protected] Université de CAEN 28 septembre 2011 M2-IAD MI5 Statistiques 2 3 4 5 ;; run; 1.9.26 Fonction /* exemple de fonction: les arguments sont en entrée de la base de donnée */ data DATASET; input MEAN STDDEV; DO I=1 TO 50; X=MEAN+STDDEV*normal(0); output; end; drop MEAN STDDEV; cards; 40 15 ;;; RUN; 1.9.27 Diagramme en baton /* \begin{verbatim} diagramme en baton */ title; footnote; goptions ftext=SWISS ctext=BLACK htext=1.0 cells; goptions colors=(red green blue cyan purple tan pink orange brown yellow plum peru salmon lime); axis1 label=(a=90 r=0); proc gchart data=Save.Class ; hbar HEIGHT / description="Horizontal Bar Chart of HEIGHT" frame woutline=1 maxis=axis1 type=FREQ coutline=BLACK patternid=midpoint cframe=CXF7E1C2 nostats ; hbar WEIGHT / description="Horizontal Bar Chart of WEIGHT" frame woutline=1 maxis=axis1 type=FREQ coutline=BLACK patternid=midpoint cframe=CXF7E1C2 nostats ; http://www.math.unicaen.fr/~kauffman/cours 22 [email protected] Université de CAEN 28 septembre 2011 M2-IAD MI5 Statistiques run; goptions ftext= ctext= htext= ; quit; /* \end{verbatim} */ 1.9.28 Graphique 3d Copiez la table houses dupus SASUSER.CORE dans votre librairie personnelle /* \begin{verbatim} TRACE 3D */ title "essai 3D"; footnote "Francois"; /* Interpollation */ proc g3grid data=Save.Houses out=WORK.PNTS; grid SQFEET * BATHS = PRICE; run; *** Surface Plots ***; goptions ftext=SWISS ctext=BLACK htext=1 cells; proc g3d data=WORK.PNTS ; plot BATHS * SQFEET = PRICE / name = 'SURF' caxis = BLACK ctext = WHITE cbottom = GREEN ctop = BLUE tilt = 70 rotate = 70 xticknum = 4 yticknum = 4 zticknum = 4 ; run; goptions ftext= ctext= htext=; axis1; proc delete data=WORK.PNTS; run; /* \end{verbatim} */ 1.9.29 Axis,goptions libname save 'c:\users\francois\tex\longitudinal\SAS'; /* les options globales */ goptions ftext=SWISS /* fonte du texte */ ctext=BLACK /* couleur des textes */ htext=1 /* taille de la fonte */ ; /* les options de GPLOT ou GCONTOUR */ symbol1 /* ci=BLUE /* c=BLUE /* v=SQUARE /* height=1 /* cells /* interpol=JOIN l=1 /* w=1 /* 1 numerotation des symboles */ couleur de la ligne */ couleur des symboles */ Symbole dessiné */ taille des symboles */ un type d'unité ou CM*/ /*interpollation*/ type de ligne de 2 à 46 pointillés */ épaisseur */ http://www.math.unicaen.fr/~kauffman/cours 23 [email protected] Université de CAEN 28 septembre 2011 M2-IAD MI5 Statistiques ; /* les options pour les axes */ axis1 /* numérotation */ width=1 /*largeur*/ label=(a=90 r=0) /*a angle pour la ligne d'écriture r rotationpour chaque caractère */ ; axis2 width=1; title "ESSAI GRAPHIQUE"; /* titre */ footnote "ZZZZZ"; /* note en pied de page */ proc gplot data=save.class; plot height*age / description = " Essai coloré" cframe = SALMON caxis = RED ctext = GREEN vaxis = axis1 haxis = axis2 ; run; symbol1; /* remet les valeurs par défaut */ /* pour voir l'environnement graphique */ proc goptions;run; /* reinitialisation */ goptions reset=all; goptions ftext= ctext= htext=; /* refaire ces graphiques en validant les sorties html TOOLS->OPTIONS->PREFERENCES:RESULTS-> CREATE HTML+FOLDER */ 1.9.30 Sortie gif html /* Sortie graphique */ libname save 'c:\users\francois\tex\longitudinal\SAS'; /* filtre ods Output Delivery System */ ods html body="c:\users\francois\tex\stat\SAS\html\essai_tables.htm"; proc print data=sinus;run; ods html close; /* SAS/GRAPH SYSTEM OPTIONS */ http://www.math.unicaen.fr/~kauffman/cours 24 [email protected] Université de CAEN 28 septembre 2011 M2-IAD MI5 Statistiques filename output 'c:\users\francois\tex\longitudinal\SAS\TMP\'; /* drivers gif,png,html */ /* goptions device=gif gsfname=output transparency noborder; goptions device=png gsfname=output transparency noborder; goptions device=psl gsfname=output transparency noborder; */ goptions device=html /* driver html */ gsfname=output /* répertoire de sortie fichiers graphiques*/ transparency /* image gif en mode transparent */ noborder /* pas de bordure */ ; title "Integration dans un fichier html"; ods html path=output /* repertoire de sortie fichiers html */ body="essai.htm" /* nom du fichier racine */ ; proc gplot data=save.class; plot height*age; run; ods html close; run; /* générations des fichiers images uniquement */ filename grafout "c:\TEMP\"; goptions device=gif gsfname=grafout gsfmode=replace; proc gtestit; run; filename grafout 'C:\TEMP\'; goptions device=win; /* sortie écran uniquement */ proc gtestit; /* affichage du device */ proc goptions OPTION=DEVICE;run; goptions device=win; /* retour */ proc goptions OPTION=DEVICE;run; 1.9.31 Sortie mode transparent /* sortie graphique */ : html mode slide goptions device=webframe nodisplay /* pas de sortie */ ; title 'This is graph one'; proc gslide gout=work.gseg /*catalogue de sortie */ ; run; quit; http://www.math.unicaen.fr/~kauffman/cours 25 [email protected] Université de CAEN 28 septembre 2011 M2-IAD MI5 Statistiques title 'This is graph two'; proc gslide gout=work.gseg /*catalogue de sortie */ ; run; quit; filename grafout 'c:\users\francois\tex\longitudinal\SAS\TMP\'; goptions device=webframe display gsfname=grafout ; proc greplay igout=work.gseg nofs ; replay _all_; DELETE _all_; run; 1.9.32 /* sortie graphique */ /* nom du répertoire de sortie */ /* catalogue d'entrée */ /* mode ligne?*/ /* revoir toutes les figures */ /* effacer toutes les figures du catalogue */ Sortie Postscript /* sortie graphique ps */ filename fichier "i:\users\francois\tex\stat\SAS\html\sinus.ps"; goptions device=pslepsfc gsfname=fichier; proc gplot data=sinus; plot y*x/ description="Essai Ps"; run; quit; 1.9.33 Procédure INSIGHT /* analyse graphique d'un tableau */ PROC INSIGHT TOOLS; RUN; Sélection de modèles 26 [email protected] Index Logiciel SAS, 1 Création d’une table, 2 Création de documents, images, Ods, 7 Sortie gplot vers gif,png,ps, 7 Sortie print vers html, 7 Sorties multiples, 8 Documentation, 2 Environnement de programmation, 2 Exemples, 9 Axis,goptions, 21 Boucle DO tabulation, 18 Calcul, 16 Conversion, 19 Création d’une table à partir d’un fichier, 9 Création de plusietrs tables, 19 Data : concaténation de colonnes, 14 Data : Concaténation de colonnes, correspondance de lignes, 14 Data : concaténation de lignes, 14 Data : infile, 13 Data : lecture formattée, 12 Data :Concaténation de colonnes de longueur différentes, 15 Data :Format et date, 15 Data :Labels, 15 Data :Rename, 16 Diagramme en baton, 20 Fonction, 20 Graphique 3d, 21 Génrarateur aléatoire, 17 Inclusion de programme sas, 9 Initialisation, 17 OUTPUT, 19 Premère sortie graphique, 9 Procédure graphique, 10 Procédure graphique : superposition, 10 Procédure INSIGHT, 24 SELECT, 18 Sortie gif html, 22 Sortie mode transparent, 23 Sortie Postscript, 24 Tableau, 18 Utilisation d’une table dans une librairie, 11 Utilisation de procédures, 12 Format, 4 Instructions, 2 Procedures graphiques, 4 Définition des types de traits symbolx, 5 La procédure gplot, 4 Parametres graphiques globaux, 6 Table d’annotation, 5 Trace des axes, 6 27 Procédure print, 3