Hi everyone,
I'm trying to get weekly returns (Monday to Friday) from my daily returns data set, for each week and each company over my sample period. Can anyone help me with that? (see example below of my data set with two companies, just for one week). Hope that's enough, let me know if you need more info. Thanks!
PERMNO DATE RET
10158 28/04/2014 -0.01
10158 29/04/2014 -0.006
10158 30/04/2014 0.006
10158 01/05/2014 -0.009
10158 02/05/2014 0.002
10234 28/04/2014 -0.02
10234 29/04/2014 -0.005
10234 30/04/2014 0.001
10234 01/05/2014 -0.007
10234 02/05/2014 0.003
Just a small modification.
data WANT;
set HAVE;
by PERMNO notsorted;
retain CUMUL;
if first.PERMNO or weekday(DATE)=2 then CUMUL=1+RET;
else CUMUL=CUMUL*(1+RET);
if last.PERMNO or weekday(DATE)=6 then do;
CUMUL=CUMUL-1;
output;
end;
drop RET;
run;
PERMNO | DATE | CUMUL |
---|---|---|
10158 | 19845 | -0.016983 |
10158 | 19852 | -0.041954 |
10234 | 19845 | -0.027850 |
Like this (Assuming all dates must be used since that's the data provided)?
data WANT;
set HAVE;
by PERMNO notsorted;
retain CUMUL;
if first.PERMNO then CUMUL=1+RET;
else CUMUL=CUMUL*(1+RET);
if last.PERMNO then do;
CUMUL=CUMUL-1;
output;
end;
drop DATE RET;
run;
PERMNO | CUMUL |
---|---|
10158 | -0.016983 |
10234 | -0.027850 |
Do you want calendar weeks or just 7 days? Does what is a week for one PermNo be the same week for another PERMNO or can they use different days because say they start on different dates? I think you'll need to expand your example data.
I have attached a text file with one company only. Each week is assigned a number and I just need the weekly returns for each one of them. I have other companies too with returns in the same weeks as the one attached so it needs by week and by permno and by year as well cause you'll find the same number assigned to a week every year. Does this help?
Just a small modification.
data WANT;
set HAVE;
by PERMNO notsorted;
retain CUMUL;
if first.PERMNO or weekday(DATE)=2 then CUMUL=1+RET;
else CUMUL=CUMUL*(1+RET);
if last.PERMNO or weekday(DATE)=6 then do;
CUMUL=CUMUL-1;
output;
end;
drop RET;
run;
PERMNO | DATE | CUMUL |
---|---|---|
10158 | 19845 | -0.016983 |
10158 | 19852 | -0.041954 |
10234 | 19845 | -0.027850 |
Assuming data is sorted by company and date try next code:
data temp;
set have;
by premno date;
retain week_count 0 week_total 0;
if first.premno then do; week_count = 0; week_total = 0; end;
if weekday(date) = 1 /* assuming monday */
then do; week_count + 1; week_total = 0; end;
week_total = week_total + ret;
run;
data want;
set temp;
by premno week_count;
if last.wee_count;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.