I am using Stata version 15.1 and I have 47 variables and 1,010 observations in my dataset. I am doing educational research with 112 colleges included (This is setup as a panel data set). However, I have expanded it so that I can capture the average prices for all colleges in set categories. I now have 69,999 observations in my data set.

My goal:
I am trying to create a variable that will represent the average price of colleges within a year range on my panel data set. I need to generate a yearly average for the following categories of colleges - 1—Public, four-year or above 2—Private not-for-profit, four-year or above 3—Private for-profit, four-year or above 4—Public, two-year. I have all the individual colleges in the data set and I know their prices. The variable for the price I want to average is "avgprice_per_year."

My first thought was to do the following:

sort unitid year
egen avgprice_pub_4yr_year=mean(avg_tuition_fees_ft), by(year)
but this only generates the average across all colleges for the year. I want it to generate a new variable that shows the average price for each type of college (1—Public, four-year or above 2—Private not-for-profit, four-year or above 3—Private for-profit, four-year or above 4—Public, two-year). So I would then have a new variable that is generated that cumulates all the Public, four-year colleges prices and adds this to every instance of the year 2009 for example. I would need to do this four times to then generate each of these four variables to then later use in my regression to see how alternative college pricing may impact the enrollment rate.