BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
daradanye
Obsidian | Level 7

Hi all,

 

I need SAS to do the following thing:

 

For id 1 to n, I need to calculate dot product of line vector of id 1 and column vector of other n-1 observations to get the value.  Basically, I need to obtain the second dataset from the first dataset.

 

I guess I need to use proc iml.  But after reading examples and basic instructions, I still have no clue of how to do it.  I will appreciate it very much if someone can give me some hints or sample code.

 

 

Many thanks!

Dara

 

a.PNGb.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input id class1-class4;
cards;
1 3 2 5 0
2 4 0 1 2
3 3 2 3 3
4 1 3 0 4
;
run;
proc iml;
use have(keep=class:) nobs nobs;
read all var _num_ into x;
close;
do i=1 to nobs-1;
 do j=i+1 to nobs;
   id1=id1//i;id2=id2//j;
   value=value//sum(x[i,]#x[j,]);
 end;
end;
create want var {id1 id2 value};
append;
close;
quit;

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

@daradanye wrote:

Hi all,

 

I need SAS to do the following thing:

 

For id 1 to n, I need to calculate dot product of line vector of id 1 and column vector of other n-1 observations to get the value.  Basically, I need to obtain the second dataset from the first dataset.

 

I guess I need to use proc iml.  But after reading examples and basic instructions, I still have no clue of how to do it.  I will appreciate it very much if someone can give me some hints or sample code.

 

 

Many thanks!

Dara

 

a.PNGb.PNG


These words are not clear. Perhaps you could state exactly what you mean by "column vector of other n-1 observations", or better yet, show us the calculation you'd like to do ... go ahead and type in an example from the data above showing us the exact calculation you want.

--
Paige Miller
daradanye
Obsidian | Level 7

Hi Paige,

 

Thank you so much for your quick reply.

 

I am assuming each observation (each id) as a unique row vector.  

 

Vector1=(3,2,5,0)

Vector2=(4,0,1,2)

Vector3=(3,2,3,3)

Vector4=(1,3,0,4)

 

What I need to calculate is:

Vector1 * transpose(Vector2) =3*4+2*0+5*1=17

Vector1 * transpose(Vector3)

Vector1 * transpose(Vector4)

Vector2 * transpose(Vector1)

Vector2 * transpose(Vector3)

Vector2 * transpose(Vector4)

Vector3 * transpose(Vector1)

Vector3 * transpose(Vector2)

Vector3 * transpose(Vector4)

Vector4 * transpose(Vector1)

Vector4 * transpose(Vector2)

Vector4 * transpose(Vector3)

 

Hope it is clear now 🙂

 

Thanks,

Dara

PaigeMiller
Diamond | Level 26

@daradanye wrote:

Hi Paige,

 

Thank you so much for your quick reply.

 

I am assuming each observation (each id) as a unique row vector.  

 

Vector1=(3,2,5,0)

Vector2=(4,0,1,2)

Vector3=(3,2,3,3)

Vector4=(1,3,0,4)

 

What I need to calculate is:

Vector1 * transpose(Vector2) =3*4+2*0+5*1=17

Vector1 * transpose(Vector3)

Vector1 * transpose(Vector4)

Vector2 * transpose(Vector1)

Vector2 * transpose(Vector3)

Vector2 * transpose(Vector4)

Vector3 * transpose(Vector1)

Vector3 * transpose(Vector2)

Vector3 * transpose(Vector4)

Vector4 * transpose(Vector1)

Vector4 * transpose(Vector2)

Vector4 * transpose(Vector3)

 

Hope it is clear now 🙂

 

Thanks,

Dara


You have pretty much written the code yourself, except that you would use the T function instead of the word "transpose". Please give it a try and report back if things don't work. Show us the SASLOG and we can probably advise further.

--
Paige Miller
Ksharp
Super User
data have;
input id class1-class4;
cards;
1 3 2 5 0
2 4 0 1 2
3 3 2 3 3
4 1 3 0 4
;
run;
proc iml;
use have(keep=class:) nobs nobs;
read all var _num_ into x;
close;
do i=1 to nobs-1;
 do j=i+1 to nobs;
   id1=id1//i;id2=id2//j;
   value=value//sum(x[i,]#x[j,]);
 end;
end;
create want var {id1 id2 value};
append;
close;
quit;

Reeza
Super User

Please post your data as text, I would have to type it out to work with it. And what are the rules, how is ID2 and Value calculated, explicitly?


You can use IML, but you could also use a data step with a temporary array approach, as well. 

 


@daradanye wrote:

Hi all,

 

I need SAS to do the following thing:

 

For id 1 to n, I need to calculate dot product of line vector of id 1 and column vector of other n-1 observations to get the value.  Basically, I need to obtain the second dataset from the first dataset.

 

I guess I need to use proc iml.  But after reading examples and basic instructions, I still have no clue of how to do it.  I will appreciate it very much if someone can give me some hints or sample code.

 

 

Many thanks!

Dara

 

a.PNGb.PNG


 

daradanye
Obsidian | Level 7

Hi Reeza,

 

Thank you so much.  Attached is the data. 

 

I am assuming each observation (each id) as a unique row vector.

 

Vector1=(3,2,5,0)

Vector2=(4,0,1,2)

Vector3=(3,2,3,3)

Vector4=(1,3,0,4)

 

What I need to calculate is:

Vector1 * transpose(Vector2) =3*4+2*0+5*1=17

Vector1 * transpose(Vector3)

Vector1 * transpose(Vector4)

Vector2 * transpose(Vector1)

Vector2 * transpose(Vector3)

Vector2 * transpose(Vector4)

Vector3 * transpose(Vector1)

Vector3 * transpose(Vector2)

Vector3 * transpose(Vector4)

Vector4 * transpose(Vector1)

Vector4 * transpose(Vector2)

Vector4 * transpose(Vector3)

 

 

Thanks,

Dara

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1824 views
  • 1 like
  • 4 in conversation