You are on page 1of 200

Introduo ao SAS

Profa Connie McManus Pimentel Universidade de Braslia

Introduo ao SAS/ produtos do SAS SAS Fundamentals Data Step Proc Steps Informaes sobre programao em SAS SAS Macro

Base SAS - data management and basic procedures SAS/STAT - statistical analysis SAS/GRAPH - presentation quality graphics SAS/OR - Operations research SAS/ETS - Econometrics and Time Series Analysis SAS/IML - Interactive Matrix Language SAS/SQL Structural query language

Statistical Analysis System, Desenvolvido em 1970s na North Carolina State University SAS Institute Inc. formado em 1976 Software de estatstica mais usado em industria e universidades

SAS windows
escolha WindowTile Horizontally

Type code

Cdigo Submitido ERROs, AVISOs, informaes


See output

SAS User Interface


Tool bar similar as aplicaes do Windows

Run para rodar SAS code

Ajuda
Nova janela
Explorer Window

Log Window

Salvar

Editor Window Results Window (not shown)

Output (no mostrado)

Editor
Editor Window tem os dados e programas de SAS

Explorer

Libraries Folder Tem bancos de dados criados no SAS

Explorer Window

Libraries Folder
Contedo do Work Folder So conjuntos de dados criados no SAS Via inputting dados e Via criando conjuntos de dados Usando programas de SAS

Contedo do Libraries Folder

Work Folder tem os conjuntos De dados criados no SAS

Log Window

Log Window tem um relatrio de Todos os comandos submitidos para SAS e mostra erros

Output Window

Output Window tem output baseado em programas de SAS programs submitido no Editor Window.

Results Window
The Results Window mostra uma listagem dos programas de SAS submitido na ordem de submisso Click on any procedure to view all output parts of the procedure and click on any individual part to view the actual output.

SAS Help

Base SAS
The Data Set
Processamento Uma etapa a cada vez Libraries & Catalogs

Base SAS
SAS Statements (Global, Data, & Proc) Opes de Sistema Formatos & Infomatos Funes

Data Set
Descritor: Nomes das variveis, Labels, Tipos de dados, formatos etc Var Nome data idade altura peso Obs1 Obs2

Processamento
SAS System compila e depois exceuta cada step em sequencia Limite de step marcado por RUN; As vezes QUIT; ou o inicio da proxima step data MYDATA ; SAS Statements ; ... run ; proc print data=MYDATA ; run ;

Step-at-a-time Processing
SAS System Compila e depois executa cada etapa em sequencia O limite da etapa marcada por RUN; As vezes QUIT; ou o inicio da proxima etapa data MYDATA ; SAS Statements ; ... run ; proc print data=MYDATA ; run ;

The DATA step

II. Getting data in


- with DATALINES - with INFILE

The IMPORT procedure

DATA step with DATALINES


Type in Editor window

Basic structure DATA datasetname; ; Each statement ends with a semicolon; RUN; INPUT describes the format
character variables followed by $ DATALINES introduces the data. /* comments enclosed */

(A word on DATA steps)

Submitting code
Highlight and submit code

Viewing data: The PRINT procedure

(A word on PROCEDURES)
Basic structure PROC procedure DATA=dataset; ; RUN; Each statement ends with a semicolon; E.g., PROC print DATA=MissAmericas; RUN;

DATA step with DATALINES


If you have a lot of lines, typing DATALINES is inconvenient.

DATA step with INFILE

The IMPORT procedure


If there are a lot of variables in different formats, the INPUT statement can be cumbersome:

IMPORT figures out the variable types (numeric, character, etc.) and takes variable names (Year, Name, etc.) from the first row.

Where is the data?

Data file: Missamericas Library: Work (default) Work is a temporary folder on some disk
erased when you quit SAS

Making a permanent SAS data set

MYLIB points to X:\. You could also say X:\somefolder\ or. MYLIB.missamericas is a file on X:\ If no library were specified, WORK (temp disk space) would be assumed. Can you find mylib and missamericas in the SAS Explorer? Can you find missamericas.sas7bdat on the X: drive?

Retrieving a permanent SAS data set


Quit SAS and relaunch it

See? The contents of mylib are permanent.

Saving code
Name the contents of the Editor window
currently blank

Select the editor window Choose FileSave as


X:\brownbag.sas

Save regularly

The DATA step with SET


Define some new variables

V. Analysis

PROC Steps
Data Management
Proc Compare Proc Contents Proc Datasets Proc Format Proc Print Proc Sort Proc SQL Proc Transpose

Basic Statistics
Proc Corr Proc Freq Proc Means Proc Rank Proc Summary Proc Tabulate Proc Univariate

Using proc ttest


data Grades.two; input gender$ x ; cards; M 51 F 38 M 49 M 56 F 40 F 36 F 55 M 51 M 60 M 18 F 25 ; run;

Using proc ttest (cont.)


ods rtf file = C:\Documents and Settings\Owner\My Documents\ttestOutput.rtf; proc ttest data=Grades.two; class gender; var x; run; Syntax: PROC ttest DATA=datasetname; ods rtf close;
CLASS factorvars; VAR responsevar; RUN;

DATA A ; INPUT SEXO $ X @@ ; CARDS ; F 75 F 76 F 80 F 77 F 80 F 77 F 76 M 82 M 80 M 85 M 85 M 78 M 87 M 82 PROC TTEST ; CLASS SEXO ;

DATA ARQ ; INPUT TIPO GRUPO $ X Y ; CARDS ; 1 A 32.6 112 1 B 36.4 130 1 A 29.5 106 1 B 36.3 112 1 A 42.5 104 1 B 42.0 115 1 A 44.5 114 1 B 35.3 106 1 A 28.5 105 1 B 41.8 121 2 A 42.7 139 2 B 49.0 139 2 A 41.5 139 2 B 53.0 139 2 A 33.5 139 2 B 49.5 139 2 A 39.3 139 2 B 42.7 139 2 A 47.1 143 2 B 32.9 112 3 A 35.3 124 3 B 40.1 134 3 A 33.6 116 3 B 31.8 133 3 A 41.6 155 3 B 33.8 103 3 A 38.4 146 3 B 40.4 124 3 A 35.9 134 3 B 38.8 123 DATA PARTE ; SET ARQ ; IF TIPO LE 2 ; PROC TTEST DATA = ARQ ; CLASS GRUPO ; PROC TTEST ; CLASS GRUPO ; VAR X ; BY TIPO ; PROC TTEST DATA = PARTE ; CLASS TIPO ;

