Statistical programming, matrix languages, and more

loop in iml taking a long time to run - is a more efficient method possible?

Reply
Contributor
Posts: 22

loop in iml taking a long time to run - is a more efficient method possible?

I have attached a simplified version of the code I am using in a much larger data set.  It is a loop in iml.

The code takes a long time to run as a result of the loop.  I was wondering if there is a more efficient way to achieve the same thing.

If it would be helpful, I can attach the more complex version of the code.  It is really the same thing, but for a much larger set of data.

Thanks for any help you can offer.

Message was edited by: Ted Moorman *This is the weight matrix for one month; m1={ 3 ,     6 ,     2  }; print m1; *This is the weight matrix for another month; m2={ 2 ,      3,      1 }; print m2; *These would be the permnos for weight matrix in month 1; p1={'a','b','c'}; *These would be the permnos for weight matrix in month 2; p2={'c','d','e'}; print p1; print p2; *This would be all of the permnos in the data set;  *This vector could be obtained with the unique statement; p={'a','b','c','d','e'}; print p; *NOTE: The loop is just for one month; *newweight is the new weight matrix to be created; newweight = j(nrow(p),1); do i = 1 to nrow(p);      newweight=0;      do j = 1 to nrow(m1);           if p=p1 then newweight=m1;      end; end;

Attachment
Contributor
Posts: 22

Re: loop in iml taking a long time to run - is a more efficient method possible?

This appears to be the best so far.

proc iml;

*This is the weight matrix for one month;

begw={ -8,

    6,

    13,

      19};

print begw;

*This is the weight matrix for another month;

endw={ -2,

     -3,

     1,

                     12};

print endw;

*These would be the permnos for weight matrix in month 1;

begp={3,1,2,9};

*These would be the permnos for weight matrix in month 2;

endp={3,2,4,7};

print begp;

print endp;

*This merges permnos and weights;

beg=begp||begw;

end=endp||endw;

print beg;

print end;

*This eliminates rows from beg that do not contain permnos in end;

newbeg=beg[t(loc(element(beg[,1],end[,1]))),];

*This creates a matrix of permnos in end not included in beg with zeros as weights;

addbeg=t(setdif(end[,1],beg[,1]))||j(nrow(t(setdif(end[,1],beg[,1]))),1,0);

*This creates a new beg matrix;

finalbeg=newbeg//addbeg;

call sortndx(endx, end, 1);

endsort=end[endx,];

call sortndx(begx,finalbeg, 1);

begsort=finalbeg[begx,];

print newbeg;

print addbeg;

print finalbeg;

print endsort;

print begsort;


quit;

run;

SAS Super FREQ
Posts: 3,416

Re: loop in iml taking a long time to run - is a more efficient method possible?

1) You can get rid of the transpose function T().  It is not needed for NEWBEG=... and you can use ncol(setdif(...)) instead of nrow(t(setdif(...)))

2) You never use the second column of BEG and END, so you can really do all of this with indices to rows of BEGP and ENDP

3) I haven't measured it, but I suspect it is slightly faster to call SORT rather than SORTNDX followed by using the row indices to sort. So you might want to try it:

endsort = end;

call sort(endsort, 1);

Ask a Question
Discussion stats
  • 2 replies
  • 308 views
  • 3 likes
  • 2 in conversation