BookmarkSubscribeRSS Feed
opti_miser
Calcite | Level 5

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;

2 REPLIES 2
opti_miser
Calcite | Level 5

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;

Rick_SAS
SAS Super FREQ

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);

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Multiple Linear Regression in SAS

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 921 views
  • 3 likes
  • 2 in conversation