Está en la página 1de 25

SAS

BASICO
Giampaolo Orlandoni Josefa Ramoni

Instituto de Estadstica Aplicada p Universidad de Los Andes Venezuela

COMBINACION DE DATA SETS


1 CONCATENACION (CONCATENATING) 1. 2. INTERCALADO ORDENADO(INTERLEAVING) 3. COMBINACION (MERGING) ( ) 4 ACTUALIZACION (UPDATING) 4.

1-CONCATENACION Data Sets


1-Concatenacin de DS. Igual Nmero de Variables
Usar SET en el Data Step Nmero total de Observaciones en el Data Set Final (DSF) es igual a la suma de observaciones en los DS que se combinan Nmero de Variables en el DSF: Igual al nmero de diferentes variables en los DS que se combinan. Ejemplo:

2_CombinarSET_1.sas
Input Nombre $ Edad Sexo $ Sueldo; Input Nombre $ Edad Sexo $ Sueldo;

1. Data Dep1; 2. Data Dep2; 3. Data Dep1_2;

Set Dep1 Dep2;


Title 'Dep1 y Dep2;

Proc Print data=Dep1_2;

Concatenacin de archivos
A1
NOMBRE
JUAN LUISA RAUL SAUL JULIA

A2
CODIGO
NA1 NA1 NA1 NA1 NA1

SEXO
M F M M F

NOMBRE
NORA RAUL

SEXO
F M

CODIGO
NA2 NA2

Data NUEVO; SET A1 A2 A2; run;


NOMBRE SEXO CODIGO

Concatenacin de archivos
A1 NOMBRE SEXO CODIGO Data c; SET A1 A2; run; A2 NOMBRE SEXO CODIGO

A1 A2 NOMBRE SEXO CODIGO

CONCATENACION Data Sets. IGUAL Nmero de Variables


1-Data Dep1
Obs 1 2 3 4 5 6 7 8 Nombre Vincente Felipe Tomas Nicolas Gisela Humberto Emily Michaela Edad 34 28 27 36 32 39 22 32 Sexo M M M M F M F F Sueldo 1000 2000 1800 3000 3500 1900 2600 2500

3-Data Dep1_2
Obs 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Nombre Vincente Felipe Tomas Nicolas Gisela Humberto Emily Michaela Martin Maria Odilia Tomas Luis Benito Francisc Edad 34 28 27 36 32 39 22 32 40 45 28 33 38 29 41 Sexo M M M M F M F F M F F M M M M Sueldo 1000 2000 1800 3000 3500 1900 2600 2500 3500 4500 2975 2750 3390 2800 3500

2-Data Dep2
Obs 1 2 3 4 5 6 7 Nombre Martin Maria Odilia Tomas Luis Benito Francisc Edad 40 45 28 33 38 29 41 Sexo M F F M M M M Sueldo 3500 4500 2975 2750 3390 2800 3500

Data Dep1_2; Set Dep1 Dep2; run;

CONCATENACION Data Sets


2-Diferente Nmero de Variables:
Nmero total de Observaciones en el Data Set Final (DSF) es igual a la suma de observaciones en los DS que se combinan Nmero de Variables en el DSF: Igual al nmero de diferentes variables en los DS que se combinan. Ejemplo:

2_CombinarSET_2,sas:
Input Nombre $ Edad Sexo $ Sueldo; Input Nombre $ Edad Sexo $;

1. Data Dep1; 2. Data Dep2; 3. Data Dep1_2;

Set Dep1 Dep2;

El DS contiene todas las variables: Nombre$ Edad Sexo$ Sueldo Se generan datos faltantes para las observaciones que no tienen informacin de alguna variable.

Las variables deben tener los mismos atributos (mismo tipo). De lo contrario la concatenacin no se realiza y el programa produce un error. Ejemplo: j p Edad numrica en Dep1 p Edad alfanumrica en Dep2

ERROR:LavariableEdadsehadefinidocomoalfanumricaycomonumrica.

