BookmarkSubscribeRSS Feed
tinghlin
Fluorite | Level 6

Dear all :

I have a data as follows:  

 

collegeIDx1x2
A11015
A21118
A31221
A41326
B51820
B62022
B72224
B82426

 

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: 

collegeIDx1x2x1meanx1stdx2meanx2std
A1101511.51204.69
A2111811.51204.69
A3122111.51204.69
A4132611.51204.69
B51820212.58232.58
B62022212.58232.58
B72224212.58232.58
B82426212.58232.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!

 

2 REPLIES 2
PaigeMiller
Diamond | Level 26

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

--
Paige Miller

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
  • 992 views
  • 0 likes
  • 3 in conversation