2-SAS Combinacion Archivos

July 22, 2017 | Autor: Giampaolo Orlandoni | Categoría: Statistics
Share Embed


Descripción

SAS BASICO Giampaolo Orlandoni Josefa Ramoni

Instituto de Estadística 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-Concatenación de DS. Igual Número de Variables •Usar SET en el Data Step •Número Número total de Observaciones en el Data Set Final (DSF) es igual a la suma de observaciones en los DS que se combinan •Número de Variables en el DSF: Igual al número de diferentes variables en los DS que se combinan. •Ejemplo:

2_CombinarSET_1.sas

1. Data Dep1; 2. Data Dep2;

Input Nombre $ Edad Sexo $ Sueldo; Input Nombre $ Edad Sexo $ Sueldo;

3. Data Dep1_2;

Set Dep1 Dep2;

Proc Print data=Dep1_2;

Title 'Dep1 y Dep2‘;

Concatenación de archivos A1 NOMBRE JUAN LUISA RAUL SAUL JULIA

A2

SEXO

CODIGO

M F M M F

NA1 NA1 NA1 NA1 NA1

NOMBRE

SEXO

CODIGO

F M

NA2 NA2

NORA RAUL

Data NUEVO; SET A1 A2 A2; run; NOMBRE

SEXO

CODIGO

Concatenación de archivos A1

A2 NOMBRE SEXO CODIGO

NOMBRE SEXO CODIGO Data c; SET A1 A2; run;

A1 A2 NOMBRE

SEXO

CODIGO

CONCATENACION Data Sets. IGUAL Número de Variables 1-Data Dep1 Obs

Nombre

Edad

Sexo

Sueldo

1

Vincente

34

M

1000

2

Felipe

28

M

2000

3

Tomas

27

M

1800

4

Nicolas

36

M

3000

5

Gisela

32

F

6

Humberto

39

7

Emily

8

Michaela

3-Data Dep1_2 Obs

Nombre

Edad

Sexo

Sueldo

1

Vincente

34

M

1000

2

Felipe

28

M

2000

3

Tomas

27

M

1800

3500

4

Nicolas

36

M

3000

M

1900

5

Gisela

32

F

3500

22

F

2600

6

Humberto

39

M

1900

32

F

2500

7

Emily

22

F

2600

8

Michaela

32

F

2500

9

Martin

40

M

3500

10

Maria

45

F

4500

11

Odilia

28

F

2975

2-Data Dep2 Obs

Nombre

Edad

Sexo

Sueldo

1

Martin

40

M

3500

12

Tomas

33

M

2750

2

Maria

45

F

4500

13

Luis

38

M

3390

3

Odilia

28

F

2975

14

Benito

29

M

2800

4

Tomas

33

M

2750

15

Francisc

41

M

3500

5

Luis

38

M

3390

6

Benito

29

M

2800

7

Francisc

41

M

3500

Data Dep1_2; Set Dep1 Dep2; run;

CONCATENACION Data Sets 2-Diferente Número de Variables: •Número total de Observaciones en el Data Set Final (DSF) es igual a la suma de observaciones en los DS que se combinan •Número

de

Variables

en

el

DSF:

Igual

al

número

de

diferentes variables en los DS que se combinan. •Ejemplo:

2_CombinarSET_2,sas:

1. Data Dep1; 2. Data Dep2;

Input Nombre $ Edad Sexo $ Sueldo; Input Nombre $ Edad Sexo $;

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 información de alguna variable.

Las variables deben tener los mismos atributos (mismo tipo). De lo contrario la concatenación no se realiza y el programa produce un error. Ejemplo: j p

Edad numérica en Dep1 p Edad alfanumérica en Dep2

ERROR: La variable Edad se ha definido como alfanumérica y como numérica. y

***Combine Combine tables by rows (Concatenate tables by Append method) tables by rows (Concatenate tables by Append method)***;; Data COMBINAD0; length NOMBRE $ 10 EDAD 8 SEXO $ 8 SUELDO 8; length NOMBRE $ 10 EDAD 8 SEXO $ 8 SUELDO 8; set Work.Dep1 Work.Dep2  ; keep NOMBRE EDAD SEXO SUELDO; keep NOMBRE  EDAD  SEXO  SUELDO; run;

CONCATENACION Data Sets. DISTINTO NUMERO DE VARIABLES 1-Data Dep1 Ob Obs

Nombre b

Edad d d

S Sexo

S ld Sueldo

1

Vincente

34

M

1000

2

Felipe

28

M

2000

3

Tomas

27

M

4

Nicolas

36

5

Gisela

6

3-Data Dep1_2 Obs

Nombre

Edad

Sexo

Sueldo

1800

1

Vincente

34

M

1000

M

3000

2

