forked from friendly/SAS-macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bctrans.sas
86 lines (73 loc) · 1.44 KB
/
bctrans.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
%macro bctrans(data=,
out=bctrans,
var=,
newvar=,
add=0,
min=-2,
max=2,
step=0.2);
proc iml;
use &data;
read all var {&var} into x;
n=nrow(x);
one=j(n,1,1);
if any(x<=0)
then x = x+min(x)+.001;
lnx=log(x);
sumlog=sum(lnx);
start loglike;
free mat;
do lam = &min to &max by &step;
lambda=round(lam,.01);
if lambda = 0
then xl=log(x);
else xl=((x##lambda) - one)/lambda;
mean=xl[:];
d=xl-mean;
ss=ssq(d)/n;
l=-.5#n#log(ss)+((lambda-1)*sumlog);
mat = mat // (lambda || l);
end;
finish;
run loglike;
print "Lambdas and their l(lambda) values",
mat [format=8.3] ;
cl = {lambda loglike};
create lambdas from mat[colname=cl];
append from mat;
quit;
proc plot data=lambdas nolegend;
plot loglike*lambda;
title 'Lambda vs. loglike(lambda) values';
run;
quit;
proc sort data=lambdas;
by descending loglike;
run;
data &out;
set lambdas;
if _n_>1 then delete;
run;
proc print data=&out;
title 'Highest lambda and l(lambda) value';
run;
proc iml;
use &data;
read all var {&var} into old;
use &out;
read all var {lambda} into power;
if power=0
then new=log(old);
else new=old##power;
create final from new;
append from new;
quit;
data final;
set final;
rename col1=&var;
run;
proc univariate normal plot data=final;
title 'Normality Assessment for';
title2 'Power-Transformed Variable';
run;
%mend bctrans;