DATA Step, Macro, Functions and more

macro do problem

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

macro do problem

Hey guys,

What is wrong with my macro? Can't seem to run it, only get black text in the editor screen.

I have a dataset with one variable diff, and i wan't to find the constant growth for a subseries of windows of 8 observations.

For each regression i want to save the output residuals.

%macro lol;

%do i=1 %to 36;

proc reg data = data.test (firstobs = &i obs = &i + 7);

model dif = ;

output out = data.lol&i;

r = 1975 + &i

run;

%end;

%mend lol;

Thanks Smiley Happy


Accepted Solutions
Solution
‎01-13-2015 07:02 AM
Super User
Super User
Posts: 7,039

Re: macro do problem

So RESIDUALS is key word on the OUTPUT statement, not a separate statement.

It tells the proc what variable name to use for to store the residual value.  So again you cannot have a variable name that starts with a number or has a plus sign embedded in it.

proc reg data = data.test (firstobs = &i obs = %eval(&i + 7) );

model dif = ;

output out = data.lol&i

   r = res_%eval(1975 + &i)

;

run;

View solution in original post


All Replies
Super User
Posts: 19,770

Re: macro do problem

Your proc reg isn't valid SAS code.

Get a working version of your proc reg and then embed it in a loop.

Occasional Contributor
Posts: 10

Re: macro do problem

Could you elaborate?

the proc reg works without the macro and do loop.

Super User
Posts: 19,770

Re: macro do problem

Can you post a log of your reg code working? There is no R statement as far as I'm aware.

Super User
Super User
Posts: 7,039

Re: macro do problem

You cannot use OBS=1+7 in a dataset option.  You could use %EVAL() to have the macro processor convert that to 8 instead.

What is the R= statement in your PROC REG code? Are you trying to calculate a new variable?  Even if PROC REG allowed that it would be a constant for each regression and so not add anything to the regression model.

Occasional Contributor
Posts: 10

Re: macro do problem

Hey Tom,

You are right, i will try with the %eval.

The R = should give me a new variable for the residuals. It works when performing a single regression.

RESIDUAL | R=names

residuals, calculated as ACTUAL minus PREDICTED

Thanks.

Solution
‎01-13-2015 07:02 AM
Super User
Super User
Posts: 7,039

Re: macro do problem

So RESIDUALS is key word on the OUTPUT statement, not a separate statement.

It tells the proc what variable name to use for to store the residual value.  So again you cannot have a variable name that starts with a number or has a plus sign embedded in it.

proc reg data = data.test (firstobs = &i obs = %eval(&i + 7) );

model dif = ;

output out = data.lol&i

   r = res_%eval(1975 + &i)

;

run;

Occasional Contributor
Posts: 10

Re: macro do problem

Thanks a lot Tom.

Super User
Posts: 19,770

Re: macro do problem

3 main errors in your code:

1. &i+7 needs the %eval function to resolve

2. Proc Reg has no R= statement

3. Model Dep= results in an intercept only model, I think that would be the difference from the mean of the results.  Not sure what the need would be for that, but if its what your looking for I guess that's fine.

Occasional Contributor
Posts: 10

Re: macro do problem

Hey Reeza, appreciate the answers.

1) I will try and do that

2) It should give me the residuals -

    RESIDUAL | R=names

     residuals, calculated as ACTUAL minus PREDICTED

3) Yeah, i know, i actuallly only need the residuals from the regression, for further calculations.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 449 views
  • 1 like
  • 3 in conversation