Hello all,

I have been using the following code in order to create a variable that gives the average rating of a golf course at the time when a user reviews it:

PHP Code:
gen avg_course_score_relative 0
sort courseid firstdate
forvalues i 
2/2720511 {

if 
courseid[`i'] == courseid[`i' - 1] {
if avg_course_score_relative[`i'
] == {
replace avg_course_score_relative avg_course_score_relative[_n 1] + 1 in `i'
}
if avg_course_score_relative[
`i'] != 1 {
replace avg_course_score_relative = avg_course_score_relative[_n - 1] in `i'
}
}
if 
courseid[`i'] != courseid[`i' - 1] {

replace avg_course_score_relative = 0 in `i'

}

}
replace avg_course_score_relative avg_course_score_relative if avg_course_score_relative != 0

save 
"data/courses_ratings"replace 

Running this code gives me the "observations out of range (r198) error". Any ideas about how I can resolve this issue?