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
- /
- Programming
- /
- How could I get RMSE and MAE for each repeat

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

03-20-2018 01:02 AM

Hello,

I want to calculate RMSE and MAE. With data data4rmse I could get one RMSE and MAE using following code.

Now I have data EQ with repeating the experiment for 3 times. How could I get RMSE and MAE for each repeat and output them into one table?

Thanks

```
data data4rmse;
input ID bmi dm bmi_me;
cards;
1 13 40 8
2 13 29 37
3 30 14 25
4 38 38 23
;
run;
data rmse;
retain square_error_sum abs_error_sum;
set data4rmse
end=last /* Flag for the last observation */
;
error = BMI - BMI_me; /* Calculate simple error */
square_error = error * error; /* error^2 */
if _n_ eq 1 then do;
/* Initialize the sums */
square_error_sum = square_error;
abs_error_sum = abs(error);
end;
else do;
/* Add to the sum */
square_error_sum = square_error_sum + square_error;
abs_error_sum = abs_error_sum + abs(error);
end;
if last then do;
/* Calculate RMSE and MAE and store in SAS data set. */
mae = abs_error_sum/_n_;
rmse = sqrt(square_error_sum/_n_);
end;
run;
data eq;
input repeat ID bmi dm bmi_me;
cards;
1 1 13 40 8
1 2 13 29 37
1 3 30 14 25
1 4 38 38 23
2 1 30 45 19
2 2 30 20 14
2 3 67 13 14
2 4 13 27 13
3 1 33 23 46
3 2 13 12 56
3 3 13 13 34
3 4 13 45 13
;
run;
```

Accepted Solutions

Solution

03-20-2018
08:49 AM

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

Posted in reply to xiangpang

03-20-2018 05:13 AM

Something like this, perhaps?

```
data rmse;
square_error_sum = 0;
abs_error_sum = 0;
do n=1 by 1 until(last.repeat);
set eq;
by repeat;
error = BMI - BMI_me; /* Calculate simple error */
square_error = error * error; /* error^2 */
square_error_sum + square_error;
abs_error_sum + abs(error);
end;
/* Calculate RMSE and MAE and store in SAS data set. */
mae = abs_error_sum/n;
rmse = sqrt(square_error_sum/n);
keep repeat mae rmse n;
run;
```

I assumed that you only really wanted one row for each repeat. I included the count (n), in case you have a different number of observations in each repeat.

The statement "a+b;" is called a SUM statement, it is equivalent to "retain a 0; a=sum(a,b);", which can save you a lot of coding.

All Replies

Solution

03-20-2018
08:49 AM

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

Posted in reply to xiangpang

03-20-2018 05:13 AM

Something like this, perhaps?

```
data rmse;
square_error_sum = 0;
abs_error_sum = 0;
do n=1 by 1 until(last.repeat);
set eq;
by repeat;
error = BMI - BMI_me; /* Calculate simple error */
square_error = error * error; /* error^2 */
square_error_sum + square_error;
abs_error_sum + abs(error);
end;
/* Calculate RMSE and MAE and store in SAS data set. */
mae = abs_error_sum/n;
rmse = sqrt(square_error_sum/n);
keep repeat mae rmse n;
run;
```

I assumed that you only really wanted one row for each repeat. I included the count (n), in case you have a different number of observations in each repeat.

The statement "a+b;" is called a SUM statement, it is equivalent to "retain a 0; a=sum(a,b);", which can save you a lot of coding.

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

Posted in reply to s_lassen

03-20-2018 08:51 AM

Thanks for your teaching and code. I am happy to learn it.

Thanks again.