Can anyone tell me what the following line of code does:
X1=X;
do i=1 to 24;
do k=1 to 4;
X[,indices[i,k]+1]=shape(0,nrow(X1),1);
end;
end;
X is a 200 row by 5 column matrix of 1's and 0's.
indices is a 24 row by 4 column matrix with the numbers 1 to 4 in different order for each row.
This is the entire code that someone wrote. It's just the shape function that doesn't make sense to me.
%macro averageAF (out, riskfactors, data);
proc logistic data=&data descending outdesign=_design_ outest=_model_ noprint;
model &out = &riskfactors;
run;
%let vars=1;
%let names=;
%do %while (%Qscan(&riskfactors,&vars) ne );
%let names=&&names %Qscan(&riskfactors,&vars);
%let vars=%eval(&vars+1);
%end;
%let vars=%eval(&vars-1);
%put &names;
data _design_;
set _design_;
if nmiss(of &riskfactors)=0 and nmiss(of &out)=0;
run;
data _indices_;
drop i perms;
array x (&vars) (1:&vars);
perms=fact(&vars);
/*vars=4 and perms=24*/
do i=1 to perms;
call allperm(i, of x(*));
output;
end;
run;
proc iml;
use _design_;
read all var {intercept &riskfactors} into X;
close _design_;
use _model_;
read var {intercept &riskfactors} into beta;
close _model_;
use _indices_;
read all var _num_ into indices;
close _indices_;
start pp(pcases,X,beta);
pcases=sum(1/(1+exp(-X*beta`)));
finish;
run pp(pcases,X,beta);
print (pcases);
pred_cases_m=shape(.,nrow(indices),&vars); /*24 rows , 4 columns*/
prev_cases_m=shape(.,nrow(indices),&vars); /*24 rows , 4 columns*/
X1=X;
do i=1 to nrow(indices); /*24*/
do k=1 to &vars; /*4*/
print (indices[i,k]);
X[,indices[i,k]+1]=shape(0,nrow(X1),1);
pred_cases_m[i,k]=sum(1/(1+exp(-X*beta`)));
print (pred_cases_m[i,k]);
end;
X=X1;
end;
The code does not make much sense to me. The SHAPE function is being used to generate a vector of zeros and then that vector is being copied repeateadly into columns 2 to 5 of X. I believe the one line
X [ , 2:5] = 0;
would have the same effect as the double loop. But you need to test this out - try printing the matrix X before and after.
I notice that the line "X = X1" is missing from the first code example that you posted. With this line the code makes more sense as X is reset after each iteration of the outermost loop. The result is that the columns of X are set to zero in every possible order.
Has your question been resolved? If so, please mark an answer as correct and close this thread. If not, what questions remain and how can we help?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.