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 algebra

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

11-19-2016 07:45 AM

how can I write a simple code for array (matrix) algebra, this are my input information

y and yhat are the arrays with one column and "n" observation

the process is repeating k times ( i = 1,...k) and for each run we have different y & yhat,

for each run the "mean absolute difference" between y & yhat should save in "m".

`m{i} = sum(of abs(y - yhat));`

I don't want to use any proc, how can I write a simple code??

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

Posted in reply to Marzi

11-19-2016 08:30 AM

So you want to do this inside a data step?

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

Posted in reply to Marzi

11-19-2016 09:28 AM

If you want to do matrix calculations your best off switching to SAS IML.

But you really haven't told us much. Depending on your data a simple datastep may also suffice. Please post sample data and expected output if you want sample code.

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

Posted in reply to Reeza

11-19-2016 11:05 AM

If you have any sample data, look at next link and try to create any initial code that might do what you want.

If you have difficulties, post your code to the forum and you will get help.

https://communities.sas.com/t5/Base-SAS-Programming/Array-help/td-p/298434

Treat this link as a demo for how to write a data step and how to define and use arrays in sas.

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

Posted in reply to Marzi

11-19-2016 03:23 PM

For the DATA step solution, sort by the grouping variable and use a BY statement to accumulate the absolute residuals.

Since you mentioned matrix algebra, I'll give a SAS/IML solution, which uses the UNIQUE-LOC technique to loop over the groups:

```
proc glm data=sashelp.class plots=none;
class sex;
model weight = height;
output predicted=yhat out=have(rename=(weight=y sex=grp) );
run;
proc iml;
use have;
read all var {grp y yhat};
close;
r = abs(y-yhat);
u = unique(grp);
sum = j(1, ncol(u));
do i = 1 to ncol(u);
idx = loc(grp = u[i]);
sum[i] = sum(r[idx]);
end;
print sum[colname=u];
```

If you have huge data or many groups, it is more efficient to use the UNIQUEBY tecnique to loop over the groups.