trabajando con arreglos, tips & tricks · que van resultando en códigos más complejos. ......

12
Copyright © 2011, SAS Institute Inc. All rights reserved. make connections • share ideas • be inspired Trabajando con arreglos, Tips & tricks

Upload: votruc

Post on 15-Oct-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Copyright © 2011, SAS Institute Inc. All rights reserved.

make connections • share ideas • be inspired

Trabajando con arreglos, Tips & tricks

Copyright © 2011, SAS Institute Inc. All rights reserved.

Introducción

A menudo nos encontramos con la idea de

trabajar con arreglos, pero al iniciar con dicha

tarea la complejidad y desconocimiento del tema

nos ha hecho desistir, tomando otros caminos

que van resultando en códigos más complejos.

Copyright © 2011, SAS Institute Inc. All rights reserved.

¿Por qué necesitamos usar los arreglos?

Simplificar el proceso

Leer y analizar datos repetitivos

Combinar arreglos y un ciclos

Trabajar con cientos de elementos

Asignar al arreglo variables consecutivas.

Hacer búsquedas de la tabla.

Copyright © 2011, SAS Institute Inc. All rights reserved.

Conceptos Básicos

Los Arreglos en SAS son diferentes que en otros lenguajes

Un arreglo en SAS es un grupo de variables temporales

La declaración ARRAY define las variables a ser procesados como un grupo.

Las variables de referencia de la matriz se denominan elementos

Los elementos del arreglo no pueden ser utilizados con KEEP o DROP.

Debe ser definido en un Paso Data y al inicio de este.

Sintaxis:

array nombre-arreglo {n} <$> <longitud> elementos-arreglo <(valor-Inicial)>;

Copyright © 2011, SAS Institute Inc. All rights reserved.

Unidimensional:

array temperature_array {24} temp1 – temp24;

Multidimensional

array sale_array {3, 12} sales1-sales12 exp1-exp12 comm1-comm12;

Temporales

array rate {6} _temporary_ (0.05 0.08 0.12 0.20 0.27 0.35);

Ejemplos

Copyright © 2011, SAS Institute Inc. All rights reserved.

Ejemplo en uso:

if month_delinquent eq 1 then balance = balance + (balance * 0.05);

else if month_delinquent eq 2 then balance = balance + (balance * 0.08);

else if month_delinquent eq 3 then balance = balance + (balance * 0.12);

else if month_delinquent eq 4 then balance = balance + (balance * 0.20);

else if month_delinquent eq 5 then balance = balance + (balance * 0.27);

else if month_delinquent eq 6 then balance = balance + (balance * 0.35);

array rate {6} _temporary_ (0.05 0.08 0.12 0.20 0.27 0.35);

if month_delinquent ge 1 and month_delinquent le 6 then

balance = balance + (balance * rate{month_delinquent});

Copyright © 2011, SAS Institute Inc. All rights reserved.

Algunos trucos

Ordenando Arreglos (sortn y sortq)

Xarry {1} {2} {3} {4} {5} {6}

Variables x1 x2 x3 x4 x5 x6

Values 0.27 0.12 0.20 0.08 0.35 0.05

data _null_;

array xarry{6} x1-x6;

set ds1;

call sortn(of x1-x6);

run;

Xarry {1} {2} {3} {4} {5} {6}

Variables x1 x2 x3 x4 x5 x6

Values 0.05 0.08 0.12 0.20 0.27 0.35

Copyright © 2011, SAS Institute Inc. All rights reserved.

Arreglo Implícito (*)

Con DIM() puedes conocer la dimensión de los arreglos.

Puedes usar Índices con tu arreglo

array temperature_array {6:18} temp6 – temp18;

Uso de Loop array item(*) $ 12 x1-x12;

do over item;

put item;

end;

Copyright © 2011, SAS Institute Inc. All rights reserved.

Errores comunes

Un rango invalido en el indice data dailytemp;

set tempdata;

array temperature_array {24} temp1-temp24;

array celsius_array {24} celsius_temp1-celsius_temp24;

do until (i gt 24);

i = 1;

celsius_array{i} = 5 / 9 * (temperature_array{i} – 31);

end;

i=0;

drop i;

run;

Nombre de una funcion declarado como arreglo 10 array mean {24} temp1-temp24;

WARNING: An array is being defined with the same name as a SAS-supplied or userdefined

function. Parenthesized references involving this name will be treated as

array references and not function references.

Copyright © 2011, SAS Institute Inc. All rights reserved.

Arreglo referenciado en multiples pasos data, pero definidos en uno solo.

data dailytemp;

set tempdata;

array temperature_array {24} temp1-temp24;

array celsius_array {24} celsius_temp1-celsius_temp24;

do i = 1 to 24;

celsius_array{i} = 5 / 9 * (temperature_array{i} – 31);

end;

run;

data celsius;

set dailytemp;

do i = 1 to 24;

if celsius_array{i} lt 0 then tempdesc = ‘below freezing’;

else if celsius_array{i} gt o then tempdesc = ‘above freezing’;

end;

run;

Copyright © 2011, SAS Institute Inc. All rights reserved.

Copyright © 2010, SAS Institute Inc. All rights reserved.

make connections • share ideas • be inspired

¡¡Gracias!!