/********************************************************************/
/*                                                                  */
/* The LIB.INSDATA data set:                                        */
/*                                                                  */
/*   ID    INSTYPE    ADDR                                          */
/*   ------------------------------                                 */
/*   09    HOUSE      Mountain Road                                 */
/*   10    HOUSE      Old Town 10                                   */
/*   11    AUTO       Old Street 11                                 */
/*   11    BOAT       Old Street 11                                 */
/*   11    HOUSE      Old Street 11                                 */
/*   11    LEISURE    Old Street 11                                 */
/*   12    AUTO       Kings Road 12                                 */
/*   12    BOAT       Kings Road 12                                 */
/*   12    HOUSE      Kings Road 12                                 */
/*   14                                                             */
/*                                                                  */
/********************************************************************/
/*                                                                  */
/* The LIB.NEWADDR data set:                                        */
/*                                                                  */
/*   ID    NEWADDR                                                  */
/*   --------------------                                           */
/*   08                                                             */
/*   11    New Street 21                                            */
/*   12    Main Street 22                                           */
/*   15                                                             */
/*                                                                  */
/********************************************************************/

/********************************************************************/

   /*  TEST1                                                        */

options msglevel=i;
data test1;
   merge lib.insdata
         lib.newaddr (rename=(newaddr=addr));
   by id;
run;

/********************************************************************/

   /*  TEST2                                                        */

data test2 (drop=newaddr);
   merge lib.insdata
         lib.newaddr;
   by id;
   if newaddr ne "" then addr=newaddr;
run;

/********************************************************************/

   /*  TEST3                                                        */

data test3 (drop=newaddr);
   merge lib.insdata (IN=ina)
         lib.newaddr;
   by id;
   if not(ina) then delete;
   if newaddr ne "" then addr=newaddr;
run;

/********************************************************************/

   /*  TEST3a                                                       */

data test3a;
   merge lib.insdata (IN=ina)
         lib.newaddr (IN=inb);
   by id;
   if not(ina) then delete;
   if inb then addr=newaddr;
run;

/********************************************************************/

   /*  TEST4                                                        */
                                                                    
data vnewaddr / view=vnewaddr;
   set lib.newaddr;
   by id;
   if last.id then output;
run;

data test4;
   merge lib.insdata (in=ina)
         vnewaddr;
   by id;
   if not(ina) then delete;
   if newaddr ne "" then addr=newaddr;
run;

/********************************************************************/

   /*  TEST5                                                        */

data test5;
   merge lib.insdata (in=ina)
         lib.newaddr (in=inb rename=(newaddr=addr));
   by id;
   ka=ina;
   kb=inb;
run;

/********************************************************************/

   /*  TEST6                                                        */

data test6;
   merge lib.insdata (in=ina)
         lib.newaddr (in=inb);
   by id;
   ka=ina;
   kb=inb;
   if newaddr ne "" then addr=newaddr;
run;

/********************************************************************/

   /*  TEST7                                                        */

data test7;
   retain ra rb;
   ina=0;
   inb=0;
   merge lib.insdata (in=ina)
         lib.newaddr (in=inb);
   by id;
   if first.id then do;
      ra=0;
      rb=0;
   end;
   ka=ina;
   kb=inb;
   ra=max(ra,ina);
   rb=max(rb,inb);
   f_id=first.id;
   l_id=last.id;
   if newaddr ne "" then addr=newaddr;
run;

/********************************************************************/

   /*  TEST8                                                        */

data vnewaddr / view=vnewaddr;
   set lib.newaddr;
   by id;
   if last.id then output;
run;

data test8;
   merge lib.insdata  (in=ina)
         vnewaddr     (in=inb);
   by id;
   if not(ina) then delete;
   if not(inb) then delete;
   if newaddr ne "" then addr=newaddr;
run;

/********************************************************************/

   /*  TEST9                                                        */

data test9;
   ina=0;     /* Reset if multiple BY values occur in lib.insdata.  */
   inb=0;     /* Reset if multiple BY values occur in vnewaddr.     */ 
   merge lib.insdata  (in=ina  drop=addr)
         vnewaddr     (in=inb  rename=(newaddr=addr));
   by id;
   if not(ina) then delete;   /* Delete OBS with changes and no     */
                              /* customers.                         */
   if not(inb) then delete;   /* Delete OBS with customers and      */
run;                          /* no changes.                        */


/********************************************************************/

   /*  TEST10                                                       */   

data test10;
   merge lib.insdata  (in=ina  drop=addr)
         vnewaddr     (in=inb  rename=(newaddr=addr));
   by id;                                                           
   if not(ina) then delete;   /* Delete OBS with changes and no     */
                              /* customers.                         */
   if not(inb) then delete;   /* Delete OBS with customers and no   */
                              /* changes.                           */
   if last.id then output;    /* Output only the last of all        */
                              /* multiple BY values.                */
run;

/********************************************************************/

   /*  TEST11                                                       */

data vnewaddr / view=vnewaddr;
   set lib.newaddr;
   by id;
   if first.id and last.id then output;
run;

data test11;
   merge lib.insdata  (in=ina)
         vnewaddr     (in=inb);
   by id;
   if not(ina) then delete;
   if not(inb) then delete;
   if newaddr ne "" then addr=newaddr;
run;

/********************************************************************/

   /*  TEST12                                                       */

data test12;
   retain naddr;
   inb=0;
   merge lib.insdata (in=ina  drop=instype addr)
         lib.newaddr (in=inb  rename=(newaddr=addr));
   by id;
   if not(ina) then delete;
   if first.id then naddr=0;
   if inb then naddr=naddr+1;
   if last.id  AND  naddr=1 then output;
run;

/********************************************************************/

   /* TEST13                                                        */

data vnewaddr / view=vnewaddr;
   rename newaddr=addr;
   retain naddr 0;
   set lib.newaddr;
   by id;
   if first.id then naddr=0;
   naddr = naddr + 1;
   if last.id  AND  naddr GE 2 then output;
run;

data test13;
   merge lib.insdata  (in=ina drop=instype addr)
         vnewaddr     (in=inb drop=naddr);
   by id;                                      
   if not(ina) then delete;
   if not(inb) then delete;
run;

/********************************************************************/

   /*  TEST14                                                       */

data test14;
   merge lib.insdata (drop=instype)
         lib.newaddr (in=inb  keep=id);
   by id;
   if inb then delete;
run;

/********************************************************************/