An Idea Exchange for SAS software and services

Comments
by SAS Employee kmw
on ‎09-27-2017 02:01 PM

The PRIVATE keyword was implemented in 9.4M4.

You can also declare private METHODs which can be called by other methods inside the package, but cannot be called from the data program using dot notation.

 

Comments have been added as well as the error you'll receive if an attempt to alter that variable occurs.

 

proc ds2 ;

   /* Create a piggy bank that applies business rules */

package piggyBank / overwrite=YES;

   dcl private double balance limit;

   method myBank();

      limit=1000;

      balance=0;

   end;

   method add_amount(double iAmount);

      if sum(iAmount, balance) > limit then

         put 'ERROR: Deposit exceedes limit!';

      else

         balance=sum(iAmount, balance);

   end;

   method print_balance();

      put balance=;

   end;

endpackage;

run;

 

   /* Create and add bogus amounts to the bank */

data _null_;

   method run();

      dcl package piggyBank myBank();

      myBank.add_amount(5000);

 

      /* Won't affect the balance, prints ERROR msg */

      myBank.print_balance();

      myBank.balance=100000;

 

      /* DS2 does NOT allow this for private varaibles*/

      myBank.print_balance();

   end;

enddata;

run;

quit;

 

produces this error:

 

ERROR: Compilation error.

ERROR: Line 29: Attempt to get/set attribute balance which has

       PRIVATE access.

by Occasional Contributor snorex
on ‎09-27-2017 03:36 PM

LOL thanks, that's great! Sorry for being behind the times. Also thanks for the example code and error.

Idea Statuses
Top Liked Authors