Dear Madam/Sir,
I would like to construct annual buy and hold returns in each fiscal year (eleven month ahead of closemonth and closemonth) using monthly return files and try an example code in this forum.
I see the following error message. Any help will be highly appreciated. Thanks. Joon1.
data c4;
set c3;
by permno;
if _n_=1 then do until;
ERROR 22-322: Syntax error, expecting one of the following: (, =.
ERROR 76-322: Syntax error, statement will be ignored.
array {1979:2019,1:12} _temporary_ ;
ERROR: Undeclared array referenced: array.
ERROR 22-322: Syntax error, expecting one of the following: <, <=, =, >, >=, EQ, GE, GT, LE, LT,
NE, NG, NL, ^=, ~=.
ERROR 76-322: Syntax error, statement will be ignored.
array logplusone {12} _temporary_;
y=year(date);
m=month(date);
logplusone{m}=log(1+ret);
if closemonth^=.;
if n(of logplusone{*})=12 then annualbhr=exp(sum(of logplusone{*}));
run;
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.C4 may be incomplete. When this step was stopped there were 0
observations and 34 variables.
The data form is below.
1 | 10001 | 19900131 | . | -0.0185 |
2 | 10001 | 19900228 | . | -0.0063 |
3 | 10001 | 19900330 | . | 0.01266 |
4 | 10001 | 19900430 | . | 0 |
5 | 10001 | 19900531 | . | -0.0127 |
6 | 10001 | 19900629 | . | 0.0141 |
7 | 10001 | 19900731 | . | 0.02564 |
8 | 10001 | 19900831 | . | -0.05 |
9 | 10001 | 19900928 | . | 0.04079 |
10 | 10001 | 19901031 | . | -0.0128 |
11 | 10001 | 19901130 | . | 0 |
12 | 10001 | 19901231 | 12 | 0.0013 |
permno | date | closemonth | ret |
The problem may be caused by skipping the condition in the until-statement in
if _n_=1 then do until;
Try something like
if _n_ = 1 then do;
do until (INSERT_CONDITION);
...
end;
end;
To increase the readability of you post, please use "insert SAS code" (the running man icon) and "insert code" for logs. Posting data in usable form is highly recommended if you want/expect tested code in answers.
Thank you for your reply, Andreas.
I used the following code and test data is attached (csv file).
data c4;
set c3;
by permno;
if _n_=1 then do until (end_of_annualbhr);
set ret end=end_of_annualbhr;
array annualbhr {1990:2019,1:12} _temporary_ ;
array logplusone {12} _temporary_;
y=year(date);
m=month(date);
logplusone{m}=log(1+ret);
if closemonth^=.;
if n(of logplusone{*})=12 then annualbhr=exp(sum(of logplusone{*}));
end;
run;
And I had the following error message.
241 data c4;
242 set c3;
243 by permno;
244 if _n_=1 then do until (end_of_annualbhr);
245 set ret end=end_of_annualbhr;
ERROR: File WORK.RET.DATA does not exist.
246 array annualbhr {1990:2019,1:12} _temporary_ ;
247 array logplusone {12} _temporary_;
248 y=year(date);
249 m=month(date);
250 logplusone{m}=log(1+ret);
251 if closemonth^=.;
252 if n(of logplusone{*})=12 then annualbhr=exp(sum(of logplusone{*}));
ERROR: Illegal reference to the array annualbhr.
253 end;
254 run;
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.C4 may be incomplete. When this step was stopped there were 0
observations and 33 variables.
Any help will be highly appreciated.
Thank you
Joon1
In the line
if n(of logplusone{*})=12 then annualbhr=exp(sum(of logplusone{*}));
you are using annualbhr as variable, but it is an array, so you have to change it to something like
if n(of logplusone{*})=12 then annualbhr{y,m}=exp(sum(of logplusone{*}));
Thanks for your help. I tried the following code and still have an error message. It will be appreciative if you can advise me how to make do loop in the first two lines of the program. Thanks.
data c4;
set c3;
by permno;
if _n_=1 then do until (end_of_annualreturn);
set annualreturn end=end_of_annualreturn;
array annualbhr {1990:2019,1:12} _temporary_ ;
array logplusone {12} _temporary_;
y=year(date);
m=month(date);
logplusone{m}=log(1+ret);
if closemonth^=.;
if n(of logplusone{*})=12 then annualbhr{y,m}=exp(sum(of logplusone{*}));
end;
run;
error message
435 data c4;
436 set c3;
437 by permno;
438 if _n_=1 then do until (end_of_annualreturn);
439 set annualreturn end=end_of_annualreturn;
ERROR: File WORK.ANNUALRETURN.DATA does not exist.
440 array annualbhr {1990:2019,1:12} _temporary_ ;
441 array logplusone {12} _temporary_;
442 y=year(date);
443 m=month(date);
444 logplusone{m}=log(1+ret);
445 if closemonth^=.;
446 if n(of logplusone{*})=12 then annualbhr{y,m}=exp(sum(of logplusone{*}));
447 end;
448 run;
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.C4 may be incomplete. When this step was stopped there were 0
observations and 33 variables.
WARNING: Data set WORK.C4 was not replaced because this step was stopped.
Please post logs using the "insert code" button and code using the "insert sas code" button, won't debug unformatted code. But the error-message you get is quite easy to understand: ERROR: File WORK.ANNUALRETURN.DATA does not exist.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.