BookmarkSubscribeRSS Feed
Bal23
Lapis Lazuli | Level 10

I need to write a funtion or module that creates a row vector called doit of dimmesion k(k-1)/2 that contains the pairwise differences between the k leverl means. I need nested do loops

doit[i,j]= Yi0 hat- Yjo hat

 

below is my code

a=3;   # k level  (maybe I can replace it with ncol (mymatrix)?)

doit={};

do i= 1 to a;

do j = i to a;

dime = group_mean[i]-groupmean[j];

doit= doit||dime;

end;

end;

but it does not work. ANy other suggestions to make it work? Any other solutions

2 REPLIES 2
IanWakeling
Barite | Level 11

I am not sure what you mean by the line of code that starts doit[i,j]=...   however the code below that can be made to work.  The problems are:

  • the do loops allow i to be equal to j - you probably do not want the zeros that will result from this.
  • either group_mean or groupmean is a typo.

If you still cannot make it work, then please show us the messages that you are getting in the log.

 

In general it is a bad habit to grow matrices by concatenation inside a loop - this is very inefficient in terms of memory use.  For a=3 you will probably not notice, but similar code with a=1000 would grind to a halt.

Rick_SAS
SAS Super FREQ

For a more on Ian's comment about concatenating inside a DO loop (as well as an alternative approach), see the article "Friends don't let friends concatenate results inside a loop."

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 1131 views
  • 0 likes
  • 3 in conversation