Good morning. I would like to use the "output" method of hash sas, but I need to add the "key" column and display it on the "class_hash" output dataset. I can't figure out how to do it. I give the example:
data large;
if 0 then set sashelp.class;
declare hash obj(dataset: 'sashelp.class', multidata:'y');
obj.definekey ("name" );
obj.defineData(all:'yes');
obj.definedone();
length key $ 200;
key = catt(name,sex);
obj.output(dataset:'class_hash');
stop;
run;
Mario
Loading and then writing a hash table is still a read/write operation so for your use case wouldn't offer any advantage over a simple data step.
Using the hash object offers often real advantages for lookups as it helps to avoid sorts.
The output method can only output what's in the hash defined as data.
The hash key column name is with all:"yes" also part of the data and will be included.
Column key which you create in your SAS data step is not in the hash table and though not available when writing the hash table in memory to disk.
Not sure if that's going to be of any use for your real use case but below one option how to add another variable to the hash.
data v_demo / view=v_demo;
set sashelp.class;
length key $ 200;
key = catt(name,sex);
run;
data large;
if 0 then set v_demo;
declare hash obj(dataset: 'v_demo', multidata:'y');
obj.definekey ("name" );
obj.defineData(all:'yes');
obj.definedone();
length key $ 200;
key = catt(name,sex);
obj.output(dataset:'class_hash');
stop;
run;
proc print data=class_hash;
run;
Thank you, but this way I don't get any advantage in terms of processing time. I was hoping you could save processing time compared to a simple data or proc sql step, using hash "output" method
Loading and then writing a hash table is still a read/write operation so for your use case wouldn't offer any advantage over a simple data step.
Using the hash object offers often real advantages for lookups as it helps to avoid sorts.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.