Published on :
Macro EXTERNE

SAS Hash Object Definition Macro

This code is also available in: Deutsch Español Français
This macro encapsulates the object-oriented syntax of the SAS© Data Step for Hash objects. It dynamically manages the initialization of keys (definekey method) and data variables (definedata method) from lists of variables provided as parameters. It automatically loads the Hash object with the specified dataset.
Data Analysis

Type : EXTERNE


The macro expects a dataset name as a parameter (&dataset) to load the Hash object. This dataset must exist in the SAS session when the macro is executed.

1 Code Block
DATA STEP
Explanation :
Definition of the 'create_hash' macro. It declares a Hash object linked to a dataset. It loops over the list of key variables to configure 'definekey' and over the list of data variables to configure 'definedata', before finalizing the definition with 'definedone'.
Copied!
1%macro create_hash(name,key,data_vars,dataset);
2 declare hash &name(dataset:&dataset);
3 %local i n d;
4 %let n=%sysfunc(countw(&key));
5 rc = &name..definekey(
6 %DO i=1 %to %eval(&n-1);
7 "%scan(&key,&i)",
8 %END;
9 "%scan(&key,&i)"
10 );
11 %let n=%sysfunc(countw(&data_vars));
12 %DO i=1 %to &n;
13 %let d=%scan(&data_vars,&i);
14 rc = &name..definedata("&d");
15 %END;
16 rc = &name..definedone();
17%mend;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright (c) 2015 by The Financial Risk Group, Cary, NC, USA.