DATASOURCE Procedure

Example 12.9 CRSP Daily NYSE/AMEX Combined Stocks

(View the complete code for this example.)

This sample code reads all the data in a three-volume daily NYSE/AMEX combined character data set. Assume that the following filerefs are assigned to the calendar/indices file and security files that this database comprises:

Fileref VOLSER File Type
calfile DXAA1 Calendar/indices file on volume 1
secfile1 DXAA1 Security file on volume 1
secfile2 DXAA2 Security file on volume 2
secfile3 DXAA3 Security file on volume 3

The data set CALDATA is created by the following statements to contain the calendar/indices file:

proc datasource filetype=crspdci infile=calfile out=caldata;
run;

Here the FILETYPE=CRSPDCI indicates that you are reading a character format (indicated by a C in the 6th position) daily (indicated by a D in the 5th position) calendar/indices file (indicated by an I in the 7th position).

The annual data in security files can be obtained by the following statements:

proc datasource filetype=crspdca
                infile=( secfile1 secfile2 secfile3 )
                out=annual;
run;

Similarly, the data sets to contain the daily security data (the OUT= data set) and the event data (the OUTEVENT= data set) are obtained by the following statements:

proc datasource filetype=crspdcs
                infile=( calfile secfile1 secfile2 secfile3 )
                out=periodic index outevent=events;
run;

Note that the FILETYPE= has an S in the 7th position, since you are reading the security files. Also, the INFILE= option first expects the fileref of the calendar/indices file since the dating variable (CALDT) is contained in that file. Following the fileref of calendar/indices file, you give the list of security files in the order in which you want to read them. When data span more than one physical volume, the filerefs of the security files residing on each volume must be given following the fileref of the calendar/indices file. The DATASOURCE procedure reads each of these files in the order in which they are specified. Therefore, you can request that all three volumes be mounted to the same drive, if you choose to do so.

This sample code illustrates the following points:

  • The INDEX option in the second PROC DATASOURCE run creates an index file for the OUT=PERIODIC data set. This index file provides random access to the OUT= data set and might increase the efficiency of the subsequent PROC and DATA steps that use BY and WHERE statements. The index variables are CUSIP, CRSP permanent number (PERMNO), NASDAQ company number (COMPNO), NASDAQ issue number (ISSUNO), header exchange code (HEXCD), and header SIC code (HSICCD). Each one of these variables forms a different key which is a single index. If you want to form keys from a combination of variables (composite indexes) or use some other variables as indexes, you should use the INDEX= data set option for the OUT= data set.

  • The OUTEVENT=EVENTS data set is sparse. In fact, for each EVENT type, a unique set of event variables are defined. For example, for EVENT=’SHARES’, only the variables SHROUT and SHRFLG are defined, and they have missing values for all other EVENT types. Pictorially, this structure is similar to the data set shown in Table 4. Because of this sparse representation, you should create the OUTEVENT= data set only when you need a subset of securities and events.

By default, the OUT= data set contains only the periodic data. However, you might also want to include the event-oriented data in the OUT= data set. This is accomplished by listing the event variables together with periodic variables in a KEEP statement. For example, if you want to extract the historical CUSIP (NCUSIP), number of shares outstanding (SHROUT), and dividend cash amount (DIVAMT) together with all the periodic series, use the following statements:

proc datasource filetype=crspdcs
                infile=( calfile secfile1 secfile2 secfile3 )
                out=both outevent=events;
   where cusip='09523220';
   keep  bidlo askhi prc vol ret sxret bxret ncusip shrout divamt;
run;

The KEEP statement has no effect on the event variables output to the OUTEVENT= data set. If you want to extract only a subset of event variables, you need to use the KEEPEVENT statement. For example, the following sample code outputs only NCUSIP and SHROUT to the OUTEVENT= data set for CUSIP=’09523220’:

proc datasource filetype=crspdxc
                infile=( calfile secfile)
                outevent=subevts;
   where cusip='09523220';
   keepevent  ncusip shrout;
run;

Output 12.9.1, Output 12.9.2, Output 12.9.3, and Output 12.9.4 show how to read the CRSP Daily NYSE/AMEX Combined ASCII Character Files.

filename dxci "%sysget(DATASRC_DATA)dxccal95.dat" RECFM=F LRECL=130;
filename dxc "%sysget(DATASRC_DATA)dxcsub95.dat" RECFM=F LRECL=400;

/*--- create output data sets from character format DX files ---*/
/*- create securities output data sets using DATASOURCE  -------*/
/*- statements                                                 -*/
proc datasource filetype=crspdcs  ascii
                infile=( dxci dxc )
                interval=day
                outcont=dxccont
                outkey=dxckey
                outall=dxcall
                out=dxc
                outevent=dxcevent
                outselect=off;
   range from '15aug95'd to '28aug95'd ;
   where cusip in ('12709510','35614220');
run;

title1 'Date Range 15aug95-28aug95 ';

title3 'DX Security File Outputs';
title4 'OUTKEY= Data Set';
proc print data=dxckey;
run;

title4 'OUTCONT= Data Set';
proc print data=dxccont;
run;

title4 "Listing of OUT= Data Set for cusip in ('12709510','35614220')";
proc print data=dxc;
run;

title4 "Listing of OUTEVENT= Data Set for cusip in ('12709510','35614220')";
proc print data=dxcevent;
run;

Output 12.9.1: Listing of the OUTBY= Data Set with OUTSELECT=OFF

Date Range 15aug95-28aug95
 
DX Security File Outputs
OUTKEY= Data Set

