BookmarkSubscribeRSS Feed
DimaFrank
Calcite | Level 5

Hi All,

The question is about ds2 package implementation:

I created user-defined package in proc ds2, named Stock, as shown below, that has Constructor, methods and variables, similar to other OOP languages.

 

proc ds2;
	
	PACKAGE Stock / overwrite=yes;
		
		dcl varchar(4) Ticker;
		dcl double OpenPrice;
		dcl double ClosePrice;
		dcl int Volume;
		
		METHOD GetTicker() RETURNS varchar;
			return Ticker;
		END;
		
		METHOD SetTicker(Varchar(4) Tick);
			Ticker = Tick;
		END;
		
		METHOD GetOpenPrice() RETURNS double;
			return OpenPrice;
		END;
		
		METHOD SetOpenPrice(double new_open_price);
			OpenPrice = new_open_price;
		ENd;
		
		METHOD GetClosePrice() RETURNS double;
			return ClosePrice;
		END;
		
		METHOD SetClosePrice(double new_close_price);
			ClosePrice = new_close_price;
		END;
		
		METHOD GetVolume() RETURNS int;
			return Volume;
		END;
		
		METHOD SetVolume(int Vol);
			Volume = Vol;
		END;
		
		/*Default Constructor*/
		METHOD Stock();
			put 'Stock - Default Constructor';
			Volume = 0;
		END;
		
		/*Overloaded constructor*/
		METHOD Stock(varchar(4) Tick, double Open, double Close, int Vol);
			put 'Overloaded constructor';
			Stock();
			SetTicker(Tick);
			SetOpenPrice(Open);
			SetClosePrice(Close);
			SetVolume(Vol);
		END;
		
		METHOD DELETE();
			PUT 'Stock - DELETE EXECUTED';
		END; 

		METHOD CalcDailyReturn() RETURNS double;
			return (ClosePrice/OpenPrice)-1;
		END;

	 ENDPACKAGE;
	 
	 
run;

quit;
	

After that I created a few instances of my Stock package (Asset1, Asset2, Asset3) inside ds2 data statement. And then initialized pre-declared variables by calling a methods on my stock instance inside the RUN() method.

 

proc ds2;

	DATA work.oop_test (overwrite=yes);
	
		dcl package Stock Asset1('AAPL', 115.94, 127.35, 400000); 
		dcl package Stock Asset2('IBM', 1232.55, 1221.77, 1400000);
		dcl package Stock Asset3('AMZN', 884.14, 891.45, 900000);

		dcl varchar(4) Ticker;
		dcl double OpenPrice;
		dcl double ClosePrice;
		dcl int Volume;
		dcl double PriceDiff;
		dcl double DailyReturn;
		
		METHOD RUN();
		
			Asset1.SetClosePrice(130.30);
			Ticker = Asset1.GetTicker();
			OpenPrice = Asset1.GetOpenPrice();
			ClosePrice = Asset1.GetClosePrice();
			Volume = Asset1.GetVolume();
			PriceDiff = Asset1.GetClosePrice() - Asset1.GetOpenPrice();
			DailyReturn = Asset1.CalcDailyReturn();
	
				
		END;
		
		METHOD TERM();
			put 'END!';
		END;
		
		
	ENDDATA;
	
run;
quit;

proc print data=oop_test;
run;

But what if I have a huge dataset with a similar variables, and I want that each observation will be a Stock package instance, in order to operate with them like with objects. 

 

Let's say I have this data set:

data market;
input Ticker $3. OpenPrice 5.1 ClosePrice 5.1 Volume;
datalines;
TTC 15.4 17.2 500000
KKL 21.2 22.5 740000
MMT 45.5 55.1 800000
;
run;

and my goal is to turn each observation to object of a Stock package.

 

Is there any possible way to do that, except declare object for each line in my dataset one after another, by calling the constructor and providing the values of each observation?

It's a little bit complicate if my dataset have a huge number of observations.

And my second question is, if this technique of data operations is optimal in terms of  processing time optimization and memory usage?

 

Thank you.

2 REPLIES 2
mkeintz
PROC Star

Why do you need each stock to be an object?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
DimaFrank
Calcite | Level 5
I’m just trying to solve it in OOP paradigm, but I am not sure that this is an optimal way to do that.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 302 views
  • 0 likes
  • 2 in conversation