macro regression

Reply
Contributor
Posts: 23

macro regression

Hi,

I am trying to run regression using macro. Time period for my data is quater and it (DATAQC  is YYQC6. format).

I ran below codes and received error messages. How can I correct errors?

ERROR: Required operator not found in expression: intnx(quarter,2001:1,'same')

ERROR: %EVAL function has no expression to evaluate, or %IF statement has no condition.

ERROR: The %FROM value of the %DO MQUARTER loop is invalid.

ERROR: Required operator not found in expression: intnx(quarter,2011:4,'same')

ERROR: %EVAL function has no expression to evaluate, or %IF statement has no condition.

/*pooled cross-sectional regressions using previous 10 quarter data*/

%let begquarter=2001:1 ;

%let endquarter=2011:4 ;

%macro coeff;

%do mquarter=%eval(intnx('quarter',&begquarter,'same'))%to %eval(intnx('quarter',&endquarter,'same'));

/*sample of past 10 quarter to estimate regression coeff to
estimate earnings t+1*/

data regdata; set comp_winsorized;

if  (DATAQC >= %eval(&mquarter-10))
and (DATAQC<
%eval(&mquarter));

/* estimate earning t+1 coefficient*/

proc reg data=regdata outest=est noprint;

model e1 = atq d dd e nege ac/ADJRSQ;

quit;

data e1&mquarter; set est;

a0=intercept; a1=atq; a2=d; a3=dd; a4=e; a5=nege; a6=ac;
aajrs=_ADJRSQ_;

DATAQC=&mquarter;

keep DATAQC a0 a1 a2 a3 a4 a5 a6 aajrs;

%end;

;

%mend;

%coeff;

Thank you!

Super User
Super User
Posts: 6,383

Re: macro regression

1) If you want to call a function in macro code then you need to use %SYSFUNC().  %EVAL() is just for doing integer arithmetic.

2) When using %SYSFUNC() to call a function do not put quotes unless they are actually part of the value.

3) Date literals are of the form DDMMMYYYY.

4) Use an integer to control your loop since "quarters" are not an easy increment to use in a DO loop.

%let begquarter='01JAN2001'd ;

%let endquarter='01DEC2011'd;

%do i=0 %to %sysfunc(intck(quarter,&begquarter,&endquarter));

%let mquarter="%sysfunc(intnx(quarter,&begquarter,&i,same),date9)"d;


Contributor
Posts: 23

Re: macro regression

Thanks Tom.

However, I am stuck with if statement in proc regression. It is a rolling regression of interval of 10 quarters.

datadate is in a date format.

I can run macro, but I keep getting Syntax error messages

%let begquarter='01JAN2001'd ;

%let endquarter='01DEC2011'd;

%macro coeff;

%do i=0%to %sysfunc(intck(quarter,&begquarter,&endquarter));

%let mquarter="%sysfunc(intnx(quarter,&begquarter,&i,same),date9)"d;

data regdata; set comp_winsorized;

