Sie sind auf Seite 1von 10

SAS Base - Introduo

Abrindo o SAS

Inicie em: START ALL PROGRAMThe SAS System. Confirm that you get three windows.
. !ditor fi"e. # Para $rogramar em SAS.
%. Log fi"e. # Consu"te sem$re &ue o $rograma n'o for e(ecutado.
). Out$ut fi"e. A$resenta o resu"tado dos seus $rogramas.






























C"ic* !($"orer
to "oo* at the data sets. See ne(t $age on this.
C"ic* on this + mar* to cance"
when the $rogram is running.
C"ic* this man to run your $rogram.

DataSet
DataSet arquivo de dados do SAS.

Criar DataSet (Arquivo) Bsico
(Salvo no diretrio padro Libraries/Work/)

data MeuDataSetBasico;
Nome="Yvert Evina";
Idade=27;
Altura=1.65;
run;




Para ,er esta te"a- confira os $assos a .ai(o.

. C"ic*
!($"orer
%. C"ic*
Li.raries
). C"ic*
/or*
0. C"ic*
data set 1 MeuDataSetBasico.


Criar/Salvar DataSet em outro diretrio (Uma library)
Por padro no nome da library (libname) pode ter no Maximo 8 caracteres.

libname MeuDir "C:\00SAS";
data MeuDir.MeuDataSetBasico;
Nome="Yvert Evina";
Idade=27;
Altura=1.65;
run;



Imprimir DataSet

DataSet salvo no diretrio padro de DataSets:

proc print data=MeuDataSetBasico;
run;

DataSet em outro diretrio:

libname MeuDir "C:\00SAS";
proc print data=MeuDir.meudatasetbasico;
run;


Obtendo estrutura do DataSet

proc contents data=MeuDataSetBasico;
run;



Obtendo estrutura dos DataSets em uma library

libname MeuDir "C:\00SAS";
proc contents data= MeuDir.MeuDataSetBasico;
run;



Obtendo Lista dos DataSets de uma library

libname MeuDir "C:\00SAS";
proc contents data=MeuDir._ALL_ nods;
run;
















Criando DataSet com dados outro DataSet

data MeuDataSetBasico_Copiado;
set MeuDataSetBasico;
run;

Selecionando colunas incluindo

data MeuDataSetBasico_Copiado;
set MeuDataSetBasico;
keep NOME SEXO;
run;

Selecionando colunas excluindo

data MeuDataSetBasico_Copiado;
set MeuDataSetBasico;
drop NOME SEXO;
run;



Criando DataSet tabular com CARDs - Simples
Por padro, na leitura dos dados, o SAS considera o espao como delimitador de campo.
Por padro, os campo so lidos como nmeros.
$ indica que o campo um texto - Para texto o tamanho padro 8 caracteres.

data Usuarios;
infile cards;
input /* Define a estrutura desse novo DataSet */
NOME $
SEXO $
IDADE
PESO
SALARIO
DT_NASC_TXT $
SALDO
;
cards;
CARLOS M 18 68.0 0170050 12/06/2012 8567.58
CARLA F 26 72.0 0057875 12/06/2012 18567.28
SILVIA F 21 60.5 0365063 12/06/2012 567.78
MARCIO M 32 85.4 2458230 12/06/2012 1927565.48
MARCIO M 32 85.4 1000582 12/06/2012 1528567.18
;
run;















Criando DataSet tabular Declarando o delimitador de coluna

data Usuarios;
infile cards DELIMITER="&";
input /* Define a estrutura desse novo DataSet */
NOME $
SEXO $
IDADE
PESO
SALARIO
DT_NASC_TXT $
SALDO
;
cards;
CARLOS&M&18&68.0&0170050&12/06/2012&8567.48
CARLA&F&26&72.0&0057875&12/06/2012&18567.48
SILVIA&F&21&60.5&0365063&12/06/2012&567.48
MARCIO&M&32&85.4&2458230&12/06/2012&1528567.48
MARCIO&M&32&85.4&1000582&12/06/2012&1528567.48
;
run;


