![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
The sample code on the Full Code tab illustrates how to use a hash object to 'chain' through multiple observations to create a new variable containing each 'link' that contributed to the 'chain'.
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.
Load a data set view into a hash table. Use the hash table to track the links that have contributed to a 'chain' and to reset the variable that is used to trigger the next look-up in the 'chain'.
Read observations from a data set. Use the FIND() method to continuously search for 'links' in the hash table, until the look-up fails. This indicates you are at the end of the chain. Create a new variable of each 'link' in the 'chain' using CATX.
/* Create starting data */
data transactions;
input id $ new_id $;
datalines;
ab1234 cf3245
klh3409 we93476
cf3245 lk23489
34pwk32 whlw09
we93476 34pwk32
fds0987 welz345
;
/* When using the DATASET: argument tag, all variables in the hash table must */
/* exist in the data set. In this case, we need a flag variable that does not */
/* exist on the current version of TRANSACTIONS. A quick way around this is */
/* to create a VIEW of TRANSACTIONS that does contain the flag variable */
/* ALREADY_USED. */
/* Create a view of the desired data set plus the additional variable. */
data transactions_vw/view=transactions_vw;
retain already_used ' ';
set transactions;
run;
data links;
keep chain start end;
length chain $100 start $8 end $8;
/* On the first iteration of the DATA step, create the hash table H. */
if _n_=1 then do;
/* Create hash table H to hold unique ID values from TRANSACTIONS. */
/* The DATASET: argument tag loads the view TRANSACTIONS_VW into H. */
declare hash h(dataset: 'transactions_vw');
h.definekey('id');
h.definedata('id','new_id','already_used');
h.definedone();
end;
/* Read values for ID from TRANSACTIONS. Use the FIND method to locate a */
/* match for ID's value in the hash table H. Grab the NEW_ID value from */
/* H and assign its value into ID. Repeat the look up with the FIND method */
/* to determine if the NEW_ID value assigned into ID has a match. If so, */
/* you are in a chain. Continue 'chaining' until no match is found. */
do until(done);
set transactions end=done;
test=h.find();
if test=0 and already_used='y' then continue;
/* Assign NEW_ID from TRANSACTIONS into ID, for the look up. Create a */
/* new variable CHAIN to track each 'link' in the chain. This is optional. */
/* To trap only the 'end points' of a chain, create 2 new variables, START */
/* and END. */
chain=catx(' ',id, new_id);
start=id;
end=new_id;
/* NEW_ID will become the new value to match in the hash table. */
id=new_id;
do until(rc ne 0); /* Keep 'chaining' until no match is found. Be sure to */
/* know your data. If you have unexpected overlap in */
/* the ranges, such as one 'link' pointing to a second */
/* link that points back to the first, you could enter */
/* an infinite loop. */
rc=h.find();
if rc=0 then do;
/* Mark the item in the hash table as 'used'. This flag will keep */
/* individual 'links' from being output as separate 'chains' of */
/* their own. */
already_used='y';
h.replace();
/* Tack NEW_ID onto the end of CHAIN. Reassign END and ID accordingly. */
chain=catx(' ',chain,new_id);
end=new_id;
id=new_id;
end;
/* If RC is not zero, no match was found, so output the observation. */
else output;
end; /* inner DO UNTIL */
end; /* outer DO UNTIL */
run;
proc print;
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.
Obs chain start end 1 ab1234 cf3245 lk23489 ab1234 lk23489 2 klh3409 we93476 34pwk32 whlw09 klh3409 whlw09 3 fds0987 welz345 fds0987 welz345
Type: | Sample |
Topic: | SAS Reference ==> DATA Step SAS Reference ==> Component Objects SAS Reference ==> Component Objects ==> hash object SAS Reference ==> Component Objects ==> hash object ==> FIND SAS Reference ==> Functions ==> Character ==> CATX |
Date Modified: | 2007-05-04 03:03:05 |
Date Created: | 2007-05-04 03:03:05 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | z/OS | 9.1 TS1M0 | |
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.1 TS1M0 | |||
Microsoft Windows 2000 Advanced Server | 9.1 TS1M0 | |||
Microsoft Windows 2000 Datacenter Server | 9.1 TS1M0 | |||
Microsoft Windows 2000 Server | 9.1 TS1M0 | |||
Microsoft Windows 2000 Professional | 9.1 TS1M0 | |||
Microsoft Windows NT Workstation | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise Edition | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Standard Edition | 9.1 TS1M0 | |||
Microsoft Windows XP Professional | 9.1 TS1M0 | |||
64-bit Enabled AIX | 9.1 TS1M0 | |||
64-bit Enabled HP-UX | 9.1 TS1M0 | |||
64-bit Enabled Solaris | 9.1 TS1M0 | |||
HP-UX IPF | 9.1 TS1M0 | |||
Linux | 9.1 TS1M0 | |||
OpenVMS Alpha | 9.1 TS1M0 | |||
Tru64 UNIX | 9.1 TS1M0 |