BookmarkSubscribeRSS Feed
nochtan
Fluorite | Level 6

Hi all,

I am trying to resolve an error I am facing where I cant get my BUY - SELL Variable. A sample of my table is below:

CUSIPDATEBUYP2P3SELL
00036020JUN06....
00036020NOV060.0076714433...
00036020JAN07.0.0075574196..
00036020FEB07.0.009285739..
00036020MAR070.004984601
00036020APR070.0097122146
00036020MAY070.0014465061
00036020JUNE070.0004802898
00036020JUL07-0.011726471
00036020SEP07-0.015695027
01880210JAN940.007576222

There are different CUSIP (companies) at different dates.

Since my BUY and Sell variables are of different columns and they are alternate (which means stock j has either buy or sell at only one time), when i try to do a BUY - SELL as BUY_SELL command, the BUY_SELL is variable empty.

What I am trying to achieve is a BUY - SELL variable, something like SUM(BUY) - SUM (SELL) = mean(difference) etc.

variable
SELL
P2
P3
P4
BUY
count (N)
55706
55952
56260
54811
56629
mean
0.000928724
0.0016922
0.0018418
0.0017465
0.0024443
std deviation
0.0135390
0.0133077
0.0132802
0.0130985
0.0134837
t value
16.19
30.08
32.90
31.22
43.14
<.0001
<.0001
<.0001
<.0001
<.0001
BUY - SELL VARIABLE
7 REPLIES 7
EricHoogenboom
Fluorite | Level 6

Hello Nochtan,

I do not fully understand your question, it seems a mix of sum and mean.

Maybe you can add a small want - have example dataset.

Proc means aggregates your data to the level of cusip:

proc means data=have noprint;

class cusip;

var buy p1 p2 sell;

output out=want mean=;

run;

Hope this helps,

Eric

nochtan
Fluorite | Level 6

Hi Eric, I have already figured out. But I have another question to ask to seek your advice.

Hi all,

I am using IBES analysts forecast revision and CRSP for stock prices.

I am quite new to SAS and I have been trying to figure out how to form portfolio of analysts forecast revision to examine stock price drift for 1month, 3months, 6months, 12months and 24months. What I am trying to achieve is to hold stocks at time t, and buy or sell stocks according to the degree of analysts' forecast revision (1 = lowest (SELL) and 5 = highest (BUY)) at time t, and find the cumulative returns for 1,3,6,12 and 24 months of the stocks bought or sold at time t. (Without changing the portfolio). In other words, stocks will stay in the portfolio at time t and I will buy or sell according to the ranking of analysts' forecast revision.

HOWEVER, I only can manage to find stock returns at time t to analysts' forecast revision portfolio at time t, and I cant get the codes for the cumulative returns.

Below are my codes:

/* Step 1. Specifying Options */

%let J=6; /* Formation Period Length: J can be between 3 to 12 months - can be adjusted */

%let K=6; /* Holding   Period Length: K can be between 3 to 12 months - can be adjusted*/

%let begdate=01JAN1994;

%let enddate=31DEC2014;

run;

/* Step 2. Assign Ranks to the Next 6 (K) Months After Portfolio Formation */

/* Forecast_revision_portfolio is the portfolio rank variable taking values between 1 and 5: */

/*          1 - the lowest  momentum group: Losers   */

/*         5 - the highest momentum group: Winners  */

data getr_2 ;

set getr_2;

HDATE1 = intnx("MONTH",date, 0,"B")-1;

HDATE2 = intnx("MONTH",date,&k-1,"E");

format HDATE1 HDATE2 monyy.;

label HDATE1= "First Holding Date";

label HDATE2= "Last Holding Date";

run;

/* Portfolio returns are average monthly returns rebalanced monthly */

proc sql;

    create table getr_3

    as select distinct*

    from getr_2 as a, recency as b

    where a.cusip=b.cusip

    and a.HDATE1<=b.date<=a.HDATE2

order by cusip, date;

