Programming the statistical procedures from SAS

An easy question about finding out the mean and adding it to data set as a variable

Accepted Solution Solved
Reply
Contributor
Posts: 65
Accepted Solution

An easy question about finding out the mean and adding it to data set as a variable

Dear all,

I want to find the mean of different group in my data set and create a variable for the mean. I would appreciate if you suggest a solution to my problem.

Suppose I have the below data set:

Name of CompanyClass of StocksDateStock Price
XXXA10/20/14100
XXXA9/20/14110
XXXA8/20/14105
XXXB10/21/14120
XXXB9/21/14122
XXXB8/22/14124
XXXB11/21/14127
XXXC10/20/14130
XXXC9/20/14133
XXXC8/20/14137
YYYYYA10/20/14102
YYYYYA9/20/14108
YYYYYA8/20/14104
YYYYYB10/21/14115
YYYYYB9/21/14113
YYYYYB8/22/14110
YYYYYB11/21/14118
YYYYYC10/20/14121
YYYYYC9/20/14126
YYYYYC8/20/14122

As you see, I have the stock price of different class of stocks for some firms, at different time.

Now I want to find out the average stock price for each class of stocks ( and create a separate variable for the mean amount. In other word, I need to reach to the below table:

Name of CompanyClass of StocksDateStock PriceAverageprice
XXXA10/20/14100105
XXXA9/20/14110105
XXXA8/20/14105105
XXXB10/21/14120123.25
XXXB9/21/14122123.25
XXXB8/22/14124123.25
XXXB11/21/14127123.25
XXXC10/20/14130133
XXXC9/20/14133133
XXXC8/20/14136133
YYYYYA10/20/14102104.6666667
YYYYYA9/20/14108104.6666667
YYYYYA8/20/14104104.6666667
YYYYYB10/21/14115114
YYYYYB9/21/14113114
YYYYYB8/22/14110114
YYYYYB11/21/14118114
YYYYYC10/20/14121123
YYYYYC9/20/14126123
YYYYYC8/20/14122123

In table above for different dates of each type of stocks is shown in the averageprice column.

I tried the following code:

proc sql;

create table Average_price as

select *,

mean (Stock price) as Averageprice

from Stock_price;

quit;

However, the result just shows the total average, not the average price for specific class of a certain company's stocks.

Thanks for your help.


Accepted Solutions
Solution
‎10-27-2014 08:47 AM
SAS Employee
Posts: 340

Re: An easy question about finding out the mean and adding it to data set as a variable

proc sql;

create table Average_price as

select *,

mean (Stock price) as Averageprice

from Stock_price

GROUP BY name_of_company, class_of_stocks;

quit;

View solution in original post


All Replies
Solution
‎10-27-2014 08:47 AM
SAS Employee
Posts: 340

Re: An easy question about finding out the mean and adding it to data set as a variable

proc sql;

create table Average_price as

select *,

mean (Stock price) as Averageprice

from Stock_price

GROUP BY name_of_company, class_of_stocks;

quit;

Contributor
Posts: 65

Re: An easy question about finding out the mean and adding it to data set as a variable

Thank you.

Contributor
Posts: 65

Re: An easy question about finding out the mean and adding it to data set as a variable

How can I eliminate the repetition in data? For example if I want to find the following table:

Name of CompanyClass of StocksAverageprice
XXXA105
XXXB123.25
XXXC133
YYYYYA104.6666667
YYYYYB114
YYYYYC123
SAS Employee
Posts: 340

Re: An easy question about finding out the mean and adding it to data set as a variable

proc sql;

create table Average_price as

select name_of_company, class_of_stocks, mean (Stock price) as Averageprice

from Stock_price

GROUP BY name_of_company, class_of_stocks;

quit;

So: simply include grouping and aggregated variables only.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 244 views
  • 4 likes
  • 2 in conversation