<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Using DS2 package like OOP class in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-DS2-package-like-OOP-class/m-p/825380#M326025</link>
    <description>&lt;P&gt;Why do you need each stock to be an object?&lt;/P&gt;</description>
    <pubDate>Tue, 26 Jul 2022 02:27:00 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2022-07-26T02:27:00Z</dc:date>
    <item>
      <title>Using DS2 package like OOP class</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-DS2-package-like-OOP-class/m-p/825345#M326002</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;The question is about ds2 package implementation:&lt;/P&gt;&lt;P&gt;I created user-defined package in proc ds2, named Stock, as shown below, that has Constructor, methods and variables, similar to other OOP languages.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;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;
	&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let's say I have this data set:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and my goal is to turn each observation to object of a Stock package.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any possible way to do that, &lt;SPAN&gt;except&amp;nbsp;&lt;/SPAN&gt;declare object for each line in my dataset one after another, by calling the constructor and providing the values of each observation?&lt;/P&gt;&lt;P&gt;It's a little bit complicate if my dataset have a huge number of observations.&lt;/P&gt;&lt;P&gt;And my second question is, if this technique of data operations is optimal in terms of&amp;nbsp;&amp;nbsp;processing time optimization and memory usage?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 23:00:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-DS2-package-like-OOP-class/m-p/825345#M326002</guid>
      <dc:creator>DimaFrank</dc:creator>
      <dc:date>2022-07-25T23:00:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using DS2 package like OOP class</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-DS2-package-like-OOP-class/m-p/825380#M326025</link>
      <description>&lt;P&gt;Why do you need each stock to be an object?&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2022 02:27:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-DS2-package-like-OOP-class/m-p/825380#M326025</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-07-26T02:27:00Z</dc:date>
    </item>
    <item>
      <title>Re: Using DS2 package like OOP class</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-DS2-package-like-OOP-class/m-p/825389#M326029</link>
      <description>I’m just trying to solve it in OOP paradigm, but I am not sure that this is an optimal way to do that.&lt;BR /&gt;</description>
      <pubDate>Tue, 26 Jul 2022 04:22:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-DS2-package-like-OOP-class/m-p/825389#M326029</guid>
      <dc:creator>DimaFrank</dc:creator>
      <dc:date>2022-07-26T04:22:31Z</dc:date>
    </item>
  </channel>
</rss>

