I suspect you'll find the proc sql suggestion most easily employed.
But, assuming the stock trading data (dataset STOCK) has variables ticker, date, and other variable of interest, ... and the SP500_LIST dataset has variablea TICKER, ENTRY_DATE, and EXIT_DATE, then the code below should work.
data want;
set sp500_list (in=insp500 keep=ticker entry_date rename=(entry_date=date))
stocks (in=indaily) ;
by ticker date ;
if first.ticker then call missing(entry_date, exit_date);
if insp500 then set sp500_list; /* Retrieve entry_date and exit_date for this ticker*/
if indaily=1 and entry_date <= date <=exit_date ;
run;
Note a ticker can enter and exit the S&P500 more than once, either because of changing finances of the company, or because TICKER has been re-assigned to different companies. So SP500_LIST can have multiple obs per ticker.
The above code assumes:
STOCKS file is sorted by ticker/date.
SP500_LIST file is sorted by ticker/entry_date
... View more