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

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!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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