BookmarkSubscribeRSS Feed
Almutairi
Fluorite | Level 6

Hi,

I have the following code and I am trying to calculate the weekly stock return of firm i from the last trading day in week t-1 to the last trading day in week t. According to my data, the week starts Sunday and ends Thursday. Unfortunately, the code does not give me the right output. Also, it does not give me the cumulative returns for each firm per week.

Your assistance is highly appreciated.

 

data Returns_1;
set Returns;
by stk date;
retain week_count 0 week_total 0;
if first.stk then do; week_count = 0; week_total = 0; end;

if weekday(date) = 1 /* assuming sunday */
then do; week_count + 1; week_total = 0; end;
week_total = week_total + ret;
run;


data Returns_2;
set Returns_1;
by stk week_count;
if last.week_count;
run;

 

 

2 REPLIES 2
mkeintz
PROC Star

Please provide, in the form of a working data step, a sample of the data you are starting with, and the erroneous results your program generates from that sample.

 

Help us help you.

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

You may be doing a lot of extra work.

If you apply the correct format to a date value you get a calendar week. So if you want a total of some value by calendar week you might try

proc summary data=Returns nway;
   class  stk date;
   format date weeku5.;
   var ret;
   output out=work.weeksummary (drop=_type_) sum=Week_total;
run;

The weekU format will display values in yyWww, 21W03 would be the third week of 2021 for example.

Formats can create groups that will be honored by the analysis, reporting and graphing procedures (most of the time).

The underlying value of the date variable in the output will be the first date of the week encountered in the data so would mostly be Mondays except for days there is no data. You can check that by printing a few records with a different format.

If you do not like the appearance change the format. You can also create a custom date format with Proc Format and a Picture statement if you want the year/ week combination displayed differently.

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
  • 2 replies
  • 357 views
  • 0 likes
  • 3 in conversation