Proc Compare
Used to compare one data set to another Very flexible Useful when developing code
proc compare <options> data=onedata compare=two ; var variablelist ; out=compdata ; run ;

Proc Contents
Provides information about a SAS data set proc contents data=mydata ; run ;

Proc Contents Output


CONTENTS PROCEDURE Data Set Name: Member Type: Engine: Created: Last Modified: Protection: Data Set Type: Label: WORK.MYDATA DATA V612 16:49 Sunday, June 27, 1999 16:49 Sunday, June 27, 1999 Observations: Variables: Indexes: Observation Length: Deleted Observations: Compressed: Sorted: 4 2 0 16 0 NO NO

-----Engine/Host Dependent Information----Data Set Page Size: 8192 Number of Data Set Pages: 1 File Format: 607 First Data Page: 1 Max Obs per Page: 507 Obs in First Data Page: 4 -----Alphabetic List of Variables and Attributes----# Variable Type Len Pos ----------------------------------1 EXPENSE Num 8 0 2 TOTAL Num 8 8

DATA CLIMA INPUT MES$ 1-3 TEMP 5-8 PRECP UMIDADE; CARDS; JAN 33.4 155 94.3 FEV 34.1 181 95.6 MAR 32.3 101 90.3 ABR 30.3 90 91.4 MAI 27.8 76 75.9 JUN 27.1 84 78.3 JUL 21.2 40 60.8 AGO 19.0 12 58.3 SET 20.1 10 59.8 OUT 22.6 53 66.1 NOV 23.8 55 60.9 DEZ 28.9 89 79.5 ; PROC CONTENTS DATA = CLIMA; RUN; PROC CONTENTS POSITION DATA = CLIMA; RUN;

Proc Datasets
Library contents maintenance copy data sets delete data sets index data sets etc. proc datasets library=libref ; delete mydata ; run ; quit ;

Proc Format
Use proc format to create custom formats and informats This is a powerful technique for transforming custom data proc format ; value agefmt low-12=child 13-19=teen 20-high=adult ; run ;

Proc print
Sends data to output flexible appropriate for must reporting needs proc print data=mydata split=*; var varlist ; id varlist ; by varlist ; run ;

PROC FORMAT ; VALUE REL 1=ADVENTISTA 2=CATOLICA 3=ESPIRITA 4=UMBANDA ; DATA ARQ ; INPUT NOME $ 1-10 SEXO $ IDADE PESO ALTURA RELIGIAO ; IF IDADE = 99 THEN IDADE = . ; IF PESO = 99 THEN PESO = . ; IF ALTURA = 999 THEN ALTURA = . ; IF RELIGIAO = 9 THEN RELIGIAO = . ; CARDS ; ALFREDO M 14 69 112 2 ALICE F 13 56 84 2 BERNADETE F 14 62 102 2 MARTA F 13 65 98 4 HENRIQUE M 14 63 102 3 MARCOS M 12 57 83 3 HELENA F 12 59 84 2 CLARA F 15 62 102 2 MARCELO M 13 62 84 4 FREDERICO M 12 59 99 3 MATEUS M 12 59 99 2 ELIANE F 11 51 50 2 ALINE F 14 64 90 2 HELOISA F 12 56 77 2 TANIA F 15 66 112 3 FELIPE M 16 72 150 3 ROBERTO M 12 64 128 4 REINALDO M 15 67 113 1 MAURICIO M 11 57 85 2 ANA CELIA F 13 62 108 3 TOMAS M 99 99 85 9 EDUARDO M 15 66 112 2 PROC PRINT ;

Proc Sort
Sorts data by the listed variable Sorted data is necessary for merging and other by group processing proc sort data=mydata out=sorted; by sortvar ; run ;

Ordenao de observaes Comando SORT


DATA ARQX ; INPUT (V1 - V10) (1.) ; CARDS ; 1023480001 5023000101 3103120110 9112311000 6001300011 PROC SORT ; BY V1 ; PROC PRINT ;

Proc SQL
An excellent tool for beginning SAS coders who are experienced with SQL Great tool for use with SAS/Access to pull data from Oracle, DB2, etc. proc sql ; sql statemnts ; quit ;

Proc Transpose
What to use when rows is columns and columns is rows? Transpose! proc transpose data=mydata ; by varlist ; copy varlist ; run ;

Proc Corr
Computes Correlation proc corr data=mydata ; Coefficients between variables run ; Spearman, and Kendall, and Pearson, Oh-my!

Proc Freq
Counts data frequencies, prepares cross-tabulation tables and calculates descriptive statistics proc freq data=mydata ; tables var1*var2 ; run ;

DATA GRUPO ; INPUT NOME $ 1-10 SEXO $ IDADE ALTURA PESO RELIGIAO ; CARDS ; ALFREDO M 14 69 112 2 ALICE F 13 56 84 2 BERNADETE F 14 62 102 2 MARTA F 13 65 98 4 HENRIQUE M 14 63 102 3 MARCOS M 12 57 83 3 HELENA F 12 59 84 2 CLARA F 15 62 102 2 MARCELO M 13 62 84 4 FREDERICO M 12 59 99 3 MATEUS M 12 59 99 2 ELIANE F 11 51 50 2 ALINE F 14 64 90 2 HELOISA F 12 56 77 2 TANIA F 15 66 112 3 FELIPE M 16 72 150 3 ROBERTO M 12 64 128 4 REINALDO M 15 67 113 1 MAURICIO M 11 57 85 2 ANA CELIA F 13 62 108 3 TOMAS M 99 99 85 9 EDUARDO M 15 66 112 2 PROC FREQ ;

DATA GRUPO ; INPUT NOME $ 1-10 SEXO $ IDADE ALTURA PESO RELIGIAO $ ; IF IDADE LE 13 THEN IDADE = 1 ; IF IDADE GT 13 THEN IDADE = 2 ; IF IDADE EQ 99 THEN IDADE = . ; IF PESO EQ 99 THEN PESO = . ; IF ALTURA EQ 999 THEN ALTURA = . ; IF RELIGIAO EQ 9 THEN RELIGIAO = . ; CARDS ; linhas de dados DATA G2 ; SET GRUPO ; IF ALTURA LT 100 ; PROC FREQ DATA = GRUPO ; TABLES IDADE ; LABEL IDADE 'GRUPOS: ATE 13 E MAIS DE 13 ANOS' ; TITLE 'TABELA DE IDADE PARA O ARQUIVO - GRUPO' ; PROC SORT DATA = GRUPO ; BY SEXO ; PROC FREQ ORDER = FREQ ; TABLES ALTURA PESO ; BY SEXO ; TITLE 'TABELA DE ALTURA E PESO PARA ARQUIVO-GRUPO, POR SEXO' ; PROC FREQ DATA = G2 ; TITLE 'DATA SET G2 - INCLUI REGISTROS COM ALTURA < 100' ; TITLE2 CRUZAMENTOS COM OPCOES - PRIMEIRA TABELA' ; TABLES SEXO * RELIGIAO / CHISQ EXPECTED ; PROC FREQ ; TITLE2 CRUZAMENTOS COM OPCOES - SEGUNDA TABELA' ; TABLES SEXO * RELIGIAO * IDADE / NOPERCENT ;

