BookmarkSubscribeRSS Feed

Requesting the addition of a [PRIVATE] keyword to protect user-defined package-scope PROC DS2 variables from direct access from outside an instance of the package.

 

Private variables and methods are a common feature of object orientated programming. See Why do we need private variables?

 

Suppose this PROC DS2:

 

proc ds2 ;
	/* Create a piggy bank that applies business rules */
	package piggyBank / overwrite=YES;
		dcl double balance limit;

		method piggyBank();
			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;

	/* Create and add bogus amounts to the bank */
	data _null_;
		method run();
			dcl package piggyBank myBank();
			myBank.print_balance();
			
			/* No problem to add 45 to the balance */
			myBank.add_amount(45);
			
			/* This will print an error message */
			myBank.add_amount(5000);
			
			/* Bypass the business rules and directly set the balance.
				I propose a [PRIVATE] keyword that would prevent 
				`balance` from being directly accessible.
			*/
			myBank.balance=100000;
		end;
	enddata;
	run;
quit;

 

It is ashame that `piggyBank.balance` is directly accessible. You can bypass the business rules and directly edit the balance! My idea is for a `private` keyword that can be used to prevent direct access, something like:

 

 

package piggyBank / overwrite=YES; 
     dcl double [PRIVATE] balance; 
     /* ... code omitted for brevity ... */ 

     /* SAS won't compile this and will print an error */ 
     myBank.balance = 10000;

 

Apologies if there's a way that I'm missing to do this. Thanks.

 

2 Comments
kmw
SAS Employee
SAS Employee

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.

snorex
Fluorite | Level 6

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