Date: Wed, 19 May 2010 05:33:34 -0400
Reply-To: Gerhard Hellriegel <gerhard.hellriegel@T-ONLINE.DE>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Gerhard Hellriegel <gerhard.hellriegel@T-ONLINE.DE>
Subject: Re: Loop for years and quarters
stepping forward monthly is done with the INTNX function. You can use that
with other units, like:
data a;
x="01jan1900"d;
y=intnx("quarter",x,3);
put y date9.;
run;
or in your case like:
...
date1 = %sysfunc (intnx(quarter, &date2, -&xmonths+1, begin));
...
So look for all calls of INTNX, replace month by quarter and see if the
logic does fit to your needs.
Gerhard
On Wed, 19 May 2010 04:32:42 -0400, SAS user <Sasuser4@GOOGLEMAIL.COM>
wrote:
>Hello
>I want to run quarterly rolling regressions; the objective is to iterate
>forward one quarter at a time and to use 20 quarters as each loop sample
>length. I have two separate variables in my dataset: year and quarter.
>I found the following code for running rolling monthly regresions. Any
>ideas about how to amend it to run quarterly regressions?
>Thanks.
>
>%let id_list = 73112 77002 77762;
>
>%let start_year= 1975;
>%let end_year= 2009;
>%let sample_years= 5;
>%let year1= %eval(&start_year-&sample_years);
>** YEAR1 is set to make sure a full sample size is extracted;
>
>* Results saved here;
>libname lib 'h:\results';
>%let final_ds= results;
>
>* 2. Define Year-Month loop, YY MM macros variable used to count forward
>years and months;
>
>%macro RRLOOP (year1= 1975, year2= 2009, nyear= 5, in_ds= mydata,
>out_ds=work.out_ds);
>
>%local date1 date2 date1f date2f yy mm;
>
>*Extra step to be sure to start with clean, null datasets for appending;
>proc datasets nolist lib=work;
> delete all_ds oreg_ds1;
>run;
>
>*Loop for years and months;
> %do yy = &year1 %to &year2;
> %do mm = 3 %to 12 %by 3;
>
> *Set date2 for mm-yy end point and date1 as 24 months prior;
> %let xmonths= %eval(12 * &nyear); *Sample period length in months;
> %let date2=%sysfunc(mdy(&mm,1,&yy));
> %let date2= %sysfunc (intnx(month, &date2, 0,end)); *Make the DATE2 last
>day of the month;
> %let date1 = %sysfunc (intnx(month, &date2, -&xmonths+1, begin)); *set
>DATE1 as first (begin) day;
> *FYI --- INTNX quirk in SYSFUNC: do not use quotes with 'month' 'end'
>and 'begin';
>
>*An extra step to be sure the loop starts with a clean (empty) dataset for
>combining results;
>proc datasets nolist lib=work;
> delete oreg_ds1;
>run;
>
>*Regression model estimation -- creates output set with coefficient
>estimates;
>proc reg noprint data=&in_ds outest=oreg_ds1 edf;
> where datadate between &date1 and &date2; *Restricted to DATE1- DATE2
>data range in the loop;
> model y = x;
> by id;
>run;
>
>*Store DATE1 and DATE2 as dataset variables;
>* and rename regression coefficients as ALPHA and BETA;
>data oreg_ds1;
> set oreg_ds1;
> date1=&date1;
> date2=&date2;
> nobs= _p_ + _edf_;
> format date1 date2 yymmdd10.;
>run;
>
>* Append loop results to dataset with all date1-date2 observations;
>proc datasets lib=work;
> append base=all_ds data=oreg_ds1;
>run;
>
> %end; %* MM month loop;
>
> %end; %* YY year loop;
>
>* Save results in final dataset;
>data &out_ds;
> set all_ds;
>run;
>
>%mend RRLOOP;
>
>
>* 3. Invoke the RRLOOP macro;
>%RRLOOP (year1= &start_year, year2= &end_year, nyear= &sample_years,
>in_ds=mydata, out_ds=&final_ds);
|