Proc Means
Calculates statistics
N, NMISS, MIN, MAX, RANGE, SUM, SUMWGT, MEAN, USS (soma dos quadrados no corrigida), CSS (corrigida), VAR, STD (desvio padro), STDERR (erro padro), CV, SKEWNESS, KURTOSIS, T, PRT

proc means ; var x ; by y ; output out=data mean= ; run ;

DATA A ; INPUT TESTE1 TESTE2 LUGAR $ DIA @@ ; CARDS ; 04 54 S 1 07 70 N 1 10 69 N 2 04 52 S 1 07 70 S 2 08 74 N 1 04 60 S 1 07 62 S 2 07 80 N 1 06 61 S 2 06 77 N 2 08 75 N 2 PROC MEANS ;

N, NMISS, MEAN, STD, MIN, MAX, RANGE, SUM, VAR, USS, CSS, STDERR, CV, SKEWNESS, KURTOSIS, T, PRT, SUMWGT.

DATA A ; INPUT TESTE1 TESTE2 LUGAR $ DIA @@ ; CARDS ; 04 54 S 1 07 70 N 1 10 69 N 2 04 52 S 1 07 70 S 2 08 74 N 1 04 60 S 1 07 62 S 2 07 80 N 1 06 61 S 2 06 77 N 2 08 75 N 2 PROC MEANS ; VAR TESTE1 TESTE2 ; PROC MEANS DATA = A MAEXDEC = 3 NMISS RANGE USS CSS SKEWNESS KURTOSIS T PRT ; VAR TESTE1 TESTE2 ; TITLE 'ESTATISTICAS SELECIONADAS ; PROC SORT ; BY LUGAR DIA ; PROC MEANS MAXDEC = 3 ; BY LUGAR DIA ; VAR TESTE1 TESTE2 ; OUTPUT OUT = NOVO MEAN = T1MED T2MED STDERR = T1EM T2EM TITLE 'ESTATISTICAS POR LUGAR E DIA' ; PROC PRINT DATA = NOVO ; TITLE 'NOVO DATA SET' ;

Teste T Pareado
Feita pela PROC MEANS DIFERENA ENTRE DUAS VARIVEIS INCLUE OPES T E PRT PARA DIFERENA Exemplo: DATA QUEST ; INPUT V1 1-2 V2 4-5 V3 7-8 ; LABEL V1 = NRO DO ANIMAL V2 = PESO ANTES DO TRATAMENTO V3 = PESO DEPOIS DO TRATAMENTO ; DIF = V3 - V2 ; CARDS ; 01 80 82 02 73 71 03 50 45 04 60 65 05 80 75 06 65 67 07 55 60 PROC MEANS MEAN STDERR T PRT ; VAR DIF ;

Proc Rank
Creates ranks for numeric data in a SAS data set proc rank data=mydata ; by varlist ; ranks newlist ; run ;

Proc Summary (resumo dos dados)


A lot like proc means Does not print to output by default Other minor differences proc summary data=mydata ; var varlist ; class varlist ; by varlist ; output out=newdata mean= ; run;

Proc Tabulate
Prints descriptive statistics Does not create a output SAS data set proc tabulate data=mydata ; var varlist ; table <<pg>,row>,col ; run ;

Proc Univariate (ver se os dados tem distribuio normal)


Proc Univariate calculates additional statistics Use when you need quantiles Provides details on extremes proc univariate data=mydata ; var varlist ; by byvar ; output out=... ; run ;

The REG procedure

DATA REGR; INPUT ANO Y X1 X2 @@; CARDS; . . . PROC REG; MODEL Y=X1 X2;

DATA REGR ; INPUT ANO Y X1 X2 @@ ; LABEL Y = INVESTIMENTOS X1 = CAPITAL X2 = LUCROS ; CARDS ; 1935 33.1 1170.6 97.8 1936 45.0 2015.8 104.4 1937 77.2 2803.3 118.0 1938 44.6 2039.7 156.2 1939 48.1 2256.2 172.6 1940 74.4 2132.2 186.6 1941 113.0 1834.1 220.9 1942 91.9 1588.0 287.8 1943 61.3 1749.4 319.9 1944 56.8 1687.2 321.3 1945 93.6 2007.7 319.6 1946 159.9 2208.3 346.0 1947 147.2 1656.7 456.4 1948 146.3 1604.4 543.4 1949 98.3 1431.8 618.3 1950 93.5 1610.5 647.4 1951 135.2 1819.4 671.3 1952 157.3 2079.7 726.1 1953 179.5 2371.6 800.3 1954 189.6 2759.9 888.9 PROC REG ; MODEL Y = X1 X2 ;

Proc ANOVA

EFEITOS A: EFEITO DE A A*B : INTERAO A(B): A DENTRO DE B A|B|C: TODOS OS EFEITOS PRINCIPAIS E CRUZADOS

Exerccio
Bloco Tratamento Peso 1 1 142,36 1 2 144,78 1 3 145,19 1 4 138,88 2 1 138,28 2 2 137,77 2 3 144,44 2 4 130,31 3 1 140,73 3 2 134,06 3 3 136,07 3 4 144,11

CALCULAR MEDIA, ERRO PADRO, DESVIO PADRO, POR TRATAMENTO E POR BLOCO.

Parcelas subdivididas
4 parcelas (variedades), 4 subparcelas (tratamento de sementes) 4 blocos (BANZATTO & KRONKA, 1989, p179)
VARIEDADES (A) TRATAMENTOS DE SEMENTES (B) B1 B2 B3 B4 B1 B2 B3 B4 B1 B2 B3 B4 B1 B2 B3 B4 1 42.9 53.8 49.5 44.4 53.3 57.6 49.8 64.1 62.3 63.4 64.5 63.6 75.4 70.3 68.8 71.6 BLOCOS 2 41.6 58.5 53.8 41.8 69.6 69.6 65.8 57.4 58.5 50.4 46.1 56.1 65.6 67.3 65.3 69.4 3 28.9 43.9 40.7 28.3 45.4 42.4 41.4 44.1 44.6 45.0 62.6 52.7 54.0 57.6 45.6 56.6 4 30.8 46.3 39.4 34.7 35.1 51.9 45.4 51.6 50.3 46.7 50.3 51.8 52.7 58.5 51.0 47.4

