BookmarkSubscribeRSS Feed
SeanZ
Obsidian | Level 7

I want to do a loop to do regression multiple times. Let me briefly talk about my case. I have a dataset with variables id,y, x1, x2, x3, ... x10 and t. id is stock id, y is return, x1...x10 are some factors, t is time. First I want to do a time series regression of y on x1 for each id (should have t months) to get coefficients of x1, then regress some change of y on the coefficients of x1 for each t (month). I need to repeat this for 10 times, x1 until x10. I wrote a program to do one rolling regression, but want to use a do loop to repeat from x1 to x10. I attach part of my code here. Please help me loop over x1 to x10 part. Thanks.

* Define independent and dependent variables;

%let x1=def;

%let x2=smb;

%let x3=hml;

%let x4=mom;

%let x5=def;

%let x6=term;

%let x7=div;

%let x8=inf;

%let x9=ip;

%let x10=pyrl;

%let y=ret;

data betas1; stop; run;

%macro tsreg; * time series regression;

%do m = 37 %to 216; * from month 37 to 216;

    data roll; set temp;

  if &m - 36 <= t < &m; * Past 36 months data, for example, for month 37, t=1-36;

  mon = &m; * Create a variable "mon" for each m loop;

  proc reg data=roll noprint outest=results EDF;

  model &y=&x1;

  by productreference mon;

  run;


  data betas1 ;

   set betas1 results;

  run;

%end;

%mend tsreg;

%tsreg

proc sort data = betas1;

by t;

run;

proc reg data = betas1 noprint outest = final;

model &y = &x1;

by t;

run;

4 REPLIES 4
Rick_SAS
SAS Super FREQ

Do you have missing values in these variables? If not, you don't need more macro loops. The REG procedure supports multiple MODEL statements.  Just list all of the explanatory variables on the VAR statement and then use multiple MODEL statements for your 10 variables within a single PROC REG call.  Here's an example:

proc reg data=sashelp.cars noprint outest=results;

var Cylinders EngineSize Length MPG_City Weight;

Cylinders:  model Horsepower = Cylinders;

EngineSize: model Horsepower = EngineSize;

Length:     model Horsepower = Length;

MPG_City:   model Horsepower = MPG_City;

Weight:     model Horsepower = Weight;

quit;

Look in the OUTEST= data set and you'll see the parameter estimates for each model.

Shashank7
Fluorite | Level 6
Hi Rick, referring to the above example, I have a similar situation to deal with. Just that I have more than 50 vars in the dataset and all of them are categorical.

I need to use one independent variable and the dependent variable in each iteration and I think leveraging macros to automate this step would be the thing to do here. If so, can you please help me as to how I should go about it? I’m having a hard time to do this. Would appreciate your thoughts. Thanks. 🙂
Rick_SAS
SAS Super FREQ

See the article "An easy way to run thousands of regressions in SAS," which compares the macro loop and a BY group analysis. If you have categorical variables you will need to use PROC GLM and include a CLASS statement. If you run into trouble, start a new thread and post example data and code.

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 4277 views
  • 4 likes
  • 4 in conversation