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
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;
@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
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.
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
@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.
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;
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
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.