A1

A2

A3

A4

ARQUIVO DE DADOS
DATA TESTE3; INFILE "C:PS.DAT"; DO V=1 TO 4; DO T= 1 TO 4; DO B=1 TO 4; INPUT Y @; OUTPUT; END; END; END; PROC ANOVA; CLASS V T B; MODEL Y = B V B*V T V*T; TEST H=B V E=B*V; MEANS V T V*T; RUN;
42.9 53.8 49.5 44.4 53.3 57.6 49.8 64.1 62.3 63.4 64.5 63.6 75.4 70.3 68.8 71.6 41.6 58.5 53.8 41.8 69.6 69.6 65.8 57.4 58.5 50.4 46.1 56.1 65.6 67.3 65.3 69.4 28.9 43.9 40.7 28.3 45.4 42.4 41.4 44.1 44.6 45.0 62.6 52.7 54.0 57.6 45.6 56.6 30.8 46.3 39.4 34.7 35.1 51.9 45.4 51.6 50.3 46.7 50.3 51.8 52.7 58.5 51.0 47.4

PROC GLM

Experimento com 3 tratamentos, com n1 = 3, n2 = 2 e n3 = 1 repeties (SEARLE, p. 219)


data teste4; input trat y ; cards; 1 101 1 105 1 94 2 84 2 88 3 32 ; title "exemplo 4 curso do SAS" PROC PRINT DATA = TESTE4; PROC GLM; CLASS TRAT; MODEL Y = TRAT; MEANS TRAT / TUKEY; CONTRAST "1 & DEMAIS" TRAT 3 - 2 - 1, "2 & 3 " TRAT 0 2 - 2 ; RUN;

Sem interao - efeitos fixos.

DATA TESTE5; INPUT REPR RAO Y; CARDS; 115 116 122 123 212 213 215 216 217 228 228 229 313 324 324 326 326 327 ; TITLE " EXEMPLO 5 CURSO SAS "; PROC PRINT DATA = TESTE5; PROC GLM; CLASS REPR RAO; MODEL Y = REPR RAO; MEANS REPR RAO/DUNCAN;

DATA TEST6 INPUT REPR RAO Y CARDS; 115 116 122 123 212 213 215 216 217 228 228 229 313 324 324 324 326 326 327 ; TITLE " EXEMPLO 6 CURSO SAS"; PROC GLM; CLASS REPR RAO; MODEL Y = REPR RAO REPR*RAO; MEANS REPR*RAO / TUKEY; RANDOM REPR RAO REPR*RAO / Q;

Com interao - efeitos aleatrios

Efeito da rao no crescimento de suinos como covarincia o peso inicial.


----------------------------------------------------------------------------------------------------------------RAO 1 RAO 2 RAO 3 ---------------------------------------------------------------- ------------------------------------PESO IN. GANHO PESO PESO IN. GANHO PESO PESO IN. GANHO PESO ------------------------------------------------------------------------------------------------------------------5 3 4 5 8 9 5 7 6 7 11 6 0 2 3 3 2 8 7 2 10 8 8 6 2 3 2 4 12 9 5 8 -----------------------------------------------------------------------------------------------------------------

7 6 4 3

DATA TESTE7; INPUT TRAT W Y ; CARDS; 153 195 1 11 6 132 245 276 202 287 2 10 8 223 2 12 9 258 387 376 334 323 386 324 ; TITLE "EXEMPLO 7 DO SAS"; PROC PRINT DATA = TESTE7; PROC GLM; CLASS TRAT; MODEL Y = TRAT W ; RUN;

VI. Getting results out


The Output window The Output Delivery System The EXPORT procedure

Save the output window


Click in the output window Choose FileSave As

Save selected results with ODS


Every part of the output has a name If you know the name
you can save that part of the output
as a data file

To do this, use the Output Delivery System (ODS)

Saving output using Output delivery System (ODS)


Goal: To provide more flexibility in producing output from SAS data steps and procedures. Using ODS, output can be produced in the following formats: rtf, pdf, postscript, html. Many procedures produce ODS objects, which can then be output in any of these formats. You can open an ODS output stream with the ODS command and a destination keyword. For example, to produce rtf formatted output from the print procedure:
ods rtf file=C:\Documents and Settings\Owner\My Documents\printOutput.rtf; proc print data=Grades.one; run; ods rtf close;

Whats the outputs name?

Saving the output

int_slope is a data set Can you find it?

Doctoring the output

The EXPORT procedure

Results.xls is an Excel file. Can you find it?

IV. Graphics

PROC GPLOT

PROC GPLOT

PROC GPLOT

PROC GPLOT

PROC GPLOT

PROC GPLOT

PROC GHART

PROC GHART

PROC GHART

PROC GHART

PROC GHART

Proc Gchart
If you need to make bar charts, use proc gchart. Use can use axis options like in gplot and the pattern global option to set the bar fill colors and patterns. If your plotting variable is categorical you get one bar per value. If you are plotting a continuous variable the procedure will group the data for you; unless you tell it otherwise. pattern1 color = blue; Don't group proc gchart; vbar boys /discrete; run;

Proc Gchart(2)
Notice that the default statistic for vbar is frequency. Additional statistics you can request include cfreq (cumulative frequency), cpercent (cumulative percent), and percent. You can get these by including this on the vbar line: / type= statisticName proc gchart; vbar month ; run;

Proc Gchart(3)
You can tell it how many groups you want like this: proc gchart; vbar month /levels = 4; run; quit;

Proc Gchart (4)


Formats can define groups proc format; value season 2-4 = "Spring" 5-7 = "Summer"; run; proc gchart; format month season.; vbar month /discrete; run;

Proc Gchart (5)


proc format; value season 11, 12, 1 = "Winter" 2-4 = "Spring" 5-7 = "Summer" 8-10 = "Fall"; run; /*months in each quarter*/ proc gchart; format month season.; vbar month / discrete; run; quit;

Proc Gchart(6)
You can specify two different statistics one to appear just above or below the bars:
proc gchart; format month season.; vbar month/outside=cfreq inside=freq; run; quit;

Basic HBAR
If you do a hbar you get frequency, cumulative frequency, percent, and cumulative percent information by default. If you want a different statistic just include it after the / If you dont want statistics include / nostats proc gchart; hbar boys /discrete; run;

