DATA Step, Macro, Functions and more

2 Syntax Errors

Accepted Solution Solved
Reply
Learner
Posts: 1
Accepted Solution

2 Syntax Errors

Hi, I am doing a momentum strategy research, and I am running into these errors when running a procedure (SAS Studio):

 

ERROR 22-322: Syntax error, expecting one of the following: ;, !, !!, &, *, **, +, -, /, <, <=, <>, =, >, >=, AND, EQ, EQT, EXCEPT,
GE, GET, GROUP, GT, GTT, HAVING, INTERSECT, LE, LET, LT, LTT, NE, NET, NOT, OR, ORDER, OUTER, UNION, ^, ^=, |, ||, ~,
~=.
 
ERROR 76-322: Syntax error, statement will be ignored.
 
 
The procedure:
 
proc sql;
create table umd
as select distinct a.permno, a.date, b.date as date1 label='sort_date', exp(sum(log(1+b.ret))) - 1 as cmret, b.ret
from commonstocks (keep=permno date) as a, commonstocks as b
where a.permno=b.permno and 0<=intck('month', b.date, a.date)<&J
group by a.permno, a.date
having count(b.ret)=&J;
quit;
 
Thanks in advance

Accepted Solutions
Solution
‎12-05-2016 11:34 AM
Super User
Posts: 10,527

Re: 2 Syntax Errors

In the future please post the code and error message from the log and paste into a code box (either the {i} or run icon) to preserve formatting. The ERROR 22-322 is usually accompanied by an underscore under the postion in code that triggered the error message. Posting into a code box will maintain the orginal formatting and allow us to see where the underscore is in the code.

 

Also the values of macro variable resolution help, so run the code with Options Mprint; so the log will show the code generated by the macro variable(s).

 

I will say it looks a tad odd to me comparing the same value, &J, to both a number of months and another count (which might need CALCULATED to resolve in the HAVE clause) .

View solution in original post


All Replies
Valued Guide
Posts: 797

Re: 2 Syntax Errors

[ Edited ]

You want rolling J-month cumulative returns for each permno. 

 

It looks to me like you are usuing the CRSP monthly returns data, not the daily returns.

 

Given that CRSP data is almost always sorted by permno/date, you can use a DATA step to process the data much faster.  This program untested program makes the following assumptions.

 

  1. data sorted by permno/date
  2. the data are monthly
  3. RET is never missing
  4. The second example works even when there are "holes" (i.e. missing months between the beginning and end of a permno).
  5. Your data has no more than 241 months for any given permno

 

 

data umd (drop=month0_date m);
  set commonstocks;
  by permno;

  /* define  array to hold cumulative log(1+ret) starting month 0 up to 20 years*/
  array sumlog_retplus1 {0:240} _temporary_ ();

  if first.permno then do;
    month0_date=date;
    do m=0 to 240; sumlog_retplus1{m}=0; end;
  end;
  retain month0_date;

  m=intck('month',month0_date,date);
  if m=0 then sumlog_retplus1{m}=log(1+ret);
  else        sumlog_retplus1{m}=log(1+ret)+sumlog_retplus1{m-1};

  if m<&J then delete;
  else if m=&J then cmret=exp(sumlog_retplus1{m})-1;
  else              cmret=exp(sumlog_retplus1{m}-sumlog_retplus1{m-&J})-1;
run;

 

The second version accomodates holes in the monthly series:

 

data umd (drop=month0_date m m2 lastm);
  set commonstocks;
  by permno;

  /* define  array to hold cumulative log(1+ret) starting month 0 up to 20 years*/
  array sumlog_retplus1 {0:240} _temporary_ ();

  if first.permno then do;
    month0_date=date;
    do m=0 to 240; sumlog_retplus1{m}=0; end;
  end;
  retain month0_date;

  m=intck('month',month0_date,date);

  lastm=lag(m);

  if m=0 then sumlog_retplus1{m}=log(1+ret);
  else if m=lastm+1 then sumlog_retplus1{m}=log(1+ret)+sumlog_retplus1{m-1};
  else do m2=lastm+1 to m-1;
    else sumlog_retplus1{m2}=sumlog_retplus1{m2-1};
  end;

  if m<&J then delete;
  else if m=&J then cmret=exp(sumlog_retplus1{m})-1;
  else              cmret=exp(sumlog_retplus1{m}-sumlog_retplus1{m-&J})-1;
run;
Solution
‎12-05-2016 11:34 AM
Super User
Posts: 10,527

Re: 2 Syntax Errors

In the future please post the code and error message from the log and paste into a code box (either the {i} or run icon) to preserve formatting. The ERROR 22-322 is usually accompanied by an underscore under the postion in code that triggered the error message. Posting into a code box will maintain the orginal formatting and allow us to see where the underscore is in the code.

 

Also the values of macro variable resolution help, so run the code with Options Mprint; so the log will show the code generated by the macro variable(s).

 

I will say it looks a tad odd to me comparing the same value, &J, to both a number of months and another count (which might need CALCULATED to resolve in the HAVE clause) .

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 191 views
  • 1 like
  • 3 in conversation