lunes, 20 de marzo de 2017

Analizar código SAS desde R


Interesante entrada sobre como analizar código SAS desde R:

Librería sasmap



En el post se explica como analizar código SAS desde R, obteniendo los procedimientos utilizados, pasos data, llamadas a macro y obteniendo incluso un flujo del proceso con la librería sasmap de Mango Solutions.

.......................................................................................................................................................

Interesting post that explain how to analyze SAS code from SAS:

sasmap library


The sasmap library obtain information such as procs used, data steps, call to macros, also yo can obtain a workflow of the SAS code. sasmap is a library of Mango Solutions

martes, 7 de marzo de 2017

Examen ejemplo certificación SAS "Base Programmer" (II)

Continuando el post anterior ejemplo examen certificación SAS BASE, añadimoos otro ejemplo de examen del certificado: 'SAS Certified Base Programmer for SAS 9''





As a continuation of the previous post: Example exam of SAS BASE certification, we add another example of exam of the certification:  'SAS Certified Base Programmer for SAS 9''

Examen / Exam:

Question 1:

data test;
set sasuser.employees;
if 2 le years_service le 10 then
amount = 1000;
else if years_service gt 10 then
amount = 2000;
else
amount = 0;
amount_per_year = years_service / amount;
run;

Which one of the following values does the variable AMOUNT_PER_YEAR contain if an employee has been with the company for one year?
A. 0
B. 1000
C. 2000
D. . (missing numeric value)

Answer : D (missing). It returns missing value as amount will be 0.
SAS Log: NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to missing values.

Question 2:

The contents of the raw data file NAMENUM are listed below:
--------10-------20-------30
Joe xx

The following SAS program is submitted:
data test;
infile 'namenum';
input name $ number;
run;

Which one of the following is the value of the NUMBER variable?
A. xx
B. Joe
C. . (missing numeric value)
D. The value can not be determined as the program fails to execute due to errors.
Answer : C. It is because number is defined as a numeric variable so it is expecting a numeric value but it reads xx, so number will be a missing value.

Question 3:

How many of the following variable names will not produce errors in an assignment statement?

variable
var
1variable
var1
#var
_variable#

A. 0
B. 1
C. 3
D. 6

Answer : C ; variable var var1. A variable cannot start with numeric or special characters except _. You also cannot use special characters anywhere in the name either though numeric values are allowed.

Question4:

Suppose the variable 'Unit_Cost_Price' (numeric) contains both missing and non missing values. What would the following code return?

proc sort data=ecsql1.price_list;
 by Unit_Cost_Price;
run;

A. A new dataset work.price_list is created with Unit_Cost_Price sorted in ascending order with missing values at the bottom of the dataset
B. The dataset ecsql1.price_list is sorted with Unit_Cost_Price sorted in descending order with missing values at the bottom of the dataset
C. A new dataset work.price_list is created with Unit_Cost_Price sorted in descending order with missing values at the top of the dataset
D. The dataset ecsql1.price_list is sorted with Unit_Cost_Price sorted in ascending order with missing values at the top of the dataset

Answer : D. It is because missing values are considered as lowest values (ascending order; they will be top of the data set)

Question 5:

 The following SAS program is submitted:

   dta work.il_corn;
    set corn.state_data;
    if state = 'Illinois';
   run;
The keyword "data" is misspelled above. What happens to this program during the compilation phase assuming "corn" is a valid libref?


A. The program fails due to syntax errors
B. The DATA step compiles but doesn't execute
C. The DATA step compiles and executes 
D. None of the above

Answer : C. It compiles and executes as SAS assumed that the 'dta' was data. But it leaves a warning in log window.

Question 6

Which of the following is a valid statement about the VALUE range in the PROC FORMAT procedure? It cannot be...

A. A single character or numeric value
B. A range of character values
C. A list of unique values separated by commas
D. A combination of character and numeric values
Answer : D.

Question 7

The following SAS program is submitted:

libname sasdata 'SAS-data-library';
data test;
set sasdata.chemists (keep = job_code);
if job_code = 'chem3'
then description = 'Senior Chemist';
run;

The variable JOB_CODE is a character variable with a length of 6 bytes.

Which one of the following is the length of the variable DESCRIPTION in the output data set?
A. 6 bytes
B. 8 bytes
C. 14 bytes
D. 200 bytes

Answer : C. The length of 'Senior Chemist' is 14.

Question 8

The following SAS program is submitted:

data work.totalsales (keep = monthsales{12} );
set work.monthlysales (keep = year product sales);
array monthsales {12} ;
do i=1 to 12;
monthsales{i} = sales;
end;
run;