Filling in Colors
On the chart line you can specify fill = x to use cross hatched fill patterns or fill = s for solid fill colors. Use solid colors if possible. You can set how patterns are applied by using patternid = on the chart line. Patternid can take these values by, group, midpoint, subgroup. Midpoint gives you a different pattern for each bar (or pie slice). Avoid using more than 9 different color/pattern combinations.

More on Bar Charts


You can group by subvariables:
proc gchart data = dxinfo; hbar ca_site / discrete group =sex; vbar ca_site / discrete group =sex; where sex ne 9; run; quit;

SEX Ma l e

F e ma l e

Co d e # 1 Ov a r y 2 Br e a s t 3 Ut e r u s / E n d o 4 Ce r v i x 5 Bl a d d e r 6 I nt est i ne 7 Pr o s t a t e 8 B o n e / S a r c o ma 9 CNS 1 0 F e ma l e NOS 1 2 Ma l e NOS 1 3 He a d & Ne c k 1 4 Ki d n e y 1 5 L e u k e mi a 16 Li ver 17 Lung 1 8 L y mp h o ma / My e l o ma 1 9 Ot h e r , NOS 2 0 Ot h e r - S p e c i f i e d 2 1 Pa n c r e a s 2 2 Sk i n 2 3 S t o ma c h / E s o p h 24 Thy r oi d 2 5 Ur i n a r y NOS 2 6 Pe r i t o n e u m 5 0 Oo p h o r e c t o my - P r o p h 5 2 Hy s t e r e c t o my 5 3 Ma s t e c t o my 5 4 Oo p h o r e c t o my - Ot h e r 5 5 B r e a s t Di s e a - B e n i g n 9 6 Me n t a l Re t a r d a t i o n 9 7 Ov a r i a n Cy s t 9 8 Ov a r i a n T u mo r - B e n i g 9 9 Un k n o wn 1 Ov a r y 2 Br e a s t 3 Ut e r u s / E n d o 4 Ce r v i x 5 Bl a d d e r 6 I nt est i ne 7 Pr o s t a t e 8 B o n e / S a r c o ma 9 CNS 1 0 F e ma l e NOS 1 2 Ma l e NOS 1 3 He a d & Ne c k 1 4 Ki d n e y 1 5 L e u k e mi a 16 Li ver 17 Lung 1 8 L y mp h o ma / My e l o ma 1 9 Ot h e r , NOS 2 0 Ot h e r - S p e c i f i e d 2 1 Pa n c r e a s 2 2 Sk i n 2 3 S t o ma c h / E s o p h 24 Thy r oi d 2 5 Ur i n a r y NOS 2 6 Pe r i t o n e u m 5 0 Oo p h o r e c t o my - P r o p h 5 2 Hy s t e r e c t o my 5 3 Ma s t e c t o my 5 4 Oo p h o r e c t o my - Ot h e r 5 5 B r e a s t Di s e a - B e n i g n 9 6 Me n t a l Re t a r d a t i o n 9 7 Ov a r i a n Cy s t 9 8 Ov a r i a n T u mo r - B e n i g 9 9 Un k n o wn 0 20 40 60 80 100 120 140 160 180

Ca n c e r

Si t e

as

F RE Q.

CUM. F RE Q.

P CT .

CUM. P CT .

F RE QUE NCY

F RE QUE NCY 180 170 160 150 140 130 120 110 100 90 80 70 60 50 40 30 20 10 0 1 2 3 4 56 7 8 9 11 1 1 02 3 4 OB UCBI P B C v r t e l n r o N F MH K a e e r at o n Sea e i r a r v d e s e ml a d y s u i ds t / ae d n t s x et a S l e / r i t a eN&y E n e r O n e c NS N d o O e o m S c a k 1 1 11 1 2 2 22 2 2 2 55 5 5 5 9 99 9 5 6 78 9 0 1 23 4 5 6 02 3 4 5 6 78 9 L L e i u v k e e r m i a L u n g L OOP y t t a mh h n p e e c h r r r o , - e m Sa a Np s / Oe MS c y i e f l i o e m d a SS T k t h i o y n mr a o c i h d / E s o p h U P OH M O B M OO U r e oy a o r e v v n i r ps s p e n aa k n i ht t h a t r r n a t oe e o s a i i o r o r r c r t l aa w y n ee t e nn n e c c o c DR N u t t mt i e CT O mo o y o s t y u S mm m e a s m y y y a r t o - - d r P OB a r t e t B o h n i e p e i o n h r g n i n g 1 2 34 5 6 7 89 1 1 1 1 0 2 3 4 OB UC B I P B C v r t e l n r o N F MH K a e er a t o nSe a e i r a r v d e s e ml a d y s ui d s t / a e d n t s x e t a S l e / r i t a e N&y E n e r O n e c NSN d o O e o m S c a k 11 1 1 1 22 2 2 2 22 5 5 5 5 59 9 9 9 56 7 8 9 01 2 3 4 56 0 2 3 4 56 7 8 9 LL ei uv k e er m i a L u n g L O OP y t t a mh h n p e ec h r r r o , - e m Sa a Nps / Oe MS c y i e f l i o e m d a S k i n ST t h o y mr a o c i h d / E s o p h UP O H MO B MO O U r e o y a o r e v v n i r p s s p en a a k ni h t t h at r r n at o e e o sa i i o r o r r c r t l a a w y n e e t e n n n e c c o c DR Nu t t mt i e C T Om o o y o s t y u S mm me a s m y y y ar t o - - d r P OBa r t et B o h ni e p e i o n h r gn i n g SEX Ca n c e r Si t e as Co d e #

Ma l e

F e ma l e

Rotate the labels


I frequently want to rotate the label on the grouping axis.
axis1 label = (angle = 90 ); proc gchart data = Chart3; title "Chart3: Chart Parturition (Birth)"; hbar _all_ / discrete maxis = axis1; run; quit; title; axis1;

A GE 9 9 :

A GE ON 1 / 1 / 9 9 * * Er r o r * * 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 0 10 20 30 40 50 60 F RE QUE NCY 70 80 90 100 110 120

F RE Q. 1 65 80 67 77 76 93 71 91 70 67 113 98 85 72 78 94 91 73 63 48 65 60 62 45 40 76 45 55 49 31

CUM. F RE Q. 1 66 146 213 290 366 459 530 621 691 758 871 969 1054 1126 1204 1298 1389 1462 1525 1573 1638 1698 1760 1805 1845 1921 1966 2021 2070 2101

