BookmarkSubscribeRSS Feed

How do I write a macro to ... run multiple regressions?

Started ‎02-28-2015 by
Modified ‎06-05-2018 by
Views 5,591

This is part three of an ongoing series into how to accomplish a task WITHOUT using a macro.

In this post, I go over one method to run multiple regressions with multiple dependent variables.

 

This is based on a question that was recently asked on the communities forum:

 https://communities.sas.com/t5/SAS-Procedures/Macro-for-multiple-GARCH-Regressions/m-p/184279/highli...

 

For example consider if you have 10 dependent variables Y1-Y10 and you want to run regressions against 3 independent variables X1-X3

 

EDIT: Rick Wicklin (a SAS employee) has published an blog post that has a fully worked example with code here:

https://blogs.sas.com/content/iml/2017/02/13/run-1000-regressions.html

 

 

Sample data:

 

Y1 Y2 Y3 Y4 Y5 Y6 Y7 Y8 Y9 Y10 X1 X2 X3

 

 

 

This would result in a PROC GLM model that looked like the following:

 

 

 

PROC GLM DATA=sample;
model y1 = x1 x2 x3;
run;

 

 

 

One method is to write a macro to loop through all the different Y variables using the above PROC GLM code. Another is to transpose your data to a different format that then allows you to use the BY group processing available in most PROCS.

 

Output data:

 

 

 

Y_Index Y X1 X2 X3
1   Y1
2   Y2
..
10  Y10
1   Y1
2   Y2
..
10  Y10

 

 

Then the PROC GLM code becomes (after a sort):

 

 

 

PROC SORT data=sample_transpose;
by Y_index;
run;
 
PROC GLM DATA=sample;
BY Y_Index;
model y= x1 x2 x3;
run;

 

 

This runs all 10 models at once. Additionally, if you're capturing model estimates via an output or ODS statement they're all included in the same table and there is no need to append different tables from each model.

 

Hope you found this useful!

Comments
Version history
Last update:
‎06-05-2018 03:15 PM
Updated by:
Contributors

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started

Article Labels
Article Tags