BookmarkSubscribeRSS Feed
zjhansen30
Fluorite | Level 6

I am trying to combine 3 results into 1, renaming it and getting the mean of those 3 results.  I have searched and searched but can't find any info on this, except for using a by or class statement, which is not what I am looking for, so far I only have this....

 

proc mean data=pat_info;
var result1 result2 result3;
run;

5 REPLIES 5
Reeza
Super User

Your question is ambiguous.

 

Are you trying to take the average of 3 values that are already averages? 

Or combining 3 datasets to get one average?

zjhansen30
Fluorite | Level 6

Sorry. never done this procedure before.  I am trying to create a variable, for example mean_result, using 3 different variable averages with 15 observations.  I need to combine the 3 result averages to get the mean for all 3 results, for all 15 obsv.

Reeza
Super User

Forget the procedure. Explain what you want in plain english, because I don't understand as is. 

 

Do you have multiple data sets you're working with, or multiple variables within a data set, or multiple groups within a dataset?

 

Maybe provide a small sample of fictional data that illustrates your issue. 

 

If I assume you have three different variables and you want the average of averages then this would work - make sure that you account for the different number of observations especially if you have missing values anywhere.

 

  data class;
  set sashelp.class;

  weight2=weight+5;
  weight3 =weight+3;
  run;

  proc means data=class stackods mean;
  var weight weight2 weight3;
  ods output summary=mean_weight;
  run;

  proc means data=mean_weight;
  var mean;
  output out=want_p_means mean(mean)=average;
  run;
zjhansen30
Fluorite | Level 6

Result1 Result2 Result3
4              4.2          4.4
2.3           3.4          4.5
1.9           4.3             6
1.4           1.4          1.4
10.3            9          5.2
3.5           3.3          5.2
3.9              5          7.6
4.7           7.3             1
7.8           1.3         5.6
4               5.1            .
4.4            1.5           2
6                   .            .
6.3                .            .
1.6            1.6             .
1.8            2.3         2.3

 

Above is the 3 variables I have. I am trying to create a variable mean_result, which is the mean of the above 3 variables.

Reeza
Super User

A very long work through but several solutions are here, 3, 2 work and 1 isn't correct but is done to illustrate the issue with that method.

 

data have;
infile cards truncover;
input Result1 Result2 Result3;
cards;
4              4.2          4.4
2.3           3.4          4.5
1.9           4.3             6
1.4           1.4          1.4
10.3            9          5.2
3.5           3.3          5.2
3.9              5          7.6
4.7           7.3             1
7.8           1.3         5.6
4               5.1            .
4.4            1.5           2
6                   .            .
6.3                .            .
1.6            1.6             .
1.8            2.3         2.3
;
run;

*WRONG WAY to do this;
  proc means data=have  noprint;
  var Result: ;
  output out=summary_mean mean(result1)=result1 mean(result2)=result2 mean(result3)=result3;
  run;

data wrong_answer;
set summary_mean;
solution = mean(result1, result2, result3);
run;

*One step data step solution;
  data data_step_solution;
  set have end=eof;
  retain running_total running_count;
  Running_total = sum(running_total, result1, result2, result3);
  Running_count = sum(running_count, n(result1, result2, result3));

  if eof then do;
     mean = running_total/running_count;
     output;
  end;

  keep mean;
  run;

  *Combine via a view and then proc means;
  data combined /view=combined;
  set have(rename=result1=result )
  have(rename=result2=result)
  have(rename=result3=result);
  keep result;
  run;

 
  proc means data=combined noprint;
  var result;
  output out=mean_result mean(result)=mean_result;
  run;

  title "Wrong answer - doesn't account for missing";
  proc print data=wrong_answer;
  run;
  title "Answer from data step";
    proc print data=data_step_solution;
  run;
  title "Answer from proc means with view";
    proc print data=mean_result;
  run;




 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 5 replies
  • 2562 views
  • 0 likes
  • 2 in conversation