| Date: | Thu, 17 Jul 2008 16:31:13 -0400 |
| Reply-To: | Paul Dorfman <sashole@BELLSOUTH.NET> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | Paul Dorfman <sashole@BELLSOUTH.NET> |
| Subject: | Re: Merging SAS Dataset with Flat File on the fly - Need HELP |
|
Brian,
Reverse the task and view the flat file as a driver and the SAS data set as a look-up table. The
latter can take on a variety of forms. Say, you have a flat file and SAS data set concocted from
sashelp.zipcode, keyed by ZIP and with about 1/3 of keys mismatched between the two:
filename flat temp ;
data sas (keep = zip state);
set sashelp.zipcode ;
file flat ;
if ranuni (1) < .3 then fzip ++ .5 ;
else fzip = zip ;
put @1 fzip z7.1 @ 10 city ;
run ;
-------------------------------
NOTE: 41988 records were written to the file FLAT.
NOTE: There were 41988 observations read from the data set SASHELP.ZIPCODE.
NOTE: The data set WORK.SAS has 41988 observations and 2 variables.
Now if you would prefer to organize the look-up table as a hash, consider
data merged ;
if _n_ = 1 then do ;
if 0 then set sas ;
dcl hash sas (dataset: 'sas') ;
sas.definekey ('zip') ;
sas.definedata ('state') ;
sas.definedone ('state') ;
end ;
infile flat truncover ;
input @1 zip 9. @10 city $35. ;
call missing (state) ;
_iorc_ = sas.find() ; * left join on flat file ;
* if _iorc_ = 0 ; * equijoin ;
run ;
If you need an equijoin (output only matching records), erase the asterisk. If you like SAS indices
better, you can make the SAS file a self-searching look-up table in itself:
proc sql ;
create index zip on sas (zip) ;
quit ;
data merged ;
infile flat truncover ;
input @1 zip 9. @10 city $35. ;
set sas key = zip / unique ;
if _iorc_ ne 0 then do ;
_error_ = 0 ;
call missing (state) ;
end ;
* nothing needed ; * left join on flat ;
*if _iorc_ = 0 ; * equijoin ;
run ;
Once again, for equijoin, delete the last asterisk. If you need the output in the form of another
flat file, just use _NULL_ instead of MERGED and a pair of FILE/PUT statements as dictated by the
requirements and common sense.
Kind regards
-----------
Paul Dorfman
Jax, FL
-----------
On Thu, 17 Jul 2008 11:59:00 -0700, Brian <brian8202@GMAIL.COM> wrote:
>Hello,
>
>I have a SAS dataset driver file that has 100 records. I need to
>merge this dataset with another file which is a flat file.
>Unfortunately this flat file has about 1 million records. One of the
>ways to do it is reading every single record from the file file into a
>SAS dataset then merge with the driver file, but this process is very
>time consuming and inefficient. I am looking for a way that I am able
>to merge the SAS dataset with the records on the flat file on the fly.
>Thank you very much for your help.
>
>
>Sincerely,
>
>Brian
|