quit;

/* Step 4. Calculate Equally-Weighted Average Monthly Returns */

proc sort data=getr_3 nodupkey; by cusip date analys; run;

proc sort data=getr_3; by date forecast_revision_rank HDATE1;run;

/* Calculate Equally-Weighted returns across portfolio stocks */

/* Every date, each MOM group has J portfolios identified by formation date */

proc means data = getr_3 noprint;

  by date forecast_revision_rank HDATE1;

    var mean_returns;

    output out = umd3 mean=mean_returns;

run;

/* Portfolio average monthly returns */

proc sort data=umd3; by date Forecast_revision_rank;

    where year(date) >= year("&begdate"d);

run;

/* Create one return series per MOM group every month */

proc means data = umd3 noprint;

  by date forecast_revision_rank;

    var mean_returns;

    output out = ewretdat mean= ewret std = ewretstd;

run;

proc sort data=ewretdat; by forecast_revision_rank ; run;

Title "Table 1: Returns of Analysts' Forecast Revision Portfolios";

Title2 "Portfolios based on 6 months lagged return and held for 6 months";

proc means data=ewretdat n mean t probt;

  class Forecast_revision_rank;

    var ewret;

run;

/* Step 5. Calculate Long-Short Portfolio Returns */

proc sort data=ewretdat; by date Forecast_revision_rank; run;

proc transpose data=ewretdat out=ewretdat2

     (rename = (_1=SELL _2=PORT2 _3=PORT3 _4=PORT4 _5=BUY)

       drop=_NAME_ _LABEL_);

  by date;

  id Forecast_revision_rank;

   var ewret;

run;

/* Compute Long-Short Portfolio Cumulative Returns */

data ewretdat3;

set ewretdat2;

by date;

LONG_SHORT=BUY-SELL;

retain CUMRET_BUY CUMRET_SELL CUMRET_LONG_SHORT 0;

CUMRET_BUY     = (CUMRET_BUY+1)*(BUY+1)-1;

CUMRET_LOSERS      = (CUMRET_SELL +1)*(SELL +1)-1;

CUMRET_LONG_SHORT  = (CUMRET_LONG_SHORT+1)*(LONG_SHORT+1)-1;

format BUY SELL LONG_SHORT PORT: CUMRET_: percentn12.1;

run;

proc means data=ewretdat3 n mean t probt;

var BUY SELL LONG_SHORT;

run;

Below is a screen shot of the table:

DATECUSIPCOMNAMAdjusted_Pricehighdays_since_52WHReturnsmean_returnsRRobsnoanalysfpedatsForecast_Revisionforecast_revision_RankHDATE1HDATE2
Jun-0600036020AAON INC5.0686419455.635555691370.0094413760.004199479412198011201130-Jun-060.334083245May-06Nov-06
Jun-0600036020AAON INC5.0686419455.635555691370.0094413760.00419947941219807978830-Jun-060.4668166485May-06Nov-06
Nov-0600036020AAON INC5.5150617435.730370416110.0340740770.007576944Oct-06Apr-07
Jan-0700036020AAON INC5.4281480585.73037041651-0.0061482850.0024473113Dec-06Jun-07
Feb-0700036020AAON INC5.4854321895.82716049470.0007207370.0006504884Jan-07Jul-07
May-0700036020AAON INC5.8686417945.92987663413-0.0030201390.0057424444Apr-07Oct-07
Jul-0700036020AAON INC5.9081481186.8069133999-0.009602679-0.0027158235Jun-07Dec-07
Aug-0700036020AAON INC6.2044446036.816789792190.0300049490.0026546035Jul-07Jan-08
Oct-0700036020AAON INC5.4103704386.816789792610.033389935-0.0034140253Sep-07Mar-08
Nov-0700036020AAON INC5.6385187226.816789792820.0042216320.0025669413Oct-07Apr-08
Dec-0700036020AAON INC5.8725925026.816789792102-0.0250860910.00248648212198010778031-Dec-07-0.3209169051Nov-07May-08

