BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Raphaellima
Calcite | Level 5

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
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

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) .

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 915 views
  • 1 like
  • 3 in conversation