I need to run CAR=UE for every prior 8 quarters and output R^2 and coefficients. Can anyone 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;
tried the data2datastep macro to post my dataset but did not work. I generated a zip file that can't be openned.
If you have the code for the regression, we can help make it into a macro. What issues are you having with the data2datastep macro? Did you download the zip and run the programs?
@vl12 wrote:
I need to run CAR=UE for every prior 8 quarters and output R^2 and coefficients. Can anyone help?
I just have the simple regression code. However, this is not what. I would need the regression run on prior 8 observation for current observation. HOpe I explained clearly. thanks!
data erc; set data.erctest1; proc sort data=erc nodupkey; by gvkey; proc reg data=erc outest=cfsde adjrsq noprint; model car= ue; by gvkey; output out=varone r=residone;
Thank you for your reply.
I downloaded the zip file, run it, got a file which however is zipped and can't be opened. are there another way to post a sample of dataset?
I updated the instructions and simplified them here.
In addition to sample data, you'll also need to provide your regression code.
@vl12 wrote:
Thank you for your reply.
I downloaded the zip file, run it, got a file which however is zipped and can't be opened. are there another way to post a sample of dataset?
Thank you! Now i can copy and paste my sample data.
my code is:But this code does not do what I want. What I want would for each observation, run regression for each gvkey for prior 8 datadate periods
My code is data erc; set data.erctest; proc sort data=erc nodupkey; by gvkey datadate; proc reg data=erc outest=cfsde adjrsq noprint; model car= ue; by gvkey; output out=varone r=residone;
Here is a fairly straitforward approach to this task:
/* Create periods of 9 quarters: previous 8 and current */
data long;
set sasforum.erctest;
do qtr = -8 to 0;
qtrDate = intnx("qtr", datadate, -qtr, "end");
output;
end;
format datadate qtrDate yymmp.;
label qtrDate = "Last quarter of 9";
run;
proc sort data=long; by gvkey qtrdate qtr; run;
/* Fit regressions. Keep only full periods (9 quarters) in the results */
proc reg data=long rsquare noprint
outest=longEst(
where=(_P_ + _EDF_= 9)
keep= _P_ _EDF_ gvKey qtrDate intercept ue _RSQ_);
where cmiss(car, ue) = 0;
by gvkey qtrDate;
model car = ue / selection=none;
run;
Thank you so much PGStats!
How do I require each regression to have at 8 quarter observations?
is the code written this way now:
datadate | qtrdate | qtr |
20100331 | 20120330 | -8 |
20100630 | 20120330 | -7 |
20100930 | 20120330 | -6 |
20101231 | 20120330 | -5 |
20110331 | 20120330 | -4 |
20110630 | 20120330 | -3 |
20110930 | 20120330 | -2 |
20111231 | 20120330 | -1 |
20120330 | 20120330 | 0 |
if for example 20110930 is missing, then it goes back to get another 8 quarter of data? so it would look like this?
datadate | qtrdate | qtr |
20091231 | 20120330 | -8 |
20100331 | 20120330 | -7 |
20100630 | 20120330 | -6 |
20100930 | 20120330 | -5 |
20101231 | 20120330 | -4 |
20110331 | 20120330 | -3 |
20110630 | 20120330 | -2 |
20110930 | 20120330 | . |
20111231 | 20120330 | -1 |
20120330 | 20120330 | 0 |
Which observations (dates) should be involved in the regression associated with datadate=20120330, for example ?
My code assumes that no date is missing. A missing date will remove all 9 regressions involving that date. This is true also for the first 8 dates. What would you like to do with missing observations?
Thank you so much, PG, for continued help!
for datadate=20120330, the regression should involve the following colored observations. Which means that I would go back to replace the missing obs by going back further to get 8 quarters. How would this to be done?
datadate | qtrdate | qtr |
20091231 | 20120330 | -8 |
20100331 | 20120330 | -7 |
20100630 | 20120330 | -6 |
20100930 | 20120330 | -5 |
20101231 | 20120330 | -4 |
20110331 | 20120330 | -3 |
20110630 | 20120330 | -2 |
20110930 | 20120330 | . |
20111231 | 20120330 | -1 |
20120330 | 20120330 | 0 |
If you have SAS ETS try PROC TIMESERIES.
Thank you, Reesa! but I am not sure how
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.