Wednesday, November 25, 2009

Read and sort in one step - Hash object tip

In SAS world, the classical way to process raw file is to read it into SAS dataset and then sort it. With SAS9 Hash object in DATA step, we can make an all-in-one step.

* Hash sample - read and sort raw data;
data _null_;
/* Declare hash object and key and data variable names */
if _N_ = 1 then do;
declare hash h(ordered:'A');
rc = h.defineKey('key');
rc = h.defineData('key','value');
rc = h.defineDone();
end;

/* Read key and data values */
infile datalines eof=eof;
input key value $;

/* Add key and data values to hash object */
rc = h.add();

return;
eof: rc = h.output(dataset:"ordered");
cards;
1 value1
3 value2
2 value3
;

Actually, it is only one little tip of Hash object.
We must never forget that the main functionality of Hash object is to quickly and efficiently store, search, and retrieve data based on lookup keys.

No comments: