![]() | ![]() | ![]() | ![]() | ![]() |
Note:
For detailed information regarding object dot programming
in the DATA step, please refer to
SAS 9.2 Language
Reference: Concepts, Using DATA Step Component Objects.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
/* Create test data sets. The variable TYPE contains a 'p' for */
/* purchase and an 's' for sale. SYMBOL is common to both */
/* data sets. */
data Transactions;
input date date7. symbol $ type $ Size;
datalines;
15Jun05 A p 500
16Jun05 G s 250
16Jun05 A s 100
17Jun05 C s 750
;
data DailyInventory;
input symbol $ Size;
datalines;
G 1000
C 1500
;
/* Use PROC SUMMARY to get an initial value for the TOTSIZE variable, */
/* whose value is the sum of all the SIZE variables. */
proc summary data=dailyinventory;
output out=totalinventory(keep=size);
var size ;
freq size;
run;
/* Create a macro variable called STOTSIZE */
data _null_;
set totalinventory;
call symput('stotsize',left(put(size,8.)));
stop;
run;
proc sort data=transactions;
by date;
run;
data _null_;
set transactions(rename=(size=t_size)) end=end;
by date;
/* Specify a LENGTH for the variables defined in the hash */
/* objects. */
length symbol $ 8 size date totsize 8;
retain totsize;
/* During the first iteration of the DATA Step, declare */
/* and instantiate the hash table DAILY. The DATASET: */
/* argument tag loads DAILYINVENTORY into DAILY. SYMBOL */
/* is defined as the 'lookup key'. SYMBOL and SIZE are */
/* defined as SYMBOL's associated data. Specifying */
/* SYMBOL in the DEFINEDATA method allows the variable to */
/* be part of the output data. Keys are not output by */
/* default. */
/* */
/* A second hash table TOT is also created. The ORDERED: */
/* argument tag requests the data be loaded into TOT in */
/* ascending sort order. DATE is specified as the */
/* 'lookup key' for TOT. DATE and TOTSIZE are defined as */
/* data values. */
if _n_=1 then do;
dcl hash daily(dataset:'dailyinventory');
daily.definekey('symbol');
daily.definedata('symbol', 'size');
daily.definedone();
dcl hash tot(ordered:'y');
tot.definekey('date');
tot.definedata('date','totsize');
tot.definedone();
totsize=&stotsize;
end;
/* On the first member of the BY-Group, add the current data */
/* values to the TOT hash table via the ADD method. */
if first.date then tot.add();
/* Use the FIND method to locate the current value of SYMBOL */
/* in the DAILY hash object. */
rc=daily.find();
/* If the current value of SYMBOL is not in the DAILY hash */
/* table, then add it. */
if rc ne 0 then rcadd=daily.add();
/* If TYPE equals 'p' for 'purchase', then calculate SIZE and */
/* use the REPLACE method to update the value of SIZE in DAILY. */
if type='p' then do;
size=sum(size,t_size);
daily.replace();
/* Use FIND to look for a matching value on DATE, update TOTSIZE */
/* and replace the value of TOTSIZE in the hash table TOT. */
tot.find();
totsize=totsize+t_size;
tot.replace();
end;
/* Or if TYPE equals 's' for 'sale', update the hash values in */
/* DAILY and TOT accordingly. */
else if type='s' then do;
size=sum(size,(-1*t_size));
daily.replace();
tot.find();
totsize=totsize-t_size;
tot.replace();
end;
/* When all the observations of the TRANSACTION data set have been */
/* processed, use the OUTPUT method to create the data set */
/* TOTALINVENTORY from the hash table TOT and DAILYINVENTORY from */
/* the hash table DAILY. */
if end then do;
tot.output(dataset:'totalinventory');
daily.output(dataset:'dailyinventory');
end;
run;
title 'Daily Inventory';
proc print data=dailyinventory;
run;
title 'Total Inventory';
proc print data=totalinventory;
format date date7.;
run;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Daily Inventory 09:12 Monday, November 28, 2005 10 Obs symbol size 1 A 400 2 C 750 3 G 750 Total Inventory 09:12 Monday, November 28, 2005 11 Obs date totsize 1 15JUN05 3000 2 16JUN05 2650 3 17JUN05 1900
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Component Objects ==> hash object ==> ADD SAS Reference ==> Component Objects ==> hash object ==> DEFINEDATA SAS Reference ==> Component Objects ==> hash object ==> DEFINEDONE SAS Reference ==> Component Objects ==> hash object ==> DEFINEKEY SAS Reference ==> Component Objects ==> hash object ==> FIND SAS Reference ==> Component Objects ==> hash object ==> OUTPUT SAS Reference ==> Component Objects ==> hash object ==> REPLACE |
| Date Modified: | 2006-06-07 03:03:04 |
| Date Created: | 2005-11-23 14:04:20 |
| Product Family | Product | Host | SAS Release | |
| Starting | Ending | |||
| SAS System | Base SAS | OpenVMS Alpha | 9.1 TS1M0 | n/a |
| z/OS | 9.1 TS1M0 | n/a | ||
| Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.1 TS1M0 | n/a | ||
| 64-bit Enabled HP-UX | 9.1 TS1M0 | n/a | ||
| 64-bit Enabled AIX | 9.1 TS1M0 | n/a | ||
| 64-bit Enabled Solaris | 9.1 TS1M0 | n/a | ||
| HP-UX IPF | 9.1 TS1M0 | n/a | ||
| Linux | 9.1 TS1M0 | n/a | ||
| Tru64 UNIX | 9.1 TS1M0 | n/a | ||




