/* Ecriture d'une table SAS: le début */
data toto ;
input id $ nom $ dept $ type $ ;
datalines;
000 Michel 12 Document
111 Fredo 45 Survey
222 Diana 45 Document
888 Monique 12 Document
999 Eric 03 Survey
;
run ;
proc print data=toto;
title 'Ma premiere table SAS';
run;
/* Lecture d'un fichier ascii délimité par '@' */
data eric;
infile 'c:\sas\eric\toto' dlm='@';
input v1-v3;
run;
/* Lecture d'un fichier délimité par ',' avec cards */
data eric;
*informat v1 & $13. (v2-v3) 1. sexe $1.;
/* le '&' permet les blancs dans v1 */
infile cards dlm=',';
input v1-v3 sexe $;
cards;
John steed,2,3,M
Eric Cahuzac,5,6,F
Nathalie baye,8,9,M
;
run;
/* Lecture d'un fichier délimité par un blanc à partir d'un autre fichier */
filename toto 'c:\sas\eric\toto.txt';
data eric;
infile toto;
*infile 'toto' obs=100;
/variante pour lire les 100 premieres lignes */
*infile 'toto' firstobs=100; /variante pour lire à
partir de la ligne 100 */
input @1 v1 $14.
@1 v2 $2.
@16 v3 3.;
run;
/* Lecture d'un fichier à partir de 2 sources identiques*/
filename toto 'c:\sas\eric\toto.txt';
filename titi 'c:\sas\eric\titi.txt';
data eric;
if not finfich1 then infile 'toto' end=finfich1;
else infile 'titi';
input v1-v3;
run;
/* Ecriture d'un fichier délimité par '/' à partir d'un autre fichier */
libname eric 'c:\sas\eric';
filename titi 'c:\sas\eric\titi.txt';
data _null_;
set eric.toto;
file titi;
put (v1-v3) (3. '/');
run;
/* Ecriture d'un fichier pour faire des formats SAS à partir d'un autre fichier */
filename out 'c:\sas\eric\titi.txt';
data _null_;
set lab73;
file out;
put ' " ' com ' "=" ' labcom ' " ';
run;
/* Sélectionner une observation sur 3 dans un fichier */
data eric;
set toto;
id+1;
/* pas obligatoire, c'est un compteur pour vérif */
if mod(_N_,3)=1;
run;
/* Calcul de totaux colonne */
proc sort data=titi;
by id;
run;
data toto;
set titi;
by id;
retain somme 0;
if id ne LAG(id) then somme=prix;
else somme=somme+prix; /* calcule
le prix cumulé par ID */
if last.id; /* garde le prix total par ID */
run;
Exemple:
ID prix somme
1 1 1
1 2 3
1 3 5
2 4 4
2 5 9
2 6 15
3 7 7
3 8 15
data &infile;
set &infile;
ident+1;
/* seule cette ligne est utile! */
label ident='Identifiant individuel';
run;
/* Transforme une table sas:sd2 au format de TRANSPORT */
libname entree 'd:\sas\eric' ;
/* chemin où est prise la table sas */
filename sortie 'c:\temp\transp'; /* non
du fichier de transport */
proc cport data=entree.rectorat file=sortie V608;
run;
/* Transforme un fichier TRANSPORT au format table sas:sd2 */
libname sortie 'c:\temp' ;
/* chemin où doit être mis le sd2 */
filename entree 'c:\temp\transp' ;
/* nom du fich transport */
proc cimport infile=entree data=sortie.recto;
run;
/* Crée une variable en utilisant un FORMAT */
data eric;
set toto;
diplo=put(etdip,$dipl.); /* le format doit être
défini avant */
run;
/* Multiplier les observations d'une table à partir de plusieurs variables */
Soit la table suivante:
ID NOM
DEPT HAMB MARSEIL
000 Michel
12 4
.
111 Fredo
45 6
6
222 Diana
45 8
8
888 Monique
12 10
.
999 Eric
03 12
12
et vous voulez la table:
ID NOM
DEPT MARCHE PRIX
000 Michel
12 HAMB
4
111 Fredo
45 HAMB
6
111 Fredo
45 MARSEIL
6
222 Diana
45 HAMB
8
222 Diana
45 MARSEIL
8
888 Monique 12
HAMB 10
999 Eric
03 HAMB
12
999 Eric
03 MARSEIL
12
data titi;
set toto;
length marche $8;
array matpx(*) hamb marseil;
do i=1 to dim(matpx);
if (matpx(i) <>.) then do;
/* ne duplique que s'il y a un prix */
prix=matpx(i);
call vname(matpx(i),marche); /* récupère
le nom des variables*/
output;
end;
end;
drop hamb marseil i ;
run;
data news;
input sex age subs;
cards;
1 35 1 /* femme=1 et Homme=0
*/
0 44 1
0 45 0
etc.........
1 46 1
0 58 0
1 50 0
1 32 1
1 52 0
1 35 1
1 51 1
;
proc probit;
class subs sex;
model subs=sex age / d=logistic;
title 'Regression Logistique sur le status d'inscription';
run;
/* Un modèle Logit plus évolué */
proc catmod data=sup.logit ;
direct aq88 ;
model cl33r = cl10
sex
insspe88
fi
sitfam
empq1
diplo
aq88
/oneway noiter noprofile ;
format cl33r f33r. cl10 f10det. sex $fsexe.
pe fpe. fi
ffi. sitfam $fsitfam. diplo fdiplo. empq1 fprofcat.
insspe88 fspedbfa.
ac fac. atde $fatde. ;
weight pond1 ;
run;
quit;
/* Ecrire un fichier de format pour s'en servir plus tard */
libname library 'c:\sas\eric'; /* emplacement du fichier format.sct */
proc format library=library;
value $ fstatut
"11" ="indépendants"
"12" ="employeurs"
"13" ="aides familiaux"
"21" ="intérimaires"
"22" ="apprentis"
"23" ="CDD"
"24" ="autres salariés"
"30" ="stagiaires
et contrats aidés"
"41" ="salariés
Etat"
"42" ="appelé
au SN"
" " ="sans
objet";
run;
/* Proc Access pour transformation de fichiers DBF en SAS */
filename dbf 'c:\toto\fich.dbf';
libname sas 'c:\toto\';
%let infile=dbf;
proc access dbms=dbf; /*
le fichier lu est un DBF */
create sas.eric.access; /* création des
descripteurs du fichier */
path=&infile;
/* chemin du fichier lu */
assign=yes;
create sas.eric.view; /* création
d'une vue du fichier */
select all;
proc access viewdesc=sas.eric
out=sas.&infile ; /* enregistre le fichier au format SAS */
run;
quit;
/* Transformation d'une variable numérique en texte */
data eric;
set toto (rename=(marche=marchar));
length marche $ 4;
marche=marchar;
extrait=substr(marche,1,2);
drop marchar;
run;
/* Transformer un fichier SAS en XLS avec DBLOAD */
libname emploi 'c:\sas\eric';
%let infile=emploi.extrai95;
PROC DBLOAD dbms=XLS data=&infile (keep= aa act s ag);
*** Data-base Description Statement ***;
path='c:\sas\eric\eric';
*** Editing Statements ***;
putnames;
limit=10;
list aa act;
label;
reset all;
*where s='2';
*** Creating and Loading Statement ***;
load;
*quit;
run;
data &infile;
set &infile;
array xx[*] _NUMERIC_;
do i=1 to dim(xx);
if xx[i]=. then xx[i]=999;
end;
drop i;
run;