SAS: Rolling window regressions for daily values [closed] - sas

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need rolling regression outputs on a daily basis. The dataset are stock returns and volume and the model is only return=volume.
I need the regression coefficient for the past 30 days, each day one day further such that the 30-day window stays intact.

Based on the question I understood that you want to run model for every month for a huge data set retrospectively. Here is something similar that I've done recently.
/*Select the parts of the data from base data based on input.*/
%macro model_by_dates(id, date1, date2);
data to_model;
set begin(where=( &date1. < date_var < &date2.));
run;
/*YOUR model code here*/
data result_file;
set From_model;
id=&id; /*To recognize from which model this is from.*/
run;
/*If you have huge files, do permanent data after every 10 loops or so. Depends.*/
proc append base=all_Results data=result_file force; run;
%mend model_by_dates;
/*here we create the dates list. You can do this in multiple ways. Datdiff maybe?*/
%let maxmonths=3;
data dates;
do i=0 to &maxmonths. by 1;
begin=date()-(i*30);
end = date()-( (i+1)*30);
output;
end;
run;
/*This is the master switch. It executes the macro for each row of the dates.*/
data _NULL_;
set dates;
call execute('%nrstr(%model_by_dates('
||strip(id)||','
||strip(begin)||','
||strip(end)||
'))');
run;
EDIT Based on further clarification I'm changing the date list creation:
data dates;
do i=0 to 30 by 1;
begin=date()-(i+30);
end = date()- i ;
output;
end;
run;

Related

2 ids with 3 distinct groups in SAS

hi I just wanted to know what would be the code in a data step for this outcome
I have the following:
and need the below:
please direct me to another page if this was answered previously...in addition how would I asked something like this in the future.
Thanks
Do you really need to do this in a data step? You might as well use proc sql for a Cartesian product.
proc sql;
create table want as
select code, groupds
from (select distinct code from have) a,
(select distinct groupds from have) b;
quit;
Here is how you can do it in a data step.
data want;
set have(keep=code where=(not missing(code)));
do i=1 to n;
set have(keep=groupds where=(not missing(groupds))) point=i nobs=n;
output;
end;
run;
The issue with this method is if you have a duplicate code or groupds a record will be created for the duplicate entry.

Is there a SAS function for removing respondents who are missing values for one question?

I'm trying to run analysis on a data set relating to a particular variable, but the question was only asked to half the respondents of the survey. Is there a function in SAS that would allow me to make a new data set from the current data set, but only including those who responded to the question of interest?
data responded_Question_42;
set survey_responses;
where not missing(Answer_42);
run;
You can also use the where clause directly at the time of analysis, for example:
Proc FREQ data=survey_responses;
where not missing(Answer_42);
table Answer_42 Answer_43 Answer_44;
run;

running a macro with each observation in sas [duplicate]

This question already has answers here:
Dynamically call macro from sas data step
(3 answers)
Closed 6 years ago.
I've got a macro which draws a pie chart for a provided ID. It basically chooses a row from a table, then transposes obtained one-row table and then draws a pie chart. It works perfectly fine if I call it for one observation (for ex. %StudPieChart(931123)). Here's the code:
%MACRO StudPieChart(id);
data projekt.temp;
set projekt.cwiczenia(keep=nrInd KOL1 KOL2 KOL3 aktywnosc where= (nrInd=&id));
drop nrInd;
run;
proc transpose data=projekt.temp out=projekt.temp;
run;
proc gchart data=projekt.temp;
pie _NAME_ / sumvar=COL1 percent=inside;
run;
%MEND;
Now I want draw a chart for not one, but some sample of observations. So I generated random sample and tried to run a macro in a data step. But it doesn't work anymore and I have no clue why.
Here's the rest of code:
proc surveyselect data=projekt.cwiczenia out=projekt.sample(keep=nrInd) sampsize=5 NOPRINT;
run;
data _NULL_;
set projekt.sample;
%StudPieChart(nrInd);
run;
You can use CALL EXECUTE.
data _NULL_;
set projekt.sample;
call execute('%nrstr(%StudPieChart('||nrInd||'));');
run;
RTM: http://support.sas.com/documentation/cdl/en/lefunctionsref/69762/HTML/default/viewer.htm#p1blnvlvciwgs9n0zcilud6d6ei9.htm

How can I calculate the average of multiple beta coefficients from a rolling window?

I'm a beginner to SAS (and stackexchange) and would really appreciate some assistance.
I currently have a regression setup in SAS which is a basic macro that runs the same single variable OLS model each year, using the prior 20 observations. That is, the year 1952 uses data from 1933 to 1952, and yields a beta for that period, with associated errors.
My goal is to calculate an average of these coefficients each decade, but I want to take into account the errors associated with each regression coefficient. Then for each decade i'd like to test whether the betas for the rolling window over that decade are significantly different from zero.
I currently have one large dataset which simply lists standard error, pvalue, beta and date, for every period. Below is a worked example of my simple code
%MACRO A_ROLLING (i);
proc reg data=dataset
outest=temp_dataset (drop=_model_) tableout;
model y = x / NOINT;
WHERE t >(1+&i) and t <(22+&i);
run;
data A_EQ1ROLLING;
set A_EQ1ROLLING temp_dataset;
run;
%mend
DATA A_EQ1ROLLING;
RUN;
%MACRO A_ROLL;
%do j=0 %to 64;
%A_ROLLING (&j);
%end;
%MEND;
%A_ROLL;

How to run the same SAS program with data from different months in one program rather than doing it separately for each month?

I need to rerun the same program with data from different months and create a separate excel spreadsheet for each month. What is a shorter way to program this in SAS than to run each program separately? For example in the following I read data from October, and at the end of the same program I output the October results to excel. I need to do the same for each month. Can I do it in one SAS program (maybe using Macro)? Thanks.
data sourceh.trades2;
set sourceh.trades1_october08_wk1;
if time<34200000 or time>57602000 then delete;
run;
proc export data=sourceh.avesymbol
outfile='C:\Documents and Settings\zd\My Documents\h\hdata\trades\2008\October 08 1 min correlations.xls'
replace;
run;
I would use a macro for that. Here I have wrapped your code into a macro which you can execute with the RunProgram(); macro statement for each desired month and year.
%MACRO RunProgram(month, year);
data sourceh.trades2;
set sourceh.trades1_&month.&year._wk1;
if time<34200000 or time>57602000 then delete;
run;
proc export data=sourceh.avesymbol
outfile="C:\Documents and Settings\zd\My Documents\h\hdata\trades\2008\&month. &year. 1 min correlations.xls"
replace;
run;
%MEND RunProgram;
%RunProgram(October, 08);
%RunProgram(November, 08);