Please help my guardian angels. Thank You.

EricHoogenboom
Fluorite | Level 6

Hello Nochtan,

I am very sorry but I am not a helpdesk.

Please shorten your question to the tiny part that you don't know yet.

Cheers,

Eric

nochtan
Fluorite | Level 6

Hi Eric,

baiscally this is the main part:

Step 2. Assign Ranks to the Next 6 (K) Months After Portfolio Formation */

/* Forecast_revision_portfolio is the portfolio rank variable taking values between 1 and 5: */

/*          1 - the lowest  momentum group: Losers   */

/*         5 - the highest momentum group: Winners  */

data getr_2 ;

set getr_2;

HDATE1 = intnx("MONTH",date, 0,"B")-1;

HDATE2 = intnx("MONTH",date,&k-1,"E");

format HDATE1 HDATE2 monyy.;

label HDATE1= "First Holding Date";

label HDATE2= "Last Holding Date";

run;

where I can't seem to assign ranks to the next (1,3,6,12,24months) even after using the intnx function. I would like to hold stocks at time t and find the returns at the desired time t+?

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Can you not then just coalesce the values, sorry am not really clear on what relation p2 or p3 is to the buy or sell column, so I will assume they relate to sell

proc sql;

     create table WANT as

     select     CUSPID,

                   sum(BUY) as BUY,

                   sum(coalesce(P2,P3,SELL)) as SELL,

                   sum(BUY) - sum(coalesce(P2,P3,SELL) as BUY_SELL

     from        HAVE

     group by CUSPID;

quit;

Another tip is to normalise your data, i.e. rather than having columns for each bit, have rows, and an identifier column:

CUSPID     DATE          ID          RESULT

0011          11jan12        BUY      0.3453525

0011          11jan12        P2         0.45564

...

It makes it easier to do aggregates.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

You haven't posted any test data in the form of a datastep, so I can't provide a working example, however something like:

proc sql;

     create table WANT as

     select     CUSPID,

                   sum(BUY) as BUY,

                   sum(SELL) as SELL,

                   sum(BUY) - sum(SELL) as BUY_SELL

     from        HAVE

     group by CUSPID;

quit;

nochtan
Fluorite | Level 6

Hi RW9,

The formula that you recommend will have a NULL Buy_Sell variable because all the stocks have alternate buy, P2, P3, P4, Sell variables. I have tried. 

What I did was this:

proc means data = want4 noprint;

  by date RR;

    var roll_avg;

    output out = want4 mean= ewret std = ewretstd;

run;

proc sort data=want4; by RR ; run;

Title "Recency Strategy (Table 1): Returns of Relative Strength Portfolios";

Title2 "Portfolios based on &J month lagged return and held for &K months";

proc means data=want4 n mean t probt;

  class RR;

    var ewret;

run;

/* Step 5. Calculate Long-Short Portfolio Returns */

proc sort data=want4; by date RR; run;

proc transpose data=want4 out=want5

     (rename = (_1=RRL _2=PORT2 _3=PORT3 _4=PORT4 _5=RRH)

       drop=_NAME_ _LABEL_);

  by date;

  id RR;

   var ewret;

run;

/* Compute Long-Short Portfolio Cumulative Returns */

data Want5;

set want5;

by date;

LONG_SHORT=RRH-RRL;

retain CUMRET_RRH CUMRET_RRL CUMRET_LONG_SHORT 0;

CUMRET_RRH     = (CUMRET_RRH+1)*(RRH+1)-1;

CUMRET_RRL      = (CUMRET_RRL +1)*(RRL +1)-1;

CUMRET_LONG_SHORT  = (CUMRET_LONG_SHORT+1)*(LONG_SHORT+1)-1;

format RRH RRL LONG_SHORT PORT: CUMRET_: percentn12.1;

run;

Sorry for the mess! 😕

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 1129 views
  • 3 likes
  • 3 in conversation