Turn on suggestions

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

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- Rolling regression using macro

Options

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 11-12-2017 06:25 AM
(1631 views)

The format of the data set: ID Date Year Month Day Variable1 Variable2

The dataset has the daily observations for each ID.

For **each ID each month**, I want to run a regression of Variable 1 on variable 2 based on **recent 6-month daily observations**.

How to use the macro to run this regression? It is more complicated than the monthly rolling regression.

-------------------------------------------------------------------------

If I do some manipulation and get the following format: IDNAME Count Variable1 Variable2

Here 'Count' measures the date from 1 to a number based on date of each ID (for example, 120). Each ID has different number of count.

Now I run a regression of Variable1 on Variable2 using only 6 counts, i.e., 1-6 for the first regression, 2-7 for the second, 3-8,....

How to write this macro for the regression. I am new to the macro. Thanks.

proc reg data=one noprint outest=results;

model variable1=variable2;

by IDNAME count;

run;

8 REPLIES 8

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

@ZZB wrote:

The format of the data set: ID Date Year Month Day Variable1 Variable2

The dataset has the daily observations for each ID.

For

each ID each month, I want to run a regression of Variable 1 on variable 2 based onrecent 6-month daily observations.How to use the macro to run this regression? It is more complicated than the monthly rolling regression.

Thanks very much!

I doubt someone here will write such a macro for you.

The general steps, for you to try, are:

- select the proper 6 months of data from original data set
- perform regression
- store results
- repeat with next six month period

--

Paige Miller

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

If I do some manipulation and get the following format: IDNAME Count Variable1 Variable2

Here 'Count' measures the date from 1 to a number based on date of each ID (for example, 120). Each ID has different number of count.

Now I run a regression of Variable1 on Variable2 using only 6 counts, i.e., 1-6 for the first regression, 2-7 for the second, 3-8,....

How to write this macro for the regression. I am new to the macro. Thanks.

proc reg data=one noprint outest=results;

model variable1=variable2;

by IDNAME count;

run;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

I don't see where you have asked a specific question.

You have asked for help writing a macro, but we don't know at what point in the process you are having trouble. Please be specific.

--

Paige Miller

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

I totally have no idea on writing the macro in this case.

For example, in the second case, I do not know what is wrong with the following code

%macro estim

%do x=6 to %to count; /* count varies for each IDNAME, some ID has a number of 100, some ID is 156 */

data temp;

set one;

if &x-5<=count<=&x;

per=&x;

proc reg data=temp noprint outest=results;

model variable1=variable2;

by IDNAME per;

run;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

So it would help if you showed the SASLOG as well as the code.

Nevertheless, a few things stand out.

semicolon is needed at the end of

%macro estim;

You need to use macro constructs in the %do statement

%do x=6 %to &count;

In order for this to work, you have to define what value &count has, I assume this is based upon the number of data points you have.

After the last line of code, you need to end the macro with %mend; and then call the macro.

%mend; %estim

--

Paige Miller

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Yet you have written code that does not include the variable ID.

You can use PROC SUMMARY to determine the maximum value of count for each ID. You can use the PROC SUMMARY results to determine the value of the macro variable &COUNT. Or you could do this via PROC SQL.

The more I think about it, the more I think you should be doing this without macros, using data step code, PROC SORT and a BY statement in PROC REG.

--

Paige Miller

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

1. Start by getting regression running for a single model

2. Expand your model and run it for three iterations.

3. Look at what's changed between 1 & 2 and that will tell you what you need to change for your model.

You can see some examples of this here: https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/

And this is a commonly asked question, so if you search you should find results here or on lexjansen.com

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. **Registration is now open through August 30th**. Visit the SAS Hackathon homepage.

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.