***Combine Combinetablesbyrows(ConcatenatetablesbyAppendmethod)***; method) ; DataCOMBINAD0; lengthNOMBRE$10EDAD8SEXO$8SUELDO8; setWork.Dep1 Work.Dep2; keepNOMBREEDADSEXOSUELDO; run;

CONCATENACION Data Sets. DISTINTO NUMERO DE VARIABLES


1-Data Dep1
Ob Obs 1 2 3 4 5 6 7 8 Nombre b Vincente Felipe Tomas Nicolas Gisela Humberto Emily Michaela Edad d d 34 28 27 36 32 39 22 32 S Sexo M M M M F M F F S ld Sueldo 1000 2000 1800 3000 3500 1900 2600 2500

3-Data Dep1_2
Obs 1 2 3 4 5 6 7 8

Nombre
Vincente Felipe Tomas Nicolas Gisela Humberto Emily Michaela Martin Maria Odilia Tomas Luis Benito Francisco

Edad
34 28 27 36 32 39 22 32 40 45 28 33 38 29 41

Sexo
M M M M F M F F M F F M M M M

Sueldo
1000 2000 1800 3000 3500 1900 2600 2500 . . . . . . .

2-Data Dep2
Obs 1 2 3 4 5 6 7 Nombre Martin Maria Odilia Tomas Luis Benito Francisc Edad 40 45 28 33 38 29 41 Sexo M F F M M M M

9 10 11 12 13 14 15

Data Dep1_2; Set Dep1 Dep2; run;

2-INTERCALADO (INTERLEAVE) Data Sets


Los DS deben estar ordenados por la misma variable especificada en el BY que acompaa al comando SET procsortdata=Work.Dep1out=WORK.TABLA1; by SEXO;run; procsortdata=Work.Dep2out=WORK.TABLA2; by SEXO;run; dataCOMBINA; length NOMBRE$10EDAD8SEXO$8SUELDO8; setWORK.TABLA1WORK.TABLA2; by SEXO; keep NOMBREEDADSEXOSUELDO;run;

2-INTERCALADO (INTERLEAVE) Data Sets

2-INTERCALADO (INTERLEAVE) Data Sets

2-INTERCALADO (INTERLEAVE) Data Sets

3-MERGE Data Sets


Combina observaciones de dos o ms DS en una sola observacin b i y en un nuevo DS. DS El nuevo DS contiene todas las variables de los DS originales

1-Merge One-to-one: 2MergeOne.sas


No se usa el BY. Observaciones se combinan segn su posicin en el DS input. El nmero de observaciones en el nuevo DS es igual al n_maximo =max(n1,n2) Ejemplo: 1. 2 2. 3. DataClase; DataHorario; DataCronograma; InputNombre$125An$2630Espec $3350; InputFechadate9. date9 @12Hora$@19Sala$; Merge ClaseHorario;

MERGE One-to-One. 2MergeOne.sas


Ejemplo: asignacin estudiantes de horarios de sala de conferencias a

Nombre: nombre estudiante. An: ao estudio: 1 1, 2 2, 3 3, 4 4 Especialidad: area de especializacin (valor faltante para 1 y 2 aos).

Programa: El siguiente programa ejecuta un Merge One-toOne de los DS, asignando un horario a cada estudiante de la clase en el orden de los datos
data Clase; Input Name $ 1-25 Year $ 26-34 Spec $ 36-50; data Horario; Input Date date9. @12 Time $ @19 Room $;

data cronograma; merge clase horario;

Merge One-to-One
Clase
Obs 1 2 3 4 5 6 Nombre Azuaje, Juan Carter, Tomas Perez, Elisa Tamayo, Raquel Uzon, Rolando Wiky, Mauricio An prim terc cuar prim segn terc Arte 3 4 Arte Matematica 1 2 Azuaje, Juan Carter, Tomas Perez, Elisa Tamayo, Raquel Uzon, Rolando Wiky, Mauricio prim terc cuar prim Arte 14SEP2010 10:00 103 14SEP2010 10:30 103 Espec