The data set named WORK.MONTHLYSALES has one observation per month for each of five years for a total of 60 observations.

Which one of the following is the result of the above program?
A. The program fails execution due to data errors.
B. The program fails execution due to syntax errors.
C. The program executes with warnings and creates the WORK.TOTALSALES data set.
D. The program executes without errors or warnings and creates the WORK.TOTALSALES data set.

Answer : B. The syntax issue lies in this line of code - keep = msales{12}
To correct the syntax issue, replace keep = msales{12} with keep = msales1-msales12


Question 9

The following SAS program is submitted:

data work.accounting;
set work.dept1 work.dept2;
run;

A character variable named JOBCODE exists in both the WORK.DEPT1 and WORK.DEPT2 SAS data sets. The variable JOBCODE has a length of 5 in the WORK.DEPT1 data set and a length of 7 in the WORK.DEPT2 data set. 

Which one of the following is the length of the variable JOBCODE in the output data set?

A. 5     
B. 7     
C. 8     
D. 12 

Answer : Since SAS checks the variable Job_code in DEPT1 for the first time of length of 5 Bytes. it sets the length to be 5. All the values that are read from DEPT2 are truncated to Chars.


Question 10

Which one of the following SAS statements renames two variables?

A. set work.dept1 work.dept2(rename = (jcode = jobcode) (sal = salary));
B. set work.dept1 work.dept2(rename = (jcode = jobcode sal = salary));
C. set work.dept1 work.dept2(rename = jcode = jobcode sal = salary);
D. set work.dept1 work.dept2(rename = (jcode jobcode) (sal salary));



Answer: B.  The syntax for RENAME is as follows :
RENAME=(old-name-1=new-name-1 old-name-2=new-name-2. . . old-name-n=new-name-n)



Formación SAS

miércoles, 22 de febrero de 2017

Corrección automática de nulos en tablas SAS

En la misma línea que este post anterior: detectar nulos en tablas SAS, añadimos un nuevo programa de ejemplo de SAS para automatizar la detección de nulos en una tabla SAS.


Se trata de una macro que revisa todos los campos de una tabla identificando nulos. Los nulos de variables de tipo numérico los sustituye por 0 y los de variables alfanuméricas por un carácter determinado, en este caso '-'. La macro valida por un lado las variables numéricas y por otra las alfanuméricas apoyándose en un array. Recibe como argumento el nombre de la tabla a validar (tabin) y genera una tabla nueva (tabout) que podría ser la misma. Se podría modificar para que los valores de sustitución para nulos de numéricos y alfanuméricos se recibieran como argumento. Esperemos os sea de utilidad.

-------------------------------------------------------------------------------------------------------------------------

This is a SAS macro that check all variables in a SAS dataset detecting null values. Null values of numeric variables are replaced by 0 and null values of character variables are replaced by  character '-'.  Macro validates numerics and character variables separated using two arrays to store both type of variables. The arguments of macro are the name of dataset to be checked (tabin) and a new dataset created (tabout) that it could be the same. You can modify the macro to add new arguments with the values to replace nulls of numeric and character variables. I hope it will be a usefull example.


-------------------------------------------------------------------------------------------------------------------------

Código SAS / SAS code:

%macro reemplazar_nulos(tabin, tabout);

data &tabout(drop = i);
set &tabin;
array num(*) _numeric_; /* array con todos los campos numericos */
array car(*) _character_; /* array con todos los campos alfanumericos */
do i=1 to dim(num);
if num(i)=. then num(i)=0;
end;
do i=1 to dim(car);
if car(i)=. then car(i)='-';
end;
run;

%mend;


Formación / training

sábado, 11 de febrero de 2017

Analisis RFM con SAS (video)

Adjuntamos video explicando como realizar análisis RFM (Recency, Frequency y Money) con SAS Enterprise Guide. Es un video muy práctico en el que en 14 min se explica de un modo sencillo como parametrizar y ejecutar esta tarea




miércoles, 8 de febrero de 2017

Macro para cambio automático permisos tablas SAS

Con frecuencia nos encontramos problemas de acceso a tablas SAS que se deben a los permisos con los que se crea la tabla de forma automática. Es posible que una tabla SAS generada por un usuario no pueda ser accedida por otros.

Habitualmente en un grupo de trabajo hay tablas maestras que deben ser accedidas por todo el equipo, para solucionar este problema de forma  rápida, añadimos una macro de SAS que cambia los permisos de todas las tablas de una librería.



