Documentos de Académico
Documentos de Profesional
Documentos de Cultura
1 SasBcvManejoArchivos
1 SasBcvManejoArchivos
BASICO
Giampaolo Orlandoni Josefa Ramoni
SubconjuntosdeDatos
To work with a subset of a SAS data set, you may need to select observations, variables, or both. In the DATA step, p, there is a number of tools for subsetting g data. Subsetting IF statements to select only those observations that meet a specified condition IF-THEN/ELSE constructs with DELETE and OUTPUT statements to delete or select observations that meet t a condition, diti respectively ti l DROP= and KEEP= data set options and DROP and KEEP statements to drop or keep selected variables statements, WHERE statements to select only those observations that meet a specified condition (under certain circumstances).
If Cod='ME1' then Grupo='Mecan1'; else if cod= cod='ME2' ME2 then Grupo= Grupo='Mecan2'; Mecan2 ; else if cod='PT1' or cod='PT2' then Grupo='Pintor'; else l G Grupo ='Otros'; 'Ot '
Proc print; run; Data Salario; Set Nomina(drop=sexo FNac FCnt);
if salario >60000 then Categ='C_Alto'; else if salario <30000 then Categ='C_Bajo'; else Categ='C_Media';
Proc print; run;
\2_1-SalarioInfile.sas
Ejemplo: SalarioInfile.sas
Data Nomina; Infile 'C:\DataSas\dat\salario.dat'; 'C:\DataSas\dat\salario dat'; Input IdN $ 1-4 Apellido $ 9-19 Nombre $ 20-29 Ciudad $ 30-42 Estado $ 43-44 / Sexo $ 1 Cod $ 9-11 Salario 20 29 @30 FNac date7. 20-29 date7 @43 FCnt date7 date7. Telef $ 54-65; 54 65; Format FNac FCnt date7.; /*Segmentando Conjuntos de datos. If segmenta */; ; Data SalarioOtros; Set Salario; if Grupo='Otros'; if Sexo= 'M'; M ; if salario > 20000 and salario < 60000; Proc print data=SalarioOtros; run;
2_1-SalarioInfile.sas
WHERE.Comparaciones Mltiples
Seleccin de observaciones basada en ms de una comparacin Comparaciones p compuestas: p AND: verificar dos condiciones OR: verificar al menos una condicin Ejemplos:
AND: proc print data=ventas noobs; var Vendedor Mes Cantidad Venta; Where Vendedor ='Garcia and Month=01; OR: proc print data=ventas noobs; var Vendedor Mes Cantidad Venta; Where VendedorCantidad>500 or Venta>20000;
WHERE.
Totales
de Grupos
Pueden suprimirse p los subtotales, , mediante el uso de SUMBY. Slo se admite una variable SUMBY, que debe aparecer tambin en BY. BY PROC PRINT calcula sumas cuando ocurren cambios en el valor de: variable SUMBY cualquier l i variable i bl en BY especificado ifi d antes t de la variable SUMBY
Ejemplo:
proc print data=Ventas; var Cantidad Venta; where Cantidad>500 or Venta>20000; format Cantidad comma7. Venta dollar14.2; sum Cantidad Venta; by Vendedor Mes; id Vendedor Mes; sumby Vendedor;
2_CombinarSET_1.sas
Input Nombre $ Edad Sexo $ Sueldo; Input Nombre $ Edad Sexo $ Sueldo;
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
Concatenacin de archivos
A1 NOMBRE SEXO CODIGO Data c; SET A1 A2; run; A2 NOMBRE SEXO CODIGO
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
2_CombinarSET_2,sas:
Input Nombre $ Edad Sexo $ Sueldo; Input Nombre $ Edad Sexo $;
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.
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
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 $;
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
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
terc
Arte
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;
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
data Ingreso(keep= vuelo ciudad ingreso); MERGE vuelo precios; p ; By destino; ingreso=pasajeros*precio; Run;
VUELO
PASAJERO DESTINO
CIUDAD
PRECIO
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
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