if  (DATADATE >= "%sysfunc(intnx(quarter,&mquarter,-10,same),date9)"d and (DATADATE< "%sysfunc
(intnx(quarter,&mquarter,-0,same),date9)"d
);

proc reg data=regdata outest=est noprint;

model e1= atq d dd e nege ac/ADJRSQ;

quit;

data e1&mquarter; set est;

a0=intercept; a1=atq; a2=d; a3=dd; a4=e; a5=nege; a6=ac; aajrs=_ADJRSQ_;

DATADATE=&mquarter;

keep DATADATE a0 a1 a2 a3 a4 a5 a6 aajrs;

run;




Super User
Super User
Posts: 6,383

Re: macro regression

Probably because of the nested quotes.  You don't have to format the dates, you can just pass on the regular integer number of days since 1/1/1960.  That will eliminate the nested quotes.

For example you could just make this change to the IF statement.

if  (DATADATE >= %sysfunc(intnx(quarter,&mquarter,-10,same))

and (DATADATE < %sysfunc(intnx(quarter,&mquarter,-0,same)));

PS You could eliminate the REGDATA data step and instead just run the regression on the original data set with a WHERE statement.

proc reg data=comp_winsorized outest=est noprint;

  where (DATADATE >= %sysfunc(intnx(quarter,&mquarter,-10,same))

    and (DATADATE < %sysfunc(intnx(quarter,&mquarter,-0,same)));

  model e1= atq d dd e nege ac/ADJRSQ;

run; quit;


data e1&mquarter;

  set est;

  a0=intercept; a1=atq; a2=d; a3=dd; a4=e; a5=nege; a6=ac; aajrs=_ADJRSQ_;

  DATADATE=&mquarter;

  keep DATADATE a0 a1 a2 a3 a4 a5 a6 aajrs;

run;

Contributor
Posts: 23

Re: macro regression

I am keeping getting errors.

My log says

ERROR: Expected close parenthesis after macro function invocation not found.

22: LINE and COLUMN cannot be determined.

ERROR 22-322: Syntax error, expecting one of the following: a name,  a quoted string, (, /, ;, _DATA_, _LAST_, _NULL_.

ERROR 200-322: The symbol is not recognized and will be ignored

NOTE: Line generated by the macro variable "MQUARTER".
1     e3"01JAN2001"d
      -----------

       22

       200

I don't know how to fix. Would you help me please?

this is my code.

/*pooled cross-sectional regressions using previous 10 years data*/

%let begquarter="01JAN2001"d ;

%let endquarter="01DEC2011"d;

%macro coeff;

%do i=0%to %sysfunc(intck(quarter,&begquarter,&endquarter));

%let mquarter="%sysfunc(intnx(quarter,&begquarter,&i,same),date9)"d;

/*Estimate earning t+1 coefficient*/

proc reg data=icc.comp_winsorized_qr outest=est noprint;

     where DATADATE >= %sysfunc(intnx(quarter,&mquarter,-10,same)and
     DATADATE <
%sysfunc(intnx(quarter,&mquarter,-0,same));

     model e1 = atq d dd e nege ac/ADJRSQ;

quit;

data e1&mquarter; set est;

      a0=intercept; a1=atq; a2=d; a3=dd; a4=e; a5=nege;a6=ac; aajrs=_ADJRSQ_;

DATADATE=&mquarter;

      keep DATADATE a0 a1 a2 a3 a4 a5 a6 aajrs;

run;

/*Estimate earning t+2 coefficient*/

proc reg data=icc.comp_winsorized_qr outest=est noprint;

      where DATADATE >= %sysfunc(intnx(quarter,&mquarter,-11,same)and DATADATE < %sysfunc(intnx(quarter,&mquarter,-1,same));

      model e2 = atq d dd e nege ac/ADJRSQ;

quit;

data e2&mquarter;  set est;

      b0=intercept; b1=atq; b2=d; b3=dd; b4=e; b5=nege;b6=ac; bajrs=_ADJRSQ_;

DATADATE=&mquarter;

      keep DATADATE b0 b1 b2 b3 b4 b5 b6 bajrs;

run;

/*Estimate earning t+3 coefficient*/

proc reg data=icc.comp_winsorized_qr outest=est noprint;

      where DATADATE >= %sysfunc(intnx(quarter,&mquarter,-12,same) and DATADATE < %sysfunc(intnx(quarter,&mquarter,-2,same));

      model e3 = atq d dd e nege ac/ADJRSQ;

quit;

data e3&mquarter ; set est;

c0=intercept; c1=atq; c2=d; c3=dd; c4=e; c5=nege;c6=ac; cajrs=_ADJRSQ_;

DATADATE=&mquarter;

      keep DATADATE c0 c1 c2 c3 c4 c5 c6 cajrs;

run;

data e&mquarter; merge e1&mquarter e2&mquarter e3&mquarter;

%end;

data coeff; set

%do i=0%to %sysfunc(intck(quarter,&begquarter,&endquarter));

e&mquarter;

%end;;

proc sort data=coeff; by DATADATE;run;

%mend;

%coeff;

Super User
Super User
Posts: 6,383

Re: macro regression

Dataset names with quotes in them will not work. Would just be easier to use the index variable I to generate the data set name.

data e1&i ;

Also change in other places where you are making data set names.

Or you could remove the quotes from the macro variable value.

In reality there is no need to make the macro variable MQUARTER look like a date if you don't want.  You could just add &I into the interval requested in the WHERE clause and base the interval calculations from the BEGQUARTER macro variable instead.  You could also create the variable DATADATE as character in your summary statistics tables.


%let mquarter=%sysfunc(intnx(quarter,&begquarter,&i,same),yyq6);

...

     where %sysfunc(intnx(quarter,&begquarter,&i-11,same) <= DATADATE < %sysfunc(intnx(quarter,&begquarter,&i-1,same));

...

data e1&mquarter

...

DATADATE="&mquarter";

Ask a Question
Discussion stats
  • 5 replies
  • 627 views
  • 3 likes
  • 2 in conversation