![]() | ![]() | ![]() | ![]() | ![]() |
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 sample data */
data in_data;
input issue :$5. time :time8. price :comma12. qty;
datalines;
AA 9:31:01 $9.13 100
AA 9:31:25 $9.14 100
AA 9:32:00 $9.13 250
AA 9:34:00 $9.13 -100
BB 9:31:01 $9.13 100
BB 9:31:25 $9.14 100
BB 9:32:00 $9.13 250
BB 9:34:00 $9.13 -100
;
/* The value of TIME_HASH equals the value of TIME in the first occurrence of */
/* PRICE for each BY-Group. QTY_HASH equals the cumulative value of QTY */
/* within each BY-Group where TIME=TIME_HASH. */
data test;
/* Define LENGTH attribute for variables in the hash object for TEST's PDV */
length time_hash qty_hash 8;
/* On the first iteration of the DATA step, create a hash table H. The */
/* ORDERED: arguement tag requests that the data be loaded into H in */
/* ascending sort order. PRICE is defined as the 'lookup key' and PRICE, */
/* TIME_HASH, and QTY_HASH are its associated data values. Defining PRICE */
/* as both KEY and DATA will allow the value of PRICE to be written the */
/* output data set TEST. Use CALL MISSING to initialize the specified */
/* variables to missing, and avoid a 'variable uninitialized' message in */
/* the SAS log. */
if _n_=1 then do;
declare hash h;
h=_new_ hash(ordered:'yes');
h.defineKey('price');
h.defineData('price','time_hash','qty_hash');
h.defineDone();
call missing(time_hash, qty_hash);
end;
set in_data;
by issue;
/* On the first member of the BY-Group ISSUE that is not the first */
/* iteration of the DATA step, delete the current hash table and */
/* create a new one with the same atttributes. */
if first.issue and _n_ ne 1 then do;
/* Create a new hash table for each new value of ISSUE */
h.delete();
h=_new_ hash(ordered:'yes');
h.defineKey('price');
h.defineData('price','time_hash','qty_hash');
h.defineDone();
end;
rc_find=h.find();
if rc_find=0 then do;
/* If rc=0 then the value of PRICE read from IN_DATA has been found in the hash table. */
/* Perform the calculation in this DO block. */
/* Use the REPLACE method to update the new value of QTY_HASH value in the hash table. */
qty_hash=qty+qty_hash;
rc_repl=h.replace();
end;
else do;
/* PRICE was not found so it is added to the hash table. */
time_hash=time;
qty_hash=qty;
rc_add=h.add();
end;
/* The implied output that executes at the end of the DATA step */
/* will create the observations in the data set TEST. */
run;
proc print;
format time time_hash time8.;
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.
time_
Obs hash qty_hash issue time price qty rc_find rc_repl rc_add
1 9:31:01 100 AA 9:31:01 9.13 100 -2147450842 . 0
2 9:31:25 100 AA 9:31:25 9.14 100 -2147450842 . 0
3 9:31:01 350 AA 9:32:00 9.13 250 0 0 .
4 9:31:01 250 AA 9:34:00 9.13 -100 0 0 .
5 9:31:01 100 BB 9:31:01 9.13 100 -2147450842 . 0
6 9:31:25 100 BB 9:31:25 9.14 100 -2147450842 . 0
7 9:31:01 350 BB 9:32:00 9.13 250 0 0 .
8 9:31:01 250 BB 9:34:00 9.13 -100 0 0 .
| Type: | Sample |
| Topic: | SAS Reference ==> Component Objects ==> hash object ==> DEFINEDATA SAS Reference ==> DATA Step SAS Reference ==> Component Objects ==> hash object ==> ADD SAS Reference ==> Component Objects ==> hash object ==> DEFINEDONE SAS Reference ==> Component Objects ==> hash object ==> DEFINEKEY SAS Reference ==> Component Objects ==> hash object ==> DELETE SAS Reference ==> Component Objects ==> hash object ==> FIND SAS Reference ==> Component Objects ==> hash object ==> REPLACE |
| Date Modified: | 2006-06-06 03:03:03 |
| Date Created: | 2005-11-02 15:22:00 |
| Product Family | Product | Host | SAS Release | |
| Starting | Ending | |||
| SAS System | Base SAS | Tru64 UNIX | 9.1 TS1M0 | n/a |
| Linux | 9.1 TS1M0 | n/a | ||
| 64-bit Enabled Solaris | 9.1 TS1M0 | n/a | ||
| HP-UX IPF | 9.1 TS1M0 | n/a | ||
| 64-bit Enabled HP-UX | 9.1 TS1M0 | n/a | ||
| 64-bit Enabled AIX | 9.1 TS1M0 | n/a | ||
| Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.1 TS1M0 | n/a | ||
| OpenVMS Alpha | 9.1 TS1M0 | n/a | ||
| z/OS | 9.1 TS1M0 | n/a | ||




