Below is a link to SAS code that can be used to calculate daily or monthly portfolio returns. The macro can be used to create benchmark returns (market returns, or factor returns for a multi-factor model), or returns based on groups (such as deciles) of fundamental variables (momentum, accruals, earnings, etc.) to do anomaly analysis.
The macro is extensively documented, including several examples that can be run directly on the WRDS system. Note, you will need to use the delisting macro (and others) if you use the _portret.sas macro.
Code to compute portfolio returns
Click here to download the _portret macro
This is the code used in Crawford, Hansen, and Price, "Do you know what's in your benchmark?" (2013, Journal of Portfolio Management). It is extensively documented. The code for the macro can replicate the CRSP market and portfolio returns, and much more. You need to provide the portfolio assignment file and the macro will compute the portfolio returns.
This code also implements the bid-ask spread adjustment used in Beaver, McNichols, and Price (2016, Journal of Accounting Literature). Quoted prices in general (and in CRSP) are somewhere in between the bid and ask prices. To incorporate bid-ask spreads, the code assumes purchases are made at the ask price, and sales are at the bid price. Separate adjustments are needed for long and short positions. For a long position, the return is calculated as (bid_t - ask_t-1)/ask_t-1. For a short position, the return is calculated as (ask_t - bid_t-1)/bid_t-1.
A more extensive example showing how to use the macro to replicate a momentum-based strategy is forthcoming.
Code to identify security type
Here is a program that will merge the share code with a permno: _shrcd.sas. For example, common shares are identified as 10, 11 and 12 (for non-US corporations). ADRs are identified with a share code beginning with 3: 30, 31, 32. See macro or CRSP documentation for details.
Code to create weekly returns from CRSP daily returns
Weekly returns are used in some instances (such as to calculate beta). CRSP only provides daily and monthly datasets. The _wsf.sas macro uses the CRSP daily dataset and creates corresponding weekly datasets. Benchmarks can be created using the _portret.sas macro.