Dear Statalisters,

I'd like to calculate a firm's age based on number of years the firm has been on the database with non-missing stock price data (prcc_c).The threshold is 40, if actual value is higher it gets replaced with 40. The last year for the count is 2019

I have tried
Code:
rangestat (count)datayear, interval(datayear -40 1) by (gvkey)
, however this does not allow setting the stock-price condition 'if prcc_c<.'. I am also trying to set the interval to 1950-2019 (sampling period)
for the threshold
Code:
replace AGE=40 if AGE>40 & !missing(AGE)
Can anyone help what would be a more appropriate code?

Example of data:
[CODE]
Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str6 gvkey long datadate double fyear str8 tic double prcc_c long ipodate float(ipoyear datayear)
"001004"  2342 1965 "AIR"           . 10227 1988 1966
"001004"  2707 1966 "AIR"           . 10227 1988 1967
"001004"  3073 1967 "AIR"        15.5 10227 1988 1968
"001004"  3438 1968 "AIR" 22.24998067 10227 1988 1969
"001004"  3803 1969 "AIR" 12.24998541 10227 1988 1970
"001004"  4168 1970 "AIR" 17.87498243 10227 1988 1971
"001004"  4534 1971 "AIR" 22.24995647 10227 1988 1972
"001004"  4899 1972 "AIR"  24.9999757 10227 1988 1973