Hay que saber la ruta física de la librería y los permisos actuales. Esto lo obtenemos con un proc contents:

proc contents data=tabsas._ALL_;
run;



Los permisos se dan utilizando el comando X y siguiendo el código rwx: Permisos rwx

%macro permisos_tablas(libreria);

PROC SQL;
create table tablas as (SELECT memname
FROM dictionary.tables WHERE libname= upcase("&libreria") );
quit;

%let dsid = %sysfunc (open(tablas));  
%let nobs = %sysfunc (attrn(&dsid, NOBS));
%if &nobs > 0 %then %do;
%do i = 1 %to &nobs;  
      %let rc = %sysfunc (fetch(&dsid));                                                                                                                                                                                                                                        
      %let tabla = %sysfunc (getvarc(&dsid,1));
      %put Cambiar permisos a tabla: &tabla;
         /* añadir los códigos con los permisos a dar y la ruta física de la librería */
X chmod 664 "/home/ruta_sas/&tabla..sas7bdat";
%end;
%end;
%let rc = %sysfunc (close(&dsid));

%mend;

%permisos_tablas(TABSAS);


Formación SAS



viernes, 3 de febrero de 2017

Examen ejemplo certificación SAS "Base Programmer" (I)

Añadimos un ejemplo de examen de la certificación: 'SAS Certified Base Programmer for SAS 9'

Información general del examen


Examen de ejemplo:

Question 1
The following program is submitted.
data WORK.TEST;
  input Name $ Age;
datalines;
John +35
;
run;
Which values are stored in the output data set?
  1. Name              Age
    ---------------------
    John               35 
  2. Name              Age
    ---------------------
    John              (missing value)
  3. Name              Age
    ---------------------
    (missing value)   (missing value)
  4. The DATA step fails execution due to data errors.
correct_answer = "A"
Question 2
Given the SAS data set WORK.ONE:
 Id  Char1
---  -----
182  M
190  N
250  O
720  P
and the SAS data set WORK.TWO:
 Id  Char2
---  -----
182  Q
623  R
720  S
The following program is submitted:
data WORK.BOTH;
   merge WORK.ONE WORK.TWO;
   by Id;
run;
What is the first observation in the SAS data set WORK.BOTH?
  1. Id  Char1  Char2
    ---  -----  -----
    182  M
    
  2. Id  Char1  Char2
    ---  -----  -----
    182         Q
    
  3. Id  Char1  Char2
    ---  -----  -----
    182  M      Q
    
  4. Id  Char1  Char2
    ---  -----  -----
    720  P      S
    
correct_answer = "C"
Question 3
Given the text file COLORS.TXT:
----+----1----+----2----+----
RED    ORANGE  YELLOW  GREEN
BLUE   INDIGO  PURPLE  VIOLET
CYAN   WHITE   FUCSIA  BLACK
GRAY   BROWN   PINK    MAGENTA
The following SAS program is submitted:
data WORK.COLORS;
  infile 'COLORS.TXT';
  input @1 Var1 $ @8 Var2 $ @;
  input @1 Var3 $ @8 Var4 $ @;
run;
What will the data set WORK.COLORS contain?
  1. Var1     Var2     Var3    Var4
    ------   ------   ------  ------
    RED      ORANGE   RED     ORANGE
    BLUE     INDIGO   BLUE    INDIGO
    CYAN     WHITE    CYAN    WHITE
    GRAY     BROWN    GRAY    BROWN
    
  2. Var1     Var2     Var3    Var4
    ------   ------   ------  ------
    RED      ORANGE   BLUE    INDIGO
    CYAN     WHITE    GRAY    BROWN
    
  3. Var1     Var2     Var3    Var4
    ------   ------   ------  ------
    RED      ORANGE   YELLOW  GREEN
    BLUE     INDIGO   PURPLE  VIOLET
    
  4. Var1     Var2     Var3    Var4
    ------   ------   ------  ------
    RED      ORANGE   YELLOW  GREEN
    BLUE     INDIGO   PURPLE  VIOLET
    CYAN     WHITE    FUCSIA  BLACK
    GRAY     BROWN    PINK    MAGENTA
    
correct_answer = "A"
Question 4
Given the SAS data set WORK.INPUT:
Var1     Var2
------   -------
A        one
A        two
B        three
C        four
A        five
The following SAS program is submitted:
data WORK.ONE WORK.TWO;
  set WORK.INPUT;
  if Var1='A' then output WORK.ONE;
  output;