Criando DataSet tabular Declarando a rego de leitura para cada campo

data Usuarios;
infile cards;
input /* Define a estrutura(campos, tipo, ordem) desse novo DataSet */
NOME $ 1-8
SEXO $ 9
IDADE 11-12
PESO 14-17
SALARIO 19-25 .2 /*Aplica em 2 casa decimais, na leitura*/
DT_NASC_TXT $ 26-36
SALDO $ 38-49
;
cards;
CARLOS M 18 68.0 0170050 12/06/2012 8.567,48
CARLA F 26 72.0 0057875 12/06/2012 18.567,48
SILVIA F 21 60.5 0365063 12/06/2012 567,48
MARCIO M 32 85.4 2458230 12/06/2012 1.528.567,48
MARCIO M 32 85.4 1000582 12/06/2012 1.528.567,48
;
run;

INPUT <varivel> [$] <incio>-<fim> [.decimal] ;
varivel Nome do campo de dados.
$ Caractere opcional que indica que a varivel s ir conter dados
alfanumricos. Sem o $, significa que a varivel ser numrica.
incio Valor que indica a posio inicial da varivel na linha de dados.
fim Valor que indica a posio final da varivel na linha de dados.
.decimal 2a"or o$ciona"- indica o n3mero de $osi45es decimais de
uma ,ari6,e" num7rica.









Criando DataSet tabular Formatando Input com tipos de dados predefinidos
do SAS

data Usuarios;
infile cards;
INFORMAT
NOME $8.
SEXO $1.
IDADE COMMA2.
PESO COMMA3.1
SALARIO COMMA7.2
DT_NASC_TXT DDMMYY10. /* Trnf data para TimeStamp */
SALDO $11.
;
input
NOME
SEXO
IDADE
PESO
SALARIO
DT_NASC_TXT
SALDO
;
cards;
CARLOS M 18 68.0 0170050 12/06/2012 8.567,48
CARLA F 26 72.0 0057875 12/06/2012 18.567,48
SILVIA F 21 60.5 0365063 12/06/2012 567,48
MARCIO M 32 85.4 2458230 12/06/2012 1.528.567,48
MARCIO M 32 85.4 1000582 12/06/2012 1.528.567,48
;
run;



Criando DataSet tabular alterando o tamanho de um campo durante a
leitura

data Usuarios;
infile cards;
length
NOME $ 2 /* Para ler apenas 2 caracteres do campo NOME */
;
input
NOME $ 1-8
SALARIO 19-25 .2
;
cards;
CARLOS M 18 68.0 0170050 12/06/2012 8.567,48
CARLA F 26 72.0 0057875 12/06/2012 18.567,48
;
run;






Criando DataSet tabular acrescentando colunas calculadas

data Usuarios;
infile cards;
input
NOME $
SEXO $
IDADE
;
/*Colunas calculadas*/
IDADE_DOBRO = IDADE * 2;
if IDADE < 18 then FAIXA_ETARISA = "Jovem";
if IDADE >= 18 then FAIXA_ETARISA = "Adulto";

cards;
CARLOS M 12 68.0 0170050 12/06/2012 8567.58
CARLA F 26 72.0 0057875 12/06/2012 18567.28
;


DataSet a partir de um arquivo texto

Todos os recursos de leitura de card, podem ser usados na leitura de arquivo.


data MeudataSet_do_TXT;
infile "C:\00SAS\usuarios.txt";
input
NOME $
SEXO $
IDADE
PESO
SALARIO
DT_NASC_TXT $
SALDO $
;
run;


DataSet a partir de um Ms-Excel


libname Usu_xsl "C:\00SAS\usuarios.xls";
data MeudataSet_do_XLS;
set Usu_xsl.'Planilha2$'n;
run;
/*Para liberar o arquivo...*/
libname Usu_xsl clear;

Ou

