DATA Step, Macro, Functions and more

Create new variable with proc means

Reply
Contributor
Posts: 27

Create new variable with proc means

 Hello I am trying  to calculate average class grade after having calculated the grade for each individual student. How would I capture the proc means output and create a new variable from this? I saw a similar question and have played around with the code but its not giving me a new variable. the dataset newP5 includes the individual grades. Thanks for any help!

 

proc means data=newP5;
var Grade;
output out=new mean(Grade)=class;
run;
PROC Star
Posts: 1,769

Re: Create new variable with proc means

Posted in reply to iressa131

Not sure if i understand your question, are you after?

 

proc means data=newP5;
var Grade;
output out=new mean=mean_grade;
run;
Contributor
Posts: 27

Re: Create new variable with proc means

Posted in reply to novinosrin

Hi this code gives me the same output i have been getting which does give me the correct average but I am looking to create a new variable for that value. For example I average 5 test scores (so i have 5 variables) for each student then get the overall average of 90 but now instead of wanting to just see it in my output, I want it to be a new variable as part of my dataset so that i have a total of 6 variables. I hope that clarified it!

Super User
Posts: 13,498

Re: Create new variable with proc means

Posted in reply to iressa131

@iressa131 wrote:

Hi this code gives me the same output i have been getting which does give me the correct average but I am looking to create a new variable for that value. For example I average 5 test scores (so i have 5 variables) for each student then get the overall average of 90 but now instead of wanting to just see it in my output, I want it to be a new variable as part of my dataset so that i have a total of 6 variables. I hope that clarified it!


Multiple variables is poor data structure. Better is to have something like
StudentId  TestId Score

 

Then a report procedure:

Proc tabulate data=have;
   class studentid testid;
   var score;
   table studentid,
         (testid all)*score*mean=''
   ;
run;

would create on row per student and have the scores across with the overall mean as the last column.

 

added bonus: use table studentid All,  and you would have the average score for each test across all of the students as the last row.

Super User
Posts: 6,751

Re: Create new variable with proc means

Posted in reply to iressa131

If I understand you correctly, the data set NEWP5 contains the variable CLASS as well as the variable GRADE, holding the grade for each individual student.  In that case:

 

proc summary data=newp5 nway;

class class;

var grade;

output out=want (keep=class class_grade) mean(grade) = class_grade;

run;

 

Take a look at the results by printing the data set WANT.  If it contains the right information, we can talk about how to combine it with the existing data set NEWP5 ... only if you want them combined.

 

Ask a Question
Discussion stats
  • 4 replies
  • 43 views
  • 0 likes
  • 4 in conversation