I dont understand what is going on in my SAS code. The code behaves as expected, but only during the first time it compiles, i.e. the table called 'SummaryTable' shows the correct 'LogLik' value for each of the 3 iterations where the code triggers %mylogreg and %ModelsSummary. However, if I re-run the code then it behaves differently, the 'LogLik' value for the 3 iterations is the same and it is equal to the value for the last iteration. So, in some way the variable 'MyLogLik' saved the value of the third iteration during the first time the code is compiled.
Fist data generation step:
data ingots;
input heat soak r n @@;
datalines;
7 1.0 0 10 7 1.7 0 17 7 2.2 0 7 7 2.8 0 12 7 4.0 0 9
14 1.0 0 31 14 1.7 0 43 14 2.2 2 33 14 2.8 0 31 14 4.0 0 19
27 1.0 1 56 27 1.7 4 44 27 2.2 0 21 27 2.8 1 22 27 4.0 1 16
51 1.0 3 13 51 1.7 0 1 51 2.2 0 1 51 2.8 0 1
;
run;
Then the macros:
%macro mylogreg(Num,param);
title "variables are ¶m";
proc logistic data=ingots des outest = parameters&Num noprint;
model r/n = ¶m;
run;
%mend;
%macro ModelsSummary(Count,Var,Param);
proc sql;
select _LNLIKE_ into:MyLogLik from parameters&Count;
insert into SummaryTable (ModelNumber,ModelVariables,NumParameters, LogLik) values (&Count,"&Var",&Param, &MyLogLik);
drop table parameters&Count;
quit;
%mend;
Then my code:
proc sql;
create table SummaryTable(
ModelNumber num,
ModelVariables varchar (500),
NumParameters num,
LogLik num
)
;
quit;
data _NULL_;
array a[2] $ (' heat' ' soak');
length temp $500;
/*Number of Variables*/
n = dim(a);
/*k tell us the number of variables in aech group of all possible conbination*/
do k=1 to n;
/*total tells the number of different convinations given that we form groups of k variables*/
total = comb(n,k);
do j=1 to total;
/*allcomb is the SAS function which forms all different combinations*/
call allcomb(j,k,of a[*]);
/*This counter show the total number of convinations for all ks*/
Counter + 1;
do i = 1 to k;
if i = 1 then temp = '';
/*this temp string contains the explanatory variables to be passed to the logistic reg*/
temp=catt(temp,a[i]);
end;
/* NumParam shows the number of parameters in the logistic model, including the intercept*/
NumParam = k + 1;
/* First we call the logistic regression macro, and the we fill out the table summarizing the important results*/
call execute('%mylogreg('||Counter||','||temp||')');
call execute('%ModelsSummary('||Counter||','||temp||','||NumParam||')');
end;
end;
run;
Then the problem is that if I re-run the code, then the values for 'LogLik' in 'SummaryTable' will all be the same, and the value corresponds to the third model. As I said it before, the first time i run the code, the loop works as expected and each model has its corresponding 'LogLik' value in 'SummaryTable'. I have checked that %ModelSummary is getting passed the correct values for the variable 'Counter' (this can also be confirmed in the SQL output), and it seems correct. So, I beleived that what I need is to clean the value of 'MyLogLik' after the main code is finished or something like that.
Can someone point me out what I am missing? I dont see what is wrong in my code!