turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Needs help in calculating line/row vector in SAS d...

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-14-2018 04:33 PM

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

Accepted Solutions

Solution

05-15-2018
01:04 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to PaigeMiller

05-15-2018 09:13 AM

```
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;
```

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to daradanye

05-14-2018 04:39 PM

@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.

--

Paige Miller

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to PaigeMiller

05-14-2018 04:48 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to daradanye

05-15-2018 08:06 AM

@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

Paige Miller

Solution

05-15-2018
01:04 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to PaigeMiller

05-15-2018 09:13 AM

```
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;
```

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to daradanye

05-14-2018 04:39 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

05-14-2018 04:53 PM

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