Dear all :
I have a data as follows:
college | ID | x1 | x2 |
A | 1 | 10 | 15 |
A | 2 | 11 | 18 |
A | 3 | 12 | 21 |
A | 4 | 13 | 26 |
B | 5 | 18 | 20 |
B | 6 | 20 | 22 |
B | 7 | 22 | 24 |
B | 8 | 24 | 26 |
I use "proc means" to get the mean and standard deviation of X1 and x2 by college.
proc means;
var x;
by by college;
run;
However, I need to create a new data set with means and standard deviations (by colleges) as 4 new variables, as follows:
college | ID | x1 | x2 | x1mean | x1std | x2mean | x2std |
A | 1 | 10 | 15 | 11.5 | 1 | 20 | 4.69 |
A | 2 | 11 | 18 | 11.5 | 1 | 20 | 4.69 |
A | 3 | 12 | 21 | 11.5 | 1 | 20 | 4.69 |
A | 4 | 13 | 26 | 11.5 | 1 | 20 | 4.69 |
B | 5 | 18 | 20 | 21 | 2.58 | 23 | 2.58 |
B | 6 | 20 | 22 | 21 | 2.58 | 23 | 2.58 |
B | 7 | 22 | 24 | 21 | 2.58 | 23 | 2.58 |
B | 8 | 24 | 26 | 21 | 2.58 | 23 | 2.58 |
11.5 is the mean of X1 for college A, 21 is the mean of X1 for college B,
1 is the std X1 for college A, 2.58 is the std of X1 for college B, etc
How do I write the program and rename the mean and std? thank you!
If your goal is to then subtract off the mean and divide by the standard deviation (is it?) this can be done in one step without much programming via PROC STDIZE. Example: https://documentation.sas.com/?docsetId=statug&docsetVersion=15.1&docsetTarget=statug_stdize_getting...
If you just want the data set you mentioned, but you don't want to use the mean and standard deviation as stated above (what other use would you have for putting these together?), then you can merge the original data set with the data set of means and standard deviations (this assumes your original data is properly sorted by college).
proc summary data=have nway;
var x1 x2;
class college;
output out=_stats_ mean= std=/autoname;
run;
data want;
merge have _stats_;
by college;
run;
Again, I raise the issue why do you want to have mean and standard deviation in the same data set as the original data? Depending on what you are doing, and depending on what the next step is, there are probably other methods that would be faster and less programming ... So tell us why
Here are the most common two ways:
https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas
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!
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.