P CT . 0. 05 3. 09 3. 81 3. 19 3. 66 3. 62 4. 43 3. 38 4. 33 3. 33 3. 19 5. 38 4. 66 4. 05 3. 43 3. 71 4. 47 4. 33 3. 47 3. 00 2. 28 3. 09 2. 86 2. 95 2. 14 1. 90 3. 62 2. 14 2. 62 2. 33 1. 48

CUM. P CT . 0. 05 3. 14 6. 95 10. 14 13. 80 17. 42 21. 85 25. 23 29. 56 32. 89 36. 08 41. 46 46. 12 50. 17 53. 59 57. 31 61. 78 66. 11 69. 59 72. 58 74. 87 77. 96 80. 82 83. 77 85. 91 87. 82 91. 43 93. 57 96. 19 98. 52 100. 00

F RE Q. * * Er r o r * * 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 0 10 20 30 40 50 60 F RE QUE NCY 70 80 90 100 110 120 1 65 80 67 77 76 93 71 91 70 67 113 98 85 72 78 94 91 73 63 48 65 60 62 45 40 76 45 55 49 31

CUM. F RE Q. 1 66 146 213 290 366 459 530 621 691 758 871 969 1054 1126 1204 1298 1389 1462 1525 1573 1638 1698 1760 1805 1845 1921 1966 2021 2070 2101

P CT . 0. 05 3. 09 3. 81 3. 19 3. 66 3. 62 4. 43 3. 38 4. 33 3. 33 3. 19 5. 38 4. 66 4. 05 3. 43 3. 71 4. 47 4. 33 3. 47 3. 00 2. 28 3. 09 2. 86 2. 95 2. 14 1. 90 3. 62 2. 14 2. 62 2. 33 1. 48

CUM. P CT . 0. 05 3. 14 6. 95 10. 14 13. 80 17. 42 21. 85 25. 23 29. 56 32. 89 36. 08 41. 46 46. 12 50. 17 53. 59 57. 31 61. 78 66. 11 69. 59 72. 58 74. 87 77. 96 80. 82 83. 77 85. 91 87. 82 91. 43 93. 57 96. 19 98. 52 100. 00

Bad charts.
SAS will also let you specify subgroups (to give stacked subgroups). Do not do these graphics. People are bad at untangling the relative heights. There are great graphics for showing subgroups but SAS cant do them. SAS will let you generate pie charts by specifying pie instead of hbar or vbar but dont. There is good psychological research suggesting people are lousy at judging angles. Exploded pie diagrams are even worse.

Scatter Plots
You can get simple scatter plots out of the analysis procedure proc reg. For fancy plots use proc gchart.

VD data (valentines day)

You can get great looking scatter plots with proc gplot.
data foo; do month = 1 to 12; input kids boys @@; girls = kids - boys; output; end; datalines; 0 0 1 1 2 2 1 0 1 0 2 1 2 0 1 1 1 0 6 2 ; run; proc gplot;

2 1 0 0

*Plot kids on vertical axis & months on horizontal;

plot kids * month; run; quit ;

The Font
If you do not like the default font you change it with the graphic global option ftext .. goptions ftext='Verdana'; ... or you can specify a different font for different parts of the graphic. I will show this later.

kids 6

0 1 2 3 4 5 6 month 7 8 9 10 11 12

Simple Axis Values


By including / haxis= and/or vaxis= you can change the ranges on the axes.
proc gplot; *this plots the same axes but you could change the ranges and/or values by modifying the values after the equal signs; plot kids * month / vaxis = 0 1 2 3 4 5 6 haxis = 1 to 12 by 1; run; quit ;

Simple Tick Marks


If you want to control the number of small (minor) tick marks you can use the options vminor and/or hminor on the plot statement line. For example to remove the minor tick marks on the previous graph do this:
plot kids * month / vaxis = 0 1 2 3 4 5 6 vminor = 0 haxis = 1 to 12 by 1 hminor = 0;

Symbols
You can change the plot symbols by setting a goptions option.
symbol1 value=star; proc gplot; plot kids * month; run; quit;
From SAS OnLineDoc V8. Copyright (c) 1999 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Adding Markers
There is a SAS font called marker that has symbols. Set the font and use the single letters below to add in a symbol.

From SAS OnLineDoc V8. Copyright (c) 1999 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Symbols
You can use custom symbols like this: symbol1 font=marker value= N color=red height = 3; This letter was proc gplot; chosen from the plot kids * month; previous slide. Note that it is a capital run; letter and it is not in quotes. quit;

Other Symbol Stuff


If you need to join the symbols, set interpol = join or interpol = spline (a smoothed line). If you need a bigger plotting symbol set height= aNumber If you need a thicker line set width= aNumber Symbol options last for the duration of your SAS session and they are additive. So you can set and reset different aspects of the symbol on different lines of you code.

Reset Symbols
To reset an option, follow it by an equal sign symbol1 font=; If it is in a list follow the equal with a comma; symbol1 font=, value = dot; To reset all aspects of a symbol type its name followed by a semicolon; symbol1; To reset all symbols type this: goptions reset=symbol;

Symbol Options at a Glance


Value is abbreviated v= and the default is PLUS Height is abbreviated h= and the default is 1 cell Interpol is abbreviated i= and the default is none Width is abbreviated w= and the default is 1 pixel Color is abbreviated c= and the default is black You specify the size of units for height and width by including the word cell (character size) cm (centimeters), in (inches) pct (percent of the display area) or pt (points) after the number of units. ALWAYS specify a color.

Two Symbols
You can overlay two plots and use different symbols for each like this:
goptions reset=all; symbol1 height=3 width=3; symbol1 font=marker value=Q color=blue ; symbol2 height=3 width=3; symbol2 font=marker value=R color=pink ; proc gplot; plot boys * month girls * month/overlay; run; quit;

Axis options
axis1 order = (1 to 12 by 1) /*what values*/ /* appearance of label */ label=(height=5pct font='Times New Roman') width=3 major=(width = 3) /*width of ticks */ minor=none /* no minor ticks */ value=(height = 5pct font='Times New Roman'); proc gplot; label Month = "Birth Month"; Use the custom axis. plot boys * month girls * month/overlay haxis = axis1; run;quit;

goptions reset= all; symbol1 font=marker value=Q color=blue h=3 w=3; symbol2 font=marker value=R color=pink h=3 w=3; axis1 order = (1 to 12 by 1) label = (height=5pct font='Times New Roman') width = 3 major = (Width = 3) minor = none value = (height = 5pct f='Times New Roman'); axis2 order = (1 to 5 by 1) label = (height=5pct font='Times New Roman') width = 3 Set from 1 to 5 not 0 to 5 major = (Width = 3) minor = none value = (height = 5pct f='Times New Roman'); proc gplot; label Month = "Birth Month"; label boys = "Kids"; plot boys * month girls * month/overlay haxis = axis1 vaxis = axis2; run;quit;