run;
How many observations will be in data set WORK.ONE?

Enter your numeric answer in the space below.


correct_answer = "8"
Question 5
The following SAS program is submitted:
data WORK.LOOP;
  X = 0;
  do Index = 1 to 5  by  2;
    X = Index;
  end;
run;
Upon completion of execution, what are the values of the variables X and Index in the SAS data set named WORK.LOOP?
  1. X = 3, Index = 5
  2. X = 5, Index = 5
  3. X = 5, Index = 6
  4. X = 5, Index = 7
correct_answer = "D"
Question 6
The following SAS program is submitted:
 
proc format;
  value score  1  - 50  = 'Fail'
              51 - 100  = 'Pass';
run;
Which one of the following PRINT procedure steps correctly applies the format?
  1. proc print data = SASUSER.CLASS;
       var test;
       format test score;
    run;
    
  2. proc print data = SASUSER.CLASS;
       var test;
       format test score.;
    run;
    
  3. proc print data = SASUSER.CLASS format = score;
       var test;
    run;
    
  4. proc print data = SASUSER.CLASS format = score.;
       var test;  
    run;
    
correct_answer = "B"
Question 7
This item will ask you to provide a line of missing code;

The SAS data set WORK.INPUT contains 10 observations, and includes the numeric variable Cost.

The following SAS program is submitted to accumulate the total value of Cost for the 10 observations:
data WORK.TOTAL;
  set WORK.INPUT;
  <insert code here>
  Total=Total+Cost;
run;
Which statement correctly completes the program?
  1. keep Total;
  2. retain Total 0;
  3. Total = 0;
  4. If _N_= 1 then Total = 0;
correct_answer = "B"
Question 8
This question will ask you to provide a line of missing code.

Given the following data set WORK.SALES:
SalesID  SalesJan  FebSales  MarchAmt
-------  --------  --------  --------
W6790          50       400       350
W7693          25       100       125
W1387           .       300       250
The following SAS program is submitted:
data WORK.QTR1;
   set WORK.SALES;
   array month{3} SalesJan FebSales MarchAmt;
   <insert code here>
run;
Which statement should be inserted to produce the following output?
SalesID  SalesJan  FebSales  MarchAmt  Qtr1
-------  --------  --------  --------  ----
W6790          50       400       350   800
W7693          25       100       125   250
W1387           .       300       250   550
  1. Qtr1 = sum(of month{_ALL_});
  2. Qtr1 = month{1} + month{2} + month{3};
  3. Qtr1 = sum(of month{*});
  4. Qtr1 = sum(of month{3});
correct_answer = "C"
Question 9
Given the following SAS error log
  44   data WORK.OUTPUT;
  45     set SASHELP.CLASS;
  46     BMI=(Weight*703)/Height**2;
  47     where bmi ge 20;
  ERROR: Variable bmi is not on file SASHELP.CLASS.
  48   run;
What change to the program will correct the error?
  1. Replace the WHERE statement with an IF statement
  2. Change the ** in the BMI formula to a single *
  3. Change bmi to BMI in the WHERE statement
  4. Add a (Keep=BMI) option to the SET statement
correct_answer = "A"
Question 10
The following SAS program is submitted:
data WORK.TEMP;
  Char1='0123456789';
  Char2=substr(Char1,3,4);
run;
What is the value of Char2?
  1. 23
  2. 34
  3. 345
  4. 2345
correct_answer = "D"

Más ejemplos de examen (parte II)

Formación SAS

martes, 3 de enero de 2017

Gestionando del ciclo de vida analítico para la toma de decisiones a gran escala

Recomendamos el siguiente whitepaper de SAS: Gestionando del ciclo de vida analítico para la toma de decisiones a gran escala.  En este whitepaper encontramos recomendaciones y mejores prácticas para el desarrollo del ciclo de vida analítico. Se plantea un marco de trabajo para la explotación y mantenimiento de modelos analíticos aplicados a diferentes ámbitos: marketing de clientes, fraude, riesgos crediticios, riesgos operativos, gestión financiera, etc..



Se repasan los errores comunes en la implementación de estos modelos y se identifican las principales fases de todo proceso de análisis: preparación, exploración, modelización, implementación, evaluación, nuevas preguntas, nueva información en un proceso cíclico que se retroalimenta. Para la automatización y gestión de este ciclo se plantea un entorno analítico basado en soluciones SAS.

Se muestran casos prácticos de empresas tales como: Visa, Orlando Magic, instituciones financieras

Más información sobre las soluciones de SAS que se mencionan en: Formación SAS