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

Dear Madam/Sir,

I try one example code to construct annual buy and hold return (returns for eleven months ahead of closing  month plus return in closing month) using monthly return. 

It will be grateful if anybody can advise me how to fix the following error in do loop. The data set is attached. Thank you. Joon1

 

data c4;
set c3;
by permno;
if _n_=1 then do until (end_of_annualreturn);
set annualbhr end=end_of_annualreturn;

ERROR: File WORK.ANNUALBHR.DATA does not exist.

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
joon1
Quartz | Level 8

Thank you very much to all.

 

The following code works well.

data c4;
set c3;
by permno;
do n=1 by 1 until(not missing(closemonth));
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{*}))-1;
end;
run;

 

 

 

View solution in original post

6 REPLIES 6
ballardw
Super User

It is pretty clear: your work library does not have a data set named ANNUALBHR.

 

Check spelling of the name, or if the set is in a different library use the <lib>.datasetname syntax.

 

If you created the data set in a previous SAS session then you likely need to recreate it as the Work library is cleared out at the end of a SAS session.

joon1
Quartz | Level 8

Thanks for your prompt reply, ballardw. Annualbhr is not library name created in the previous step. It is the variable name that I want to construct as shown at the last line in the program.

I am not familiar with macro do-loop logic, so I posted this question in the forum.
 
I am not asking the interpretation of error. My question was how to construct do loop from month 1 (-11 month) to month 12 (closing month).
 
(1+ret)(1+ret)----(1+ret(closing month)-1=annualbhr
 
Any help will be appreciated.
Thanks
Joon1
LeonidBatkhan
Lapis Lazuli | Level 10

Hi joon1,

When you write in your code

 

set annualbhr end=end_of_annualreturn;

SAS interprets that as you are reading data table named WORK.ANNUALBHR (statement SET is used for reading data tables). Since this data table does not exist you get that ERROR: File WORK.ANNUALBHR.DATA does not exist.

 

There is nothing wrong with your array named annualbhr.

Hope this helps.

Regards,

Tom
Super User Tom
Super User

Looks like you probably just need to read until you see the observation with CLOSEMONTH non-missing.

data want ;
  do n=1 by 1 until(not missing(closemonth));
     logplusone=log(1+ret);
     sumlogplusone=sum(0,sumlogplusone,logplusone);
  end;
  if n=12 then annualbhr =exp(sumlogplusone));
  drop rev;
run;
joon1
Quartz | Level 8

Thanks, Tom, for your suggesion.

However, your programming makes sas program running for a couple of hours without producing output. It seems that there is a indefinite loop in your code. What does "drop rev" indicate? Thanks, Joon1.

joon1
Quartz | Level 8

Thank you very much to all.

 

The following code works well.

data c4;
set c3;
by permno;
do n=1 by 1 until(not missing(closemonth));
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{*}))-1;
end;
run;

 

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 683 views
  • 0 likes
  • 4 in conversation