PROC IMPORT
OUT= MeudataSet_do_XLS
DATAFILE= "C:\00SAS\usuarios.xls"
DBMS=EXCEL2000 REPLACE;
GETNAMES=YES;
SHEET="Planilha2";
RUN;




Ms-Excel a partir de um DataSet

libname novo_xsl "C:\00SAS\novo.xls";
data novo_xsl.PlanilhaNova;
set Usuarios;
run;
libname novo_xsl clear;



Incluindo clausula para a Listagem

proc print data=Usuarios;
where ALTURA <= 1.50;
run;




Selecionando Colunas e sua Ordem na Listagem

proc print data=Usuarios;
var ALTURA NOME; /*Invert a ordem e, descartei o Id */
run;



Filtragem personalizada

proc print data=Usuarios;
var ALTURA NOME;
where (ALTURA <= 1.50) or (NOME = "Yvert");
run;




Manipulando Colunas

data ABC;
set sashelp.Class;

weight_metric=weight*0.45359237;
height_metric=(height* 2.54)/100 ;
BMI=weight_metric/(height_metric**2);

length status $ 15; /* length usado durante a leitura dos dados*/

If BMI < 18.5 then status="Magro";
If BMI => 18.5 and BMI < 25 then status="Normal";
If BMI => 25 and BMI < 30 then status="Gordo";
If BMI >= 30 then status="Obeso";
run;








Mais manipulaes de Colunas


data abc;
set sashelp.Class;
var1=height+weight;
var2=sum(of height weight);
var3=weight-height;
var4=height*weight;
var5=height/weight;
var6=1/(height+weight);
var7=mean(of height weight);
var7B=mean(height, weight);/*this way is okay too*/
var8=max(of height weight);
var9=min(of height weight);
var10=log(height);
var11=abs(var3); /*Absolute values */
var12=nmiss(of height weight);/*N of missing cases*/
var13=n(of height weight); /*N of observations*/
var14=name||sex;
var15=compress(name||sex);/*COMPRESS gets rid of space in between*/
var16=substr(name,1,3);/*take the first 3 letters starting from the first letter*/
var17=upcase(name);/*upper case*/
run;


How is Z=mean(of X1 X2 X3) different from Z=(X1+X2+X3)/3;?
How is Z=sum(of X1 X2 X3) different from Z=X1+X2+X3;?

Functions, such as mean(of ) or sum (of ), take statistics of non-
missing values. They do return values even when some of the variables in
the brackets are missing. For example, if X1 is missing:
X=mean (of X1 X2 X3); will return the average of X2 and X3.
In contrast,
X=(X1+X2+X3)/2 will return a missing value, namely, .

Application: How do we restrict analytical samples using NMISS function
Imagine we are running several procedures on your data. We want to always
be using the same number of observations, but sometimes depending on the
pattern of missing values, it is hard to use the same data for each
procedure. Here is a way to force your sample to be the same, by making
sure that you are using a set of variables that does not have any missing
values. (I forget what pairwise deletion and listwise deltion meant, but
this is to do one of them, which is a stricter way of selecting cases.)

Use NMISS function to create a new variable john.

data ABC;
set sashelp.class;
if name="Janet" then height=.; /*just imagine Janet was missing a value for
her height*/
X=nmiss(of height weight);/*this returns the number of missing cases*/
run;




proc means data=ABC;
where X=0; /*Run only when X=0, namely, number of missing cases is 0*/
var weight height;
run;

proc reg data=ABC;
where X=0; /*Run only when X=0, namely, number of missing cases is 0*/
model height=weight;
run;


Criando DataSet Ordenado

proc sort
data=sashelp.class
out=kaz;
by AGE;
run;


Mdias

PROC MEANS data=ABC;
VAR AGE HEIGHT;
run;


S$ecify mean- 8- ST9- MA:- and MI8
PROC M!A8S data;*a< mean n std ma( min stderr=
2AR mat> mat?=run=
run=

Das könnte Ihnen auch gefallen