Find the mean of a variable depending on another variables value?

Reply
Highlighted
New Contributor
Posts: 2

Find the mean of a variable depending on another variables value?

Basically, I have 3 given variables and then 3 calculated variables. I want to find the means of GPA at each level of GPALevel.

So I want my program to look at GPALevel and when GPALevel=1 I want it to calculate the mean of all the GPA's that are at GPALevel1. And when GPALevel=2 I want to calculate the mean of all the GPA's that have GPALevel=2. I can't figure out how to do this, whatever I try just outputs the mean of ALL the GPA's regardless of what their GPALevel is.

 

Here is my code:

 

data SATGPA;
input MathSAT $ VerbalSAT GPA;
SAT=MathSAT + VerbalSAT;
IF SAT<=1100 THEN SATLevel=1;
IF SAT>1100 and SAT<=1200 THEN SATLevel=2;
IF SAT>1200 and SAT<=1300 THEN SATLevel=3;
IF SAT>1300 THEN SATLevel=4;
IF GPA<=2.8 THEN GPALevel=1;
IF GPA>2.8 and GPA<=3.2 THEN GPALevel=2;
IF GPA>3.2 and GPA<=3.5 THEN GPALevel=3;
IF GPA>3.5 THEN GPALevel=4;
datalines;
580	420	 2.9
670	530	2.83
680	540	 2.9
630	640	 3.3
620	630	3.61
580	550	2.75
620	600	2.75
690	500	   3
520	500	2.77
570	630	 2.9
620	550	   3
690	570	3.25
350	300	3.13
680	570	3.53
550	530	 3.1
570	540	 3.2
620	640	3.27
750	560	 3.3
700	680	 2.6
670	550	3.53
680	550	2.67
590	700	 3.3
600	650	 3.5
630	640	 3.7
;

 

Super User
Posts: 20,236

Re: Find the mean of a variable depending on another variables value?

Posted in reply to KoalaKrazy

I can't figure out how to do this, whatever I try just outputs the mean of ALL the GPA's regardless of what their GPALevel is.

Can you include what you've tried so far then? 

 

Spoiler

KoalaKrazy wrote:

Basically, I have 3 given variables and then 3 calculated variables. I want to find the means of GPA at each level of GPALevel.

So I want my program to look at GPALevel and when GPALevel=1 I want it to calculate the mean of all the GPA's that are at GPALevel1. And when GPALevel=2 I want to calculate the mean of all the GPA's that have GPALevel=2. I can't figure out how to do this, whatever I try just outputs the mean of ALL the GPA's regardless of what their GPALevel is.

 

Here is my code:

 

data SATGPA;
input MathSAT $ VerbalSAT GPA;
SAT=MathSAT + VerbalSAT;
IF SAT<=1100 THEN SATLevel=1;
IF SAT>1100 and SAT<=1200 THEN SATLevel=2;
IF SAT>1200 and SAT<=1300 THEN SATLevel=3;
IF SAT>1300 THEN SATLevel=4;
IF GPA<=2.8 THEN GPALevel=1;
IF GPA>2.8 and GPA<=3.2 THEN GPALevel=2;
IF GPA>3.2 and GPA<=3.5 THEN GPALevel=3;
IF GPA>3.5 THEN GPALevel=4;
datalines;
580	420	 2.9
670	530	2.83
680	540	 2.9
630	640	 3.3
620	630	3.61
580	550	2.75
620	600	2.75
690	500	   3
520	500	2.77
570	630	 2.9
620	550	   3
690	570	3.25
350	300	3.13
680	570	3.53
550	530	 3.1
570	540	 3.2
620	640	3.27
750	560	 3.3
700	680	 2.6
670	550	3.53
680	550	2.67
590	700	 3.3
600	650	 3.5
630	640	 3.7
;

 


New Contributor
Posts: 2

Re: Find the mean of a variable depending on another variables value?

I tried IF THEN statements.
IF GPALevel=1 THEN Mean1=mean(GPA)
Super User
Posts: 20,236

Re: Find the mean of a variable depending on another variables value?

Posted in reply to KoalaKrazy

Here's an example:

https://github.com/statgeek/SAS-Tutorials/blob/master/proc_means_basic

 

*Create summary statistics for a dataset by a 'grouping' variable and store it in a dataset;

*Generate sample fake data;
data have;
	input ID          feature1         feature2         feature3;
	cards;
1               7.72               5.43              4.35
1               5.54               2.25              8.22 
1               4.43               6.75              2.22
1               3.22               3.21              7.31
2               6.72               2.86              6.11
2               5.89               4.25              5.25 
2               3.43               7.30              8.21
2               1.22               3.55              6.55

;
run;

*Create summary data;
proc means data=have noprint;
	by id;
	var feature1-feature3;
	output out=want median= var= mean= /autoname;
run;

*Show for display;
proc print data=want;
run;

 

 

Ask a Question
Discussion stats
  • 3 replies
  • 83 views
  • 0 likes
  • 2 in conversation