Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
A cura di
Laura Neri
Dip. Metodi Quantitativi
Università degli Studi di Siena
PARTE I
IL SISTEMA SAS
Sistema integrato di prodotti software
• data entry, manipolazione archivi;
• stesura di report e grafici;
• analisi statistiche e matematiche;
• previsioni e supporto alle decisioni;
• ricerca operativa e project management;
• sviluppo di applicazioni.
Data Step
Inizia con un data statement e consente di leggere/creare
e/o modificare archivi di dati
Proc Step
Inizia con un proc statement, ovvero richiama una
procedura SAS, la esegue su un SAS dataset e produce dei
risultati
e le finestre di programmazione
In the W i n d o w s o p e r a t i n g en v i r o n m e n t yo u d o th is
with
the ri g h t m o u s e b u t t o n . T h e n select eit h e r Print or
Save As fr o m the po p- u p m e n u .
y o u r m o u s e.
li k e in W i n d o w s :
When yo u fir s t o p e n th is w i n d o w , yo u w i l l see a
ty p e of o u t p u t , an d it is w h a t yo u see in the O u t p u t
en v i r o n m e n t . Yo u can tu r n on H T M L ou t p u t by
O n c e yo u ha v e tu r n e d on H T M L o u t p u t , the n eve r y
ti m e yo u run a pr o g r a m , yo u r ou t p u t will
au t o m a t i c a l l y ap p e a r in the Res u l t s V i e w e r w i n d o w .
SAS DATA SET
V1 V2 ... .... Vk
.
Before you can use a SAS data set, you have to tell SAS
where to find it. You do that by setting up a SAS library. A
SAS library is simply a location where SAS data sets are
stored. A SAS library might be a folder or directory on your
computer, or it might be a physical location like a hard
drive, or CD.
Per definire una libreria permanente si possono seguire due
strade:
fare click sul comando New Library della barra Menu
ed inserire le informazioni richieste, tra cui il nome
della library ed il percorso fisico associato alla libreria
stessa
Scrivere nella finestra Program Editor l‘istruzione
Dove:
mylibname è il nome attribuito alla libreria
path è il percorso fisico dove vengono memorizzati
i dati
Comments
There are two styles of comments you can use:
one starts with an asterisk (*) and ends with a
semicolon (;) the other style starts with a slash asterisk
(/*) and ends with an asterisk slash (*/).
Istruzione INFILE
Istruzione INPUT
Istruzione DATALINES (CARDS)
Istruzione INFILE
Indica al sistema dove leggere i dati
nomefile
nome, con eventuale percorso, del file ASCII da leggere o
parola chiave CARDS se i dati sono inseriti da programma
Istruzione INPUT
Definisce nome, tipo e modo di lettura delle variabili
I modi di lettura sono: a lista, a colonna, con formato
LETTURA A LISTA
Possibile quando i dati sono registrati in formato libero, con
almeno uno spazio bianco tra un campo ed il successivo
data pippo;
infile 'F:\written\didattica\CorsoSAS\input_lista.txt';
input codice genere eta;
run;
data pluto;
input x1-x3;
datalines;
1 5 7
9 3
2
6 9 8
13 5 8
;
run;
equivalentemente
input x1-x3;
cards;
1 5 7
9 3
2
6 9 8
13 5 8
;
run;
data pippo;
infile
'F:\written\didattica\CorsoSAS\input_colonna.txt';
input codice 1-3 genere $ 4 eta 5-6;
run;
data pippo;
infile
'F:\written\didattica\CorsoSAS\input_lista.txt';
input codice 3. @5 genere $1. @7 eta 2.;
run;
data uno;
input var1-var6;
cards;
18 66 24 23
19 45 34 20
40 97 56 43
23 56 73 90
;
run;
NOTA: SAS è passat o a una nuova riga quan d o l'ist r u z i o n e INPUT ha supe r a t o la
fine di una riga.
NOTA: Il data set WORK.UNO ha 2 osservazi o n i e 6 varia b i l i.
Data tre;
infile cards missover;
cards;
18 66 24 23
19 71 26 21
20 68 23 29
;
run;
proc print data=tre;
run;
Data tre;
input var1-var6;
infile cards;
cards;
18 66 24 23 12 1
19 71 26 21 3
20 68 23 29
1
;
run;
proc print data=tre;
run;
Data tre;
input var1-var6;
infile cards truncover;
cards;
18 66 24 23 12 1
19 71 26 21 3
20 68 23 29
1
;
run;
proc print data=tre;
run;
1 18 66 24 23 12 1
2 19 71 26 21 3 .
3 20 68 23 29 . .
4 1 . . . . .
File
Import Data/Export Data
….
PROC CONTENTS
PROC PRINT
PROC FSEDIT
PROC FORMAT
*PROGRAM_PROCEDURE_GENERALI.SAS;
PROC FSEDIT
DATA=SAS-data-set
….
proc fsedit data= consumo_toscana (obs=20);
var numcomp sesso1 eta1 bar;run;
PROC FORMAT: consente di creare una libreria di format
permanenti da utilizzare nei data step o nelle procedure
value-range-set(s);
******************PROC FORMAT*******************;
proc format;
value $genere 'm'='maschio'
'f'='femmina';
value si_no 1='sì'
2='no';
run;
data segreteria;
input id 1. @3 genere $1. @5 ricovero 1. @7
analisi 1.;
How do I read a SAS data file when I don't have its format
library?
If you try to use a SAS data file that has permanent formats but
you don't have the format library, you will get errors like this.
OPTIONS nofmterr;
at the top of your program, SAS will go ahead and process the file
despite the fact that it does not have the format library. You will
not be able to see the formatted values for your variables, but you
will be able to process your data file
LINGUAGGIO SAS: GESTIONE DEI DATA SET
LABEL
RENAME
KEEP
DROP
RETAIN
*PROGRAM_GESTIONE1;
DROP variabili ;
KEEP variabili ;
Variabile=Espressione
Tipo di operatori:
Aritmetici, di comparazione, logici, carattere
OPERATORI ARITMETICI
priorit simbol descrizione
à o
1 ** Elevamento a
potenza
2 * moltiplicazione
2 / Divisione
3 + addizione
3 - sottrazione
OPERATORI DI COMPARAZIONE
< LT Minore di
<= LE Minore o uguale di
> GT Maggiore di
>= GE Maggiore o uguale di
^= NE Non uguale
IN Uguale a uno degli
elementi della lista
OPERATORI LOGICI
Priorità
1 ^ NOT
2 & AND
3 | OR
Istruzione WHERE
Istruzione IF..THEN <ELSE>
WHERE espressione;
********PROC FORMAT********;
PROC FORMAT ;
value genere 1='maschio'
2='femmina';
value titstu 1='laurea o laurea breve'
2='diploma'
3='licenza media o qualifica'
4='lic. elementare,
analfabeta';
new-variable=PUT(old-variable,format)
data new;
set sashelp.class;
new=put(age,8.);
run;
new-variable=INPUT(old-variable,format)
data new2;
set new;
new2=input(new,best12.);
run;
Trucchetto:
A variabile carattere, X variabile numerica
Se si usa A con un operatore che richiede operandi
numerici Y=A+X, SAS converte A in numerica;
Se si usa un operatore di comparazione, Y=A<X, la
variabile carattere è convertita in numerica;