BookmarkSubscribeRSS Feed
SAM_22
Fluorite | Level 6

Hello, i would like to form transaction based calendar time portfolios. The portfolios are formed by mimicking daily trades (adding and dropping stocks after certain holding period, e.g. a month).

I have trading data with many buy and sell trades in a day. If i understand it correctly, this is how daily portfolios are constructed:

 

Day 1 (Jan 1): calculate the average return of stocks bought by investors in the first day.

Day 2 (Jan 2): calculate the average return of stocks bought in day 1&2.

Day 3 (Jan 3): calculate the average return of stocks bought in day 1, 2 & 3.

Day 4 (Jan 4): and so on.

After a month (e.g. Feb 1), all the stocks from day 1 must be dropped, while adding new stocks from today's trade.

On the next day, stocks from Feb 2 are dropped, while adding new ones from this day.

On Feb 3, stocks from Jan 3 are dropped, while adding new ones from this day. and so on;

 

The results should be like this;

 

DatePortfoliosStocks boughtportfolio return
Jan-01Portfolio day 1stock 1, stock 2, stock 3average return (stock 1,2&3)
Jan-02Portfolio day 2stock 4, stock 5, stock 6average return (stock 1,2,3,4,5,&6)
Jan-03Portfolio day 3stock 7, stock 8, stock 9average return (stock 1,2,3,4,5,6,7,8&9)
 . continue wih the same pattern
 . .
 . .
After one month (assuming 20 trading days in a month)
    
Feb-01Portfolio day 21 (assuming  20 trading days in a month) stock 10, stock 11, stock 12average return (stock 4,5,6,7,8,9,10,11&12) stocks 1,2,&3 are dropped
Feb-02Portfolio day 22stock 13, stock 14, stock 15average return (stock 7,8,9,10,11,12,13,14&15) stocks 4,5,&6 are dropped
Feb-03Portfolio day 23stock 16, stock 17, stock 18average return (stock 10,11,12,13,14,15,16,17,&18) stocks 7,8,&9 are dropped
   and so on

 

So far i only know the codes for regular moving average, and do not know how to do it like this.

Any help would be greatly appreciated. Thank you in Advance!

 

3 REPLIES 3
Satish_Parida
Lapis Lazuli | Level 10
Can you post how the data looks like, using data step datalines. Then it would be easy for us to provide some sort of result.
SAM_22
Fluorite | Level 6

Thanks for replying. I attached my data sample:

I just want to average returns daily.

day 1: average returns in day 1

day 2: average returns in day 1&2;

day 3: average returns in day 1,2&3;

but after being hold for a month, the stocks must be dropped, just like the way i explained before.

It doesnt need to be exact jan 1 to feb 1 it can be interval like 20 or 30 days

 

andreas_lds
Jade | Level 19

Please post the data in usable form, an excel-file is useless.

SAS Innovate 2025: Register Now

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 811 views
  • 0 likes
  • 3 in conversation