ObsCUSIPPERMNOCOMPNOISSUNOHEXCDHSICCDBYSELECTST_DATEEND_DATENTIMENOBSNINRANGENSERIESNSELECT
168391610100007952978733990007JAN198611JUN198752100357
212709510100107967980933840117JAN198628AUG19953511243110357
349307510100207972982436710027JAN198630APR1993265100357
4003386901003022160013310002JUL196226DEC1968237000357
541741F20100407988984636210007FEB198615JUN1989122500357
60007421010050131133448029DEC197216JUN1978199600357
735614220100608007987631040124FEB198629DEC19953596249210357


Output 12.9.2: Listing of the OUTCONT= Data Set

Date Range 15aug95-28aug95
 
DX Security File Outputs
OUTCONT= Data Set

ObsNAMEKEPTSELECTEDTYPELENGTHVARNUMLABELFORMATFORMATLFORMATD
1BIDLO11168Bid or Low 00
2ASKHI11169Ask or High 00
3PRC111610Closing Price of Bid/Ask average 00
4VOL111611Share Volume 00
5RET111612Holding Period Return 00
6SXRET111613Standard Deviation Excess Return 00
7BXRET111614Beta Excess Return 00
8NCUSIP0028.Name CUSIP 00
9TICKER0025.Exchange Ticker Symbol 00
10COMNAM00232.Company Name 00
11SHRCLS0021.Share Class 00
12SHRCD0016.Share Code 00
13EXCHCD0016.Exchange Code 00
14SICCD0016.Standard Industrial Classification Code 00
15DISTCD0016.Distribution Code 00
16DIVAMT0016.Dividend Cash Amount 00
17FACPR0016.Factor to adjust price 00
18FACSHR0016.Factor to adjust shares outstanding 00
19DCLRDT0016.Declaration dateDATE70
20RCRDDT0016.Record dateDATE70
21PAYDT0016.Payment dateDATE70
22SHROUT0016.Number of shares outstanding 00
23SHRFLG0016.Share flag 00
24DLSTCD0016.Delisting code 00
25NWPERM0016.New CRSP permanent number 00
26NEXTDT0016.Date of next available informationDATE70
27DLBID0016.Delisting bid 00
28DLASK0016.Delisting ask 00
29DLPRC0016.Delisting price 00
30DLVOL0016.Delisting volume 00
31DLRET0016.Delisting return 00
32TRTSCD0016.Traits code 00
33NMSIND0016.National Market System Indicator 00
34MMCNT0016.Market maker count 00
35NSDINX0016.NASD index 00


Output 12.9.3: Listing of the OUT= Data Set with OUTSELECT=OFF for CUSIPs 12709510 and 35614220

Date Range 15aug95-28aug95
 
DX Security File Outputs
Listing of OUT= Data Set for cusip in ('12709510','35614220')

ObsCUSIPPERMNOCOMPNOISSUNOHEXCDHSICCDDATEBIDLOASKHIPRCVOLRETSXRETBXRET
11270951010010796798093384015AUG19957.5007.87507.562529200-0.008197..
21270951010010796798093384016AUG19957.5007.87507.500022365-0.008264..
31270951010010796798093384017AUG19957.5007.87507.5000334160.000000..
41270951010010796798093384018AUG19957.3757.50007.375016666-0.016667..
51270951010010796798093384021AUG19957.3757.37507.375093820.000000..
61270951010010796798093384022AUG19957.2507.37507.250033674-0.016949..
71270951010010796798093384023AUG19957.2507.37507.3125223710.008621..
81270951010010796798093384024AUG19957.1257.50007.125038621-0.025641..
91270951010010796798093384025AUG19956.8757.37507.000029713-0.017544..
101270951010010796798093384028AUG19957.0007.12507.0000387980.000000..
113561422010060800798763104015AUG199512.37512.687512.3750391360.000000..
123561422010060800798763104016AUG199512.12512.375012.203145916-0.013889..
133561422010060800798763104017AUG199512.25012.312512.2500436440.003841..
143561422010060800798763104018AUG199512.25012.625012.3750110270.010204..
153561422010060800798763104021AUG199512.37512.625012.375073780.000000..
163561422010060800798763104022AUG199512.25012.375012.250099655-0.010101..
173561422010060800798763104023AUG199512.12512.250012.125095148-0.010204..
183561422010060800798763104024AUG199512.12512.375012.37501855720.020619..
193561422010060800798763104025AUG199512.00012.250012.00009575-0.030303..
203561422010060800798763104028AUG199512.00012.062512.0625128540.005208..


Output 12.9.4: Listing of the OUTEVENT= Data Set in Range 15aug95–28aug95

Date Range 15aug95-28aug95
 
DX Security File Outputs
Listing of OUTEVENT= Data Set for cusip in ('12709510','35614220')

ObsCUSIPPERMNOCOMPNOISSUNOHEXCDHSICCDEVENTDATENCUSIPTICKERCOMNAMSHRCLSSHRCDEXCHCDSICCDDISTCDDIVAMTFACPRFACSHRDCLRDTRCRDDTPAYDTSHROUTSHRFLGDLSTCDNWPERMNEXTDTDLBIDDLASKDLPRCDLVOLDLRETTRTSCDNMSINDMMCNTNSDINX
112709510100107967980933840DELIST28AUG1995    ............20323588...0.0.037500....
212709510100107967980933840NASDIN24AUG1995    ....................12172


Note in Output 12.9.4 that there were no events in range for cusip 35614220. For more information about CRSPAccess Data access, see Chapter 46, SASECRSP Interface Engine.

Last updated: June 19, 2025