Felipe

28

M

2000

32

F

3500

3

Tomas

27

M

1800

Humberto

39

M

1900

4

Nicolas

36

M

3000

5

Gisela

32

F

3500

7

Emily

22

F

2600

6

Humberto

39

M

1900

8

Michaela

32

F

2500

7

Emily

22

F

2600

8

Michaela

32

F

2500

9

Martin

40

M

.

2-Data Dep2 Obs

Nombre

Edad

Sexo

10

Maria

45

F

.

1

Martin

40

M

11

Odilia

28

F

.

2

Maria

45

F

12

Tomas

33

M

.

13

Luis

38

M

.

3

Odilia

28

F

14

Benito

29

M

.

4

Tomas

33

M

15

Francisco

41

M

.

5

Luis

38

M

6

Benito

29

M

7

Francisc

41

M

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 acompaña al comando SET proc sort data=Work.Dep1 out=WORK.TABLA1; by SEXO;  run; SEXO; run; proc sort data=Work.Dep2 out=WORK.TABLA2; by SEXO;   run; SEXO; run; data   COMBINA; length NOMBRE $ 10 EDAD 8 SEXO $ 8 SUELDO 8; set  WORK.TABLA1      WORK.TABLA2 ; by SEXO; keep NOMBRE EDAD SEXO SUELDO;  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 más DS en una sola observación 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 según su posición en el DS input. •El número de observaciones en el nuevo DS es igual al n_maximo =max(n1,n2) •Ejemplo: 1. 2 2. 3.

Data Clase;  Data Horario; Data Horario; Data Cronograma; 

Input Nombre $ 1‐25 An $ 26‐30 Espec $ 33‐50; Input Fecha date9 @12 Hora $ @19 Sala $; Input Fecha date9.  @12 Hora $  @19 Sala $; Merge Clase Horario;

MERGE One-to-One. 2MergeOne.sas Ejemplo: asignación estudiantes

de

horarios

de

sala

de

conferencias

a

•Nombre: nombre estudiante. •An: An: año estudio: 1 1°, 2 2°, 3 3°, 4 4° •Especialidad: area de especialización (valor faltante para 1° y 2° años).

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

Nombre

An

Espec

1

Azuaje, Juan

prim

2

Carter, Tomas

terc

Arte

3

Perez, Elisa

cuar

Matematica

4

Tamayo, Raquel

prim

5

Uzon, Rolando

segn

6

Wiky, Mauricio

terc

Arte

Horario Obs

Fecha

Hora

Sala

1

14SEP2010

10 00 10:00

103

2

14SEP2010

10:30

103

3

14SEP2010

11:00

207

4

15SEP2010

10:00

105

5

15SEP2010

10:30

105

6

17SEP2010

11:00

207

Cronograma = Clase + Horario Obs Nombre

An

Espec

Fecha

Hora

Sala

1

Azuaje, Juan

prim

2

Carter, Tomas

terc

Arte

3

Perez, Elisa

cuar

Matematica 14SEP2010 11:00 207

4

Tamayo, Raquel

prim

15SEP2010 10:00 105

5

Uzon, Rolando

segn

15SEP2010 10:30 105

6

Wiky, Mauricio

terc

14SEP2010 10:00 103

Arte

14SEP2010 10:30 103

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;

Input Nombre $ 1-10

Edad

proc sort data=Dep1 out=WORK.Tabla1; 2-Data Dep2;

Sexo $

by SEXO;

Input Nombre $ 1-10 Edad Sexo $ Sueldo;

proc sort data=Dep2 out=WORK.Tabla2;

by SEXO;

3-Data Dep1_2_Merge1; Merge WORK.Tabla1 WORK.Tabla2; By Sexo;

2-Merge Match: 2MergeMatch.sas

MERGE Match Data Sets Una compañía mantiene dos Sas DS: COMPANY y FINANZA. Data Set

Variable

Nombre COMPAÑÍA

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;

Combinación de archivos PRECIOS

VUELO VUELO

PASAJERO

921 982 114 431

DESTINO

169 120 185 103

DFW DFW LAX LAX

DESTINO DFW FRA LAX LON

CIUDAD

PRECIO

Dallas Frankfurt Los Angeles London

600 1200 900 900

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.

con

•Se trabaja con dos DS: •Master DS: conjunto j original g de datos •Transaction DS: contiene la información nueva que va a reemplazar la información antigua contenida en e e el Master. aste Foma general: Update masterDS

transactionDS;

BY variable identificadora; •Update sólo puede manejar dos DS •El 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

BY

MERGE

Action Performed

•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

MODIFY

SET

UPDATE

PROC APPEND

•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

Lihat lebih banyak...

Comentarios

Copyright © 2017 DATOSPDF Inc.