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
- /
- array for sumproduct across variables

Topic Options

- Subscribe to 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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

02-13-2014 04:26 PM

Need help to finish the below code to get sumproduct with arrays. Thanks

data test;

array xvals{1:1000} _TEMPORARY_;

array yvals{1:1000} _TEMPORARY_;

do i=1 to 1000;

sumproduct=sum(of xvals(i)*yvals(i)); /* I am stuck here ???*/

end;

run;

Accepted Solutions

Solution

02-13-2014
06:47 PM

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

Posted in reply to Reeza

02-13-2014 06:47 PM

Thanks Reeza. Finally I came up with the below solution to have sum product for all 1000 values (variables in case of my dataset). This is same as excel sumproduct with few lines of SAS code. Kudos !!!

%macro vals;

%do i = 1 %to 1000;

&i

%end;

%mend;

data test;

array xvals{1000} _TEMPORARY_ (%vals);

array yvals{1000} _TEMPORARY_ (%vals);

sumproduct = 0;

do i=1 to 1000;

sumproduct= sum(sumproduct, (xvals(i)*yvals(i)));

end;

run;

All Replies

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

Posted in reply to nash_sas

02-13-2014 04:48 PM

You're not retaining anything between loop iterations, so the value you're getting, summing it doesn't error out, is the last summation.

This assumes that you want the sum of xvals(i)*yvals(i) and not something else.

data test;

array xvals{1:1000} _TEMPORARY_;

array yvals{1:1000} _TEMPORARY_;

sumproduct=0;

do i=1 to 1000;

sumproduct=sumproduct+xvals(i)*yvals(i); /* I am stuck here ???*/

end;

run;

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

Posted in reply to Reeza

02-13-2014 04:59 PM

Thanks Reeza. I tried that but still end result of sumproduct didn't give the expected result. started with 3 values, instead of 1000 and expected result for sumproduct is 14 = (1*1) + (2*2) + (3*3 ) which isn't coming with this code

data test;

array xvals{1:3} _TEMPORARY_;

array yvals{1:3} _TEMPORARY_;

sumproduct=0;

do i=1 to 3;

sumproduct=sumproduct+xvals(i)*yvals(i);

end;

run;

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

Posted in reply to nash_sas

02-13-2014 05:01 PM

Where do you put the values in the array?

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

Posted in reply to nash_sas

02-13-2014 05:02 PM

You need to give XVALS and YVALS some values.

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

Posted in reply to nash_sas

02-13-2014 05:53 PM

data test;

array xvals{1:3} _TEMPORARY_ (1:3);

array yvals{1:3} _TEMPORARY_ (1:3);

sumproduct=0;

do i=1 to dim(xvals);

sumproduct=sumproduct+xvals(i)*yvals(i);

end;

run;

Solution

02-13-2014
06:47 PM

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

Posted in reply to Reeza

02-13-2014 06:47 PM

Thanks Reeza. Finally I came up with the below solution to have sum product for all 1000 values (variables in case of my dataset). This is same as excel sumproduct with few lines of SAS code. Kudos !!!

%macro vals;

%do i = 1 %to 1000;

&i

%end;

%mend;

data test;

array xvals{1000} _TEMPORARY_ (%vals);

array yvals{1000} _TEMPORARY_ (%vals);

sumproduct = 0;

do i=1 to 1000;

sumproduct= sum(sumproduct, (xvals(i)*yvals(i)));

end;

run;

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

Posted in reply to nash_sas

02-13-2014 06:52 PM

What is the purpose of the ARRAYS? You are summing i*i.