turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- how to do a loop(do) in SAS

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-09-2013 03:24 PM

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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to SeanZ

07-09-2013 03:50 PM

Here's a perfect example of what you want to do:

http://www.ats.ucla.edu/stat/sas/seminars/sas_macros_introduction/

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to SeanZ

09-03-2014 02:22 PM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to SeanZ

01-31-2018 06:36 PM

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.

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Shashank7

02-01-2018 08:58 AM

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.