DATA Step, Macro, Functions and more

Needs help in calculating line/row vector in SAS dataset

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Needs help in calculating line/row vector in SAS dataset

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


Accepted Solutions
Solution
‎05-15-2018 01:04 PM
Super User
Posts: 10,844

Re: Needs help in calculating line/row vector in SAS dataset

Posted in reply to PaigeMiller
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


All Replies
Respected Advisor
Posts: 3,247

Re: Needs help in calculating line/row vector in SAS dataset

Posted in reply to daradanye

@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
New Contributor
Posts: 3

Re: Needs help in calculating line/row vector in SAS dataset

Posted in reply to PaigeMiller

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 Smiley Happy

 

Thanks,

Dara

Respected Advisor
Posts: 3,247

Re: Needs help in calculating line/row vector in SAS dataset

Posted in reply to daradanye

@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 Smiley Happy

 

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
Solution
‎05-15-2018 01:04 PM
Super User
Posts: 10,844

Re: Needs help in calculating line/row vector in SAS dataset

Posted in reply to PaigeMiller
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;

Super User
Posts: 23,928

Re: Needs help in calculating line/row vector in SAS dataset

Posted in reply to daradanye

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


 

New Contributor
Posts: 3

Re: Needs help in calculating line/row vector in SAS dataset

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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