Cronograma = Clase + Horario


Obs Nombre An Espec Fecha Hora Sala

Matematica 14SEP2010 11:00 207 15SEP2010 10:00 105

Horario
Obs 1 2 3 4 5 6 Fecha 14SEP2010 14SEP2010 14SEP2010 15SEP2010 15SEP2010 17SEP2010 Hora 10 00 10:00 10:30 11:00 10:00 10:30 11:00 Sala 103 103 207 105 105 207

segn

15SEP2010 10:30 105

terc

Arte

17SEP2010 11:00 207

2-Merge Match: 2MergeMatch.sas


Se usa BY p para combinar observaciones p provenientes de los DS input data, basados en en valores comunes de la variable por la que se unen (merge) los DS.
MERGE SAS-data-set-list; BY variable-list;

Ejemplo: 1-Data Dep1; Sueldo; 2-Data Dep2; Input Nombre $ 1-10 Edad Sexo $

proc sort data=Dep1 out=WORK.Tabla1; proc sort data=Dep2 out=WORK.Tabla2; 3-Data Dep1_2_Merge1;

by SEXO; by SEXO;

Input Nombre $ 1-10 Edad Sexo $ Sueldo;

Merge WORK.Tabla1 WORK.Tabla2; By Sexo;

2-Merge Match: 2MergeMatch.sas

MERGE Match Data Sets


Una compaa mantiene dos Sas DS: COMPANY y FINANZA.
Data Set Variable

Nombre COMPAA Edad Sexo Nombre FINANZA Id Sueldo

1-Data Company; Input Nom $ 1-25 Edad 27-28 Sexo $ 30; proc sort data=company; by Nom; 2-Data Finanza; Input IdNum $ 1-11 Nom $ 13-40 Sueldo; proc sort data=finanza; by Nom; 3-Data CompaFina; Merge company finanza; by nom;

Combinacin de archivos
VUELO
VUELO
921 982 114 431

PRECIOS
DESTINO
DFW DFW LAX LAX

PASAJERO
169 120 185 103

DESTINO
DFW FRA LAX LON

CIUDAD

PRECIO
600 1200 900 900

Dallas Frankfurt Los Angeles London

data Ingreso(keep= vuelo ciudad ingreso); MERGE vuelo precios; p ; By destino; ingreso=pasajeros*precio; Run;

VUELO

PASAJERO DESTINO

CIUDAD

PRECIO

4-UPDATING Data Sets


Reemplaza valores de variables del MasterDS valores no faltantes del TransactionDS. Se trabaja con dos DS: Master DS: conjunto j original g de datos Transaction DS: contiene la informacin nueva que va a reemplazar la informacin antigua contenida en e e el Master. aste Foma general: Update masterDS transactionDS; con

BY variable identificadora; Update slo puede manejar dos DS El comando d BY indica i di l las variables i bl match t h Ambos DS deben estar ordenados por variables match especificadas en BY. las mismas

4-UPDATING Data Sets


Ejemplo

Programa SAS: \2_Update1.sas Salida: \2_Update1.html

Statement/ Procedure

Action Performed

BY

Controls the operation of a SET, MERGE, UPDATE, or MODIFY statement in the DATA step and sets up special grouping variables. BY-group processing is a means of processing observations that have the same values of one or more variables. Reads observations from two or more SAS DS and joins them into a single observation. When using MERGE with BY, the data must be sorted or indexed on the BY variable. Processes observations in a SAS data set Sorted or indexed data are not required for use with BY, but are recommended for performance in place place. (Contrast with UPDATE.) Reads observations from one or more SAS data sets. Applies transactions to observations in a master SAS data set. UPDATE does not update observations in place; it produces an updated copy of the current data set. Both the master and transaction DS must be sorted by y or indexed on the BY variable. Adds observations from one SAS DS to the end of another SAS DS

MERGE

MODIFY

SET

UPDATE

PROC APPEND

También podría gustarte