You seem to have the basic idea. To get what you want use the logic of the hash1 (or hash2) but put the provzip data set into the hash table and iterate over the address data set.
The logic you're using only gets rows for which you have a postal code; doing it the other way will get all addresses, with postal code missing if the lookup fails.
Use:
> if rc ^=0 then postl_cd=' ';
> if rc =0 then output;
but take the "if rc = 0" off the second statement.
Note: it's usual with a hash lookup like this to put the small data set into the hash table. This beats merge by not requiring the large data set to be sorted. Proc SQL is a viable alternative; only a test will tell which is better.
Cheers,
Jonathan
> Hello,
>
> I'm trying to learn some hash object....The original
> was written in a merge, I tried proc sql and it is
> acceptable using left join, just wondering how to get
> the left join using hash object..
>
>
> data address;
> addr_key=10000001;
> address='test address1';
> output;
> addr_key=10000002;
> address='test address2';
> output;
> addr_key=10000003;
> address='test address3';
> output;
> n;
>
> proc print u;
> title 'address';
> n;
>
>
> data provzip;
> addr_key=10000001;
> postl_cd='87048';
> output;
> addr_key=10000002;
> postl_cd='39735';
> output;
> run;
>
> proc print u;
> title 'provzip';
> un;
>
>
> data hash1 (drop=rc);
> length postl_cd $5;
> declare AssociativeArray hh () ;
> rc = hh.DefineKey ( 'addr_key' ) ;
> rc = hh.DefineData ( 'addr_key', 'address' ) ;
> rc = hh.DefineDone () ;
> call missing(postl_cd);
> do until ( eof1 ) ;
> set address end = eof1 ;
> rc = hh.add () ;
> end ;
> do until ( eof2 ) ;
> set provzip end = eof2 ;
> rc = hh.find () ;
> if rc ^=0 then postl_cd=' ';
> if rc =0 then output;
> end ;
> run ;
>
> proc print data=hash1;
> title 'hash1';
> n;
>
> data hash2;
> if _n_ = 1 then do;
> if 0 then set address;
> declare hash lookup(dataset:'address');
> lookup.defineKey('addr_key');
> lookup.definedata('address');
> lookup.defineDone();
> end;
>
> set provzip;
> if lookup.find(key:addr_key) ne 0 then postl_cd='
> ';
>
>
>
> PROC PRINT;
> title 'hash2';
> N;
>
> proc sql;
> create table sql1 as
> select *
> from address a
> left join provzip b
> on a.addr_key=b.addr_key
> ;
> quit;
> un;
>
> proc print u;
> title 'sql';
> n;