Solved
New Contributor
Posts: 3

# Average monthly Price

Hi All

I am trying to calculate AVERAGE MONTHLY PRICE using the PROC SQL Code but stryggling to get the right coding.

The data looks like this :

Dates                    Amount

01Jul1994            1000

02MAY1995         1500

10MAY1995         2000

09JUN1995         3000

27JUN1995         3500

.

.

.

.

.

.AND SO ON FOR DIFFENRENT MONTHS IN DIFFERENT YEARS.

How to go abou it? Any suggestion will be helpfull

Accepted Solutions
Solution
‎06-21-2017 12:20 PM
Super User
Posts: 10,766

## Re: Average monthly Price

``````data have;
input Dates : date9.     Amount;
format dates date.;
cards;
01Jul1994            1000
02MAY1995         1500
10MAY1995         2000
09JUN1995         3000
27JUN1995         3500
;
run;
proc summary data=have nway;
class dates;
format dates monyy7.;
var amount;
output out=want mean=;
run;``````

All Replies
Posts: 2,989

## Re: Average monthly Price

[ Edited ]

Create a variable called MONTH, you can use the MONTH function in SAS to do this. Also create a variable called YEAR, using the YEAR function in SAS.

Then run PROC MEANS or PROC SUMMARY with YEAR and MONTH as CLASS variables.

--
Paige Miller
Super User
Posts: 9,599

## Re: Average monthly Price

Something like:

```proc sql;
create table WANT as
select MNTH,
avg(AMOUNT) as AVG
from   (select month(DATES) as MNTH,AMOUNT from HAVE)
group by MNTH;
quit;```
Super User
Posts: 10,214

## Re: Average monthly Price

Something like

``````proc sql;
create table want as
select put(dates,yymmn6.) as yymm, mean(amount) as mean_price
from have
group by calculated yymm
;
quit;``````

??

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎06-21-2017 12:20 PM
Super User
Posts: 10,766

## Re: Average monthly Price

``````data have;
input Dates : date9.     Amount;
format dates date.;
cards;
01Jul1994            1000
02MAY1995         1500
10MAY1995         2000
09JUN1995         3000
27JUN1995         3500
;
run;
proc summary data=have nway;
class dates;
format dates monyy7.;
var amount;
output out=want mean=;
run;``````
Posts: 2,989

## Re: Average monthly Price

Ksharp wrote:
``````data have;
input Dates : date9.     Amount;
format dates date.;
cards;
01Jul1994            1000
02MAY1995         1500
10MAY1995         2000
09JUN1995         3000
27JUN1995         3500
;
run;
proc summary data=have nway;
class dates;
format dates monyy7.;
var amount;
output out=want mean=;
run;``````

Certainly a superior method than what I described above.

--
Paige Miller
☑ This topic is solved.