thanks again. However the code won't work for me as it's only for one company ibm and has an exact beginning and ending dates. My sample companies have different beginning and ending dates.
PG's code works just that I need to back fill the missing quarter with previous quarter. If this can be solved in his original code, that would solve my problem.
Thank you so much for trying to help!
If all you want is a regression of the previous 8 obs, then the dates don't matter and the code is a bit different:
/* Create periods of 8 previous observations */
data long;
do obs = 1 by 1 until(last.gvKey);
set sasforum.erctest;
by gvkey notsorted;
do grp = obs+1 to obs+8;
if grp > 8 then output;
end;
end;
run;
proc sort data=long; by gvkey grp; run;
/* Fit regressions. Keep only full groups (8 obs) in the results */
proc reg data=long rsquare noprint
outest=longEst(
where=(_P_ + _EDF_= 8)
keep= _P_ _EDF_ gvKey grp intercept ue _RSQ_);
where cmiss(car, ue) = 0;
by gvkey grp;
model car = ue / selection=none;
run;
proc sql;
create table regStats as
select distinct
b.gvKey,
b.datadate,
a.intercept,
a.ue as slope,
a._RSQ_
from
longEst as a inner join
long as b on a.gvKey=b.gvKey and a.grp=b.obs
order by gvKey, datadate;
quit;
Hi PG,
yes. This would work. This code restrict the regression to be run on each gvkey datadate pair to 8 observations,which is what I ask for.
If I'd like to use all the observations in prior quarters but require at least 8 obs for a given gvkey and datadate, how can I do it? So some regression will have 8 obs, some will have 9 or more. And can you output the number of obs used in the regression as well?
Thank you so much!
You seem to want a cumulative regression now. That's a different matter. It might be better to put it as a new question to attact more attention.
thanks. will make a new post
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.