|
Chapter Contents |
Previous |
Next |
| The DATASOURCE Procedure |
This example shows how to extract a subset of cross sections when the required cross sections are listed in an external file. In the case of a COMPUSTAT file, the required cross sections are a list of companies. For example, you may want to extract annual data for a list of companies whose industry classification codes (DNUM), CUSIP issuer codes (CNUM), and CUSIP issue number and check digits (CIC) are given in an external file, COMPLIST, as follows:
2640 346377 104 3714 017634 106 5812 171583 107 6025 446150 104 8051 087851 101
When the required companies are listed in an external file, you can either use the SAS macro processor to construct your WHERE statement expression or restructure your data file and include it after the WHERE key word.
The following steps use the first approach to construct the WHERE statement expression in the macro variable WHEXPR:
filename compfile 'host-specific-file-name' <host-options>;
%macro whstmt( fileref );
%global whexpr;
data _null_;
infile &fileref end=last;
length cnum $ 6;
input dnum cnum cic;
call symput( 'dnum'||left(_n_), left(dnum) );
call symput( 'cnum'||left(_n_), cnum );
call symput( 'cic' ||left(_n_), left(cic) );
if last then call symput( 'n', left(_n_) );
run;
%do i = 1 %to &n;
%let whexpr = &whexpr
(DNUM=&&dnum&i and CNUM="&&cnum&i" and CIC=&&cic&i);
%if &i ^= &n %then %let whexpr = &whexpr or;
%end;
%mend whstmt;
%whstmt( compfile );
filename compustat 'host-specific-Compustat-file-name' <host-options>;
proc datasource filetype=csaibm infile=compstat
outby=company out=dataset;
where &whexpr;
run;
The same result can also be obtained by creating an external file, WHEXPR, from the COMPFILE and including it after the WHERE key word, as shown in the following statements:
filename whexpr 'host-specific-WHEXPR-file-name' <host-options>;
data _null_;
infile compfile end=last; file whexpr;
length cnum $ 6;
input dnum cnum cic;
put "( " dnum= "and CNUM='" cnum $6. "' and " cic= ")" @;
if not last then put ' or'; else put ';' ;
run;
filename compstat 'host-specific-Compustat-file-name' <host-options>;
proc datasource filetype=csaibm infile=compustat
outby=company out=dataset;
where %inc 'host-specific-WHEXPR-file-name';
run;
title1 'Information on Selected Companies';
proc print data=company;
run;
The Output 10.8.1 shows the OUTBY= data set created by the preceding statements. As you can see, the companies listed in the COMPLIST file are reported in this data set.
Output 10.8.1: Printout of the OUTBY= Data Set Listing Selected Companies
|
Chapter Contents |
Previous |
Next |
Top |
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.