Exporting Images
If you need to save the image:
put your plot inside ODS html code use edit menu and pick copy

If you want to save a single high resolution copy of the picture (that can be resized without changing the appearance of the picture) include this code before your plot:
filename theFile "C:\projects\blah.cgm"; goptions device=cgmOf97L; /*or =gif */ goptions gsfname=theFile gsfmode=replace; /* some SAS/GRAPH stuff */ goptions reset=all;

Saving Images
You can save your images in a SAS graphics catalog in a permanent library by including: gout=theLibname.theCatalogName on the proc gplot line. If you dont specify an output catalog your pictures will be saved in the work library in a catalog called Gseg. If the graphics catalog exists the new graphics will be added to it. If does not exist, it will be created for you. You can use the SAS explorer to look at files in a graphics catalog.

Interaction plot
data blah; input hasCancer race test; datalines; 0 0 6.8 0 1 5.8 1 0 6.4 1 1 9.3 ; run;

proc gplot; plot test * hasCancer = race; run; quit;

symbol1 interpol = join color = black; symbol2 i = join color = red; proc gplot; title "Interaction plot for race & cancer "; plot test * hasCancer = race; run; quit;

proc format; value race_f 0 = "Black" 1 = "White"; value cancer_f 0 = "Healthy" 1 = "Sick"; run; proc gplot; format race race_f. hasCancer cancer_f.; title "Interaction plot for race & cancer "; plot test * hasCancer = race / haxis = 0 to 1; run; quit;

Graphic Size
You can set the horizontal and vertical size in inches (in), centimeters (cm) or points (pt) of your pictures by using the hsize= and vsize= graphics options. goptions hsize=4 in vsize=4 in; If you are exporting images you can also set xpixels= and ypixels=.

ODS Graphics
SAS 9.1.x introduced new automatic graphics. These graphical procedures are still considered experimental so double check them. Most of the commonly use analysis procedures now support these graphics. These include: corr, anova, glm, lifetest, logistic, mixed, phreg, reg. Before you analysis code type ods graphics on; and after type ods graphics off;

The UNIVARIATE command with the HISTOGRAM statement

Coisas

SAS Statements
Global Statements DATA STEP Statements PROC STEP Statements Outros tipos de Statement
Macro Statements, SCL statements, etc.

SAS Statements
Pode comear com um LABEL seguido por um colon : Espao em branco ignorado Termina com semicolon ;
LABEL: Statement ; title 'obprogram' ; data hi ; Hello = 'World!' ; run ; proc print ; id hello ; run ;

Global Statements
Executado entre etapas Globals dentro de etapas so executados antes da etapa Often set global parameters or options

Examples:
endsas ; filename fileref path\file ; libname libref path\file ; options pageno=1 ; page ; titlen text ; X command ;

SAS DATA STEP Statements


Declarations Executables May be restricted as to location data MYDATA ; length a $14 ; input a ; cards ; (847)-390-6660 (800)-SAS-HELP ; run;

Proc STEP Statements


Proc Step Statements available depend upon the PROC Similar to Data Step Statements
title Important Phone Numbers ; proc print data=MYDATA ; by a ; id a ; run ;

Useful System Options


OPTIONS altlog=filename altprint= fname nodate missing= OPTIONS option1=value stimer pageno=1 option2 nooption3 ; pagesize=60 ; Use the Options statement to set various System Options

Formats and Informats


Formats
Converts character or numeric data to a formatted text string $UPCASE BESTw. DOLLARw.d MMDDYY8.

Informats
Converts a formatted text string to data $QUOTE COMMA8.3 MMDDYY6.

Format & Informat Examples


Format
Informat data _null_ ; data mydata ; pi=3.141592654 ; input date mmddyy8. ; p=put(pi,best5.) ; cards; put pi= p ; 01/01/60 stop ; ; run ; run ;

output: pi=3.142

result: date=0

Funes
Used in data step programming Return character or numeric values Syntax:
function(arg1,...)

Examples
d=input(12/01/99,mm ddyy8.) y=abs(x)

TRANSFORMAO NOS DADOS


Modificao de Variveis Lidas Data Arq; Input Nome $ 1-10 Sexo $ 12 Idade 14-15 Altura 17-18 Peso 20-22 ; Peso = Peso * 0.45 ; Cards ; linha de dados Proc Print ;

Criao de Variveis Novas

Data arq ; input nome $ 1-10 sexo 12 idade 14-15 altura 17-18 peso 20-22; Pesokg = peso * 0.45 ; alturape = altura / 12 ; cards ;
linhas de dados Proc print ;

Operadores Aritmticos e Funes Para montar expresses matemticas num programa SAS, dispe-se dos operadores:
Smbolo + * / ** Operao Soma Subtrao Multiplicao Diviso Exponenciao Exemplo R = S+T U = V-X A = B*C G = H/I Y=X2 Comando SAS R = S+T U = V-X A = B*C G = H/I Y = X**2

FUNES MATEMTICAS. Exemplos: Y = SIN(X) Z = LOG(A+B) W = SQRT(A)+C

COMANDO IF
IF condio THEN comando 1 ; ELSE comando 2 ; IF Altura > 72 THEN VX = 1 ; ELSE VX = 2 ; IF Idade < 13 Then VY = 0 ; IF A >= 5 and B=0 THEN Result = 'SIM' ; ELSE Result = 'NAO' ;

OPERADORES LGICOS
Smbolo < <= > >= = -= Abreviao LT LE GT GE EQ NE Comparao menor que menor ou igual maior que maior ou igual igual a diferente de

IF Sexo EQ 'F' THEN A = 1 ; OU IF Sexo = 'F' THEN A = 1 ;

The DATA STEP Overview


Data Step a feijo com arroz da programao SAS DATA STEP compilado e depois executado DATA STEP um loop de fazer at no tem mais dados

Data Step Simples


data mydata ; input expense ; total+expense ; cards; 12.00 5.95 16.28 13.42 ; run ;

DATA STEP Compilao


Compiles SAS statements Creates an input buffer if necessary initializes the program data vector records SAS data set descriptor information

Compilao do Data Step


data mydata ; input expense ; total+expense ; cards; 12.00 5.95 16.28 13.42 ; run ;

Checks syntax & compiles Creates input buffer (200 characters) PDV :
_N_ _ERROR_ expense total

