/*1*/

DATA alea;
  do i = 1 to 350;
    x1=rannor(954367);
    x2=rannor(954367)*sqrt(2)+10;
	x3=ranuni(954367);
	x4=ranuni(954367)*10;
    x5=-10+ranuni(954367)*20;
    output;
  end;
  drop i;
run;

proc print data= alea;
run;

proc means data=alea; run;

DATA alea2;
  set alea;
    y1=abs(x1);
    y2=sign(x1);
    y3=sqrt(x4);
    y4=round(x2,0.1);    
    y5=exp(x1);
    y6=max(x3,x4,x5);
    y7=min(x3,x4,x5);
    y8=mean(abs(x1),abs(x5));
drop x1-x5;
run;

proc means data=alea2; run;

/*2*/

libname w 'ruta';
libname ej 'ruta\ejercicios';

proc means data=w.fami; run;

data ej.fami2; set w.fami;
if oci+menjar+roba+habitatge>100 then c0=1;
if oci+menjar+roba+habitatge>50 then c1=1;
else c1=0;
if habitatge>oci+menjar+roba then c2=1;
else c2=0;
if habitatge<menjar+roba then c3=1;
else c3=2;
if (roba+menjar>0.70*habitatge) then c4='sup70';
else c4='inf70';
run;

proc freq data=ej.fami2; tables c0 c2 /missing; run;

data ej.fami2; set ej.fami2; 
 if habitatge>25 then habit=1;
 if oci<15 then oc=1; 
run;

proc freq data = ej.fami2; 
 tables habit * oc / missing; 
run;

/*3*/

data ej.socis2; set w.socis; 
antic=(today()-data_i)/365.25;
edat=(today()-data_n)/365.25;
if edat<40 then cuota=35;
else if (edat>=40 and edat<=64) then cuota = 1.4*35;
else if (edad>64) then cuota = 0.8*35;
if missing(edat)=1 then cuota=.p;
run;

proc means data=ej.socis2; var cuota; class genere; run;

proc means data=ej.socis2; var antic; class genere; run;

proc means data=ej.socis2 sum; var cuota; run;

/*4*/
data ej.enqasseg; set w.enqasseg; 
label X1='N?mero encuesta' 
X2='A?o de nacimiento'
X3='G?nero (1= Hombre 2 = Mujer)'
X4='Estado civil (1 = Viudo/a, 2 = Separado/Divorciado, 3 = Casado/a y 4 = Soltero/a)'
X5='Calidad del servicio de atenci?n al cliente'
X6='Calidad del servicio de tramitaci?n de siniestros'
X7='Calidad del servicio de reclamaciones';
array V(3) x5-x7;
do i=1 to 3;
  if V(i) >=6 then V(i)=.p;
end;
drop i;
run;

proc freq data=ej.enqasseg; tables x5-x7/missing; run;

proc means data=ej.enqasseg; var x5-x7; run;

data ej.enqasseg; set ej.enqasseg; vmed = mean(of x5--x7); vstdev=std(of x5--x7); run;

/*5*/


proc means data=w.siniestros sum; 
 var n_sin coste; 
run;

proc freq data=w.siniestros; 
 tables sexo*n_sin; 
run;

proc univariate data=w.siniestros; 
 var coste; 
 histogram /normal; 
run;

data sin; 
 set w.siniestros; 
 if n_sin>0 then coste1=coste/n_sin; 
run;

proc means data=sin mean var; 
 var coste1; 
 class sexo; 
run;


/*6*/

DATA ej.dat1 (DROP = COUNT x1--x100);
SET w.dat1;
ARRAY VAL(100) X1 - X100;
ARRAY YY(100) Y1 - Y100;
DO COUNT = 1 TO 100;
   YY(COUNT)=sqrt(abs(val(count)));
END;
RUN;


/*7*/


DATA ej.dat2sol (DROP = COUNT);
SET w.dat2;
ARRAY XX(100) X1 - X100;
DO COUNT = 1 TO 100;
   if (XX(COUNT)=-99) then XX(COUNT)=.;
END;
if (nmiss(of x1--x100)>53) then s=1;
else s=0;
RUN;

proc freq data=ej.dat2sol; tables s; run;