BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
xiangpang
Quartz | Level 8

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;
1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

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.

View solution in original post

2 REPLIES 2
s_lassen
Meteorite | Level 14

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.

xiangpang
Quartz | Level 8

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

Thanks again.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 2649 views
  • 3 likes
  • 2 in conversation