MYDATA descriptor information

DATA STEP Execuo


Counts iterations (increments _N_) Sets PDV values to missing, Exceptions:
Retained variables When reading a SAS data set

Execuo da DATA STEP


data mydata ; input expense ; total+expense ; cards; 12.00 5.95 16.28 13.42 ; run ;

Program Data Vector:


_N_ _ERROR_ expense total 1 0 . .

Data STEP Execution (II)


Checks for end of file marker Reads in data into the input buffer Parses input buffer into the Program Data Vector

Executing the DATA STEP (II)


data mydata ; input expense ; total+expense ; cards; 12.00 5.95 16.28 13.42 ; run ;

Input Buffer 12.00 Program Data Vector:


_N_ _ERROR_ expense total 1 0 12.0 .

DATA STEP Execution (III)


Additional DATA STEP statements are executed operating on the PDV The PDV is written to the SAS data set as an observation Return to the top

Executing the DATA STEP (III)


data mydata ; input expense ; total+expense ; cards; 12.00 5.95 16.28 13.42 ; run ;

Program Data Vector:


_N_ _ERROR_ expense total 1 0 12.0 12.0

Output to MYDATA
12.0 12.0

Putting it all together


data mydata ; input expense ; total+expense ; cards; 12.00 5.95 16.28 13.42 ; run ;

Program Data Vector:


_N_ _ERROR_ expense total ___ _______ _______ _____ d d R

Output to MYDATA
_N_ 1 2 3 4 expense 12.0 _______ _______ _______ total 12.0 _____ _____ _____

Data STEP Compilao


Input Buffer
Used with input from a file, cards, etc, not with SAS data sets defaults to 200 characters Holds character data for parsing into the program data vector

Data Step Compilation


Program Data Vector
Buffer for the current observation All Statements are executed on data in the Program Data Vector Written out as an observation at the end of each iteration

Data Step Compilation


DATA SET Descriptor information
Variables and data types/sizes Informats Formats Labels indexed variables Number of observations Last Modified

Execution (PDV Initialization)


SAS Resets PDV (program data vector) values to missing at the top of the DATA STEP except when
A variable is listed in a retain statement A variable is in an implied retain Data is read in from a SAS data set, e.g. a set statement is used

Execution (Reading in Data)


Data Sources
Instream Data SAS Data Sets External Files

Instream cards ; cards4 ; SAS Data Sets set olddata ; External files infile myfile...; input x...;

Execution (Reading in Data)


Flat Files
List Input Column Input Formatted Input Named Input

Column Input
input S $ 1-5 a 6-9 SKU02123

Formatted Input
input date MMDDYY8. 12/31/99

List Input
input x y z ;... 12.7 16.5 13.2

Named Input
input name=$ score= name=John score=100

Execution (Reading in Data)


Infile Statement
Sets the input file options
Missover Stopover Firstobs obs pad Delimiter

Input Statement
reads in data pointer controls
@n, +n, / #n n-n

other terms
variable names $~=? informat

Execution (Reading in Data)


Invalid Data
Is generally recorded as missing Up to 27 special missing value flags may be assigned a-z and _ e.g. missing b ; Input statements are critical Ensure sufficient space for character variables with a length statement

Execution (Programming)
Data Step Code Looks a lot like other languages Code operates on the variable values in the program data vector The data step is a loop Functions, Logic and Flow control

Execution (Output)
Keep and Drop determine which variables in the Program Data Vector are written to the data set An output ; statement writes data to the SAS data set If a data step does not contain an output statement it is implied at the end of the data step

Using Data Steps


To read in data from a flat file and prepare for processing To write formatted output to a flat file To perform custom calculations on data data NEWDATA ; ... run ; data _NULL_ ; ... put @1 ... run ; data NEWDATA ; set OLDDATA ;...

SAS Programming Tips


Comments Indentation & Spacing Avoiding the missing semicolon ; Beware the unbalanced quote Always read your log The Data Step Debugger

Comments
Three types of Comments
Comment Comment Statement Macro Comment
/* Comment */ * Comment Statement ; %* Macro Comment ;

Comments can go between tokens, but cannot be nested Comment statements must go between statements Macro comments are stripped during macro processing

Avoid Missing Semicolons


SAS statements end with a semicolon The missing semicolon is probably the most common SAS error Avoid the missing semicolon by putting a space before it: data mydata ;

Beware the unbalance quote!


Unbalanced quotes can kill you. Sometimes its just easier to start over So save your work often Use quotes consistently Double check statements with quotes string literal &macrovar or

The Data Step Debugger


data baddata /debug ; statements ; ... run ; Use the data step debugger in interactive/display manager mode Create watch variables, and step through data step code Best with small data sets

Always read your log


Always read your log Always read your log Always read your log Always read your log Always read your log Always read your log Always read your log

Libraries & Catalogs


Libraries
logical collections of SAS stuff Usually a directory Permanent and Temporary e.g. work.data libname mylib c:\path\dir\ ;

Catalogs
Catalogs contain compiled SAS stuff (entries) Catalogs are stored in libraries SAS code, Macro code, AF & SCL code, etc. Graphs, Formats

Saving code
Name the contents of the Editor window
currently blank

Select the editor window Choose FileSave as


X:\brownbag.sas

Save regularly

Consistent indentation & spacing


Properly indent and close out each data and proc step Use indentation for do loops, conditional processing, etc. Be liberal with white space The first thing I always do when Im given old legacy code is properly indent the code. Once this is done, the problem is usually self-evident.

VII. Reducing repeated code


Macros

A very brief introduction to the SAS Macro facility


The Macro facility is a tool for writing SAS code. All it does is process text Macros resolve first then code is sent to the compiler Similar to the C preprocessor

Macros
Often you need to repeat analyses
with minor adjustments

Instead of cutting and pasting


use a macro

Macros
Basic structure %macro choose_a_name (arg1=, arg2=); /* SAS code using &arg1, &arg2 */ %mend;

Macros
Dont say

Instead, say

SAS Macro Variables


&foo or &bar. Assignment %let foo=string literal Up to 64K Macro Variable Arrays &&var&i Macro Variable References &&&foo Global or Local
;

Macro Functions
Declaration
%macro mymacro(data=&data) ; macro statements ; ... %mend mymacro ;

Macro Call
%macro(mydata)

Para aprender mais


Online documentation
wks.uts.ohio-state.edu/sasdoc/8/sashtml/onldoc.htm very technical

The Little SAS Book


in SRL, great introduction & reference

Slides

J. Patrick McDonald Spikeware Don Miller Et al.