BookmarkSubscribeRSS Feed
ZZB
Obsidian | Level 7 ZZB
Obsidian | Level 7

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
PaigeMiller
Diamond | Level 26

@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 on recent 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:

 

  1. select the proper 6 months of data from original data set
  2. perform regression
  3. store results
  4. repeat with next six month period
--
Paige Miller
ZZB
Obsidian | Level 7 ZZB
Obsidian | Level 7

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;

PaigeMiller
Diamond | Level 26

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
ZZB
Obsidian | Level 7 ZZB
Obsidian | Level 7

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;

 

 

PaigeMiller
Diamond | Level 26

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
ZZB
Obsidian | Level 7 ZZB
Obsidian | Level 7

Thanks! But the problem is how to define the "count", which varies for each ID. Could you give an example of code in this case?

PaigeMiller
Diamond | Level 26

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
Reeza
Super User

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

 

 

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
  • 8 replies
  • 1420 views
  • 0 likes
  • 3 in conversation