So I'm creating a hash table and filling it with data using the following code:
/*Declare and Define Hash*/
Declare Hash hh();
hh.DefineKey('MyKey');
hh.DefineData('Var1', 'Var2');
hh.DefineDone();
/*Load Hash*/
Do Until (EOF1);
SET MyLib.HashData end=EOF1;
hh.Add();
End;
In the process of running the do loop, can I manipulate data before it is added to the hash table? In other words:
Do Until (EOF1);
SET MyLib.HashData end=EOF1;
Var2 = VarA || VarB;
hh.Add();
End;
Thanks!!
Jonathan
Yes. The best way to find answer is to make a experiment for it .
Yes. The best way to find answer is to make a experiment for it .
Do consider the option of building a view from the table with all the transformed data you need and load the whole thing into the hash at once (with the dataset option), instead of iterating through the data and adding each member one by one.
Cheers from Portugal.
Daniel Santos @ www.cgd.pt
hi ... I like Ksharp's idea, why not just try it and see if it works
here's an example of Daniel's idea of using a view that allows you to load the data set into the hash table in the declare statement
* your data;
data test;
input mykey (var1 var2) (:$1.);
datalines;
123 a b
234 c d
345 q z
999 m m
;
* add a variable;
data test_view / view=test_view;
length var3 $3;
set test;
var3 = catx('+',var1,var2);
run;
* some "keys";
data search;
input key @@;
datalines;
234 999
;
* load the view, look for "keys";
data found;
if 0 then set test_view;
declare hash hh(dataset:'test_view');
hh.definekey('mykey');
hh.definedata(all:'yes');
hh.definedone();
do until(done);
set search (rename=(key=mykey)) end=done;
if ^hh.find() then output;
end;
stop;
run;
mykey var1 var2 var3
234 c d c+d
999 m m m+m
Thanks for the feedback everyone!
@Ksharp, normally I would, but the computer I was on when I thought of the question did not have SAS on it and I needed a quick answer for a presentation I'm doing today. I knew you guys and gals would not let me down!
@DanielSantos and @MikeZdeb, the dataset option is the way I normally load the hash table, but sometimes I need to do things on the fly and would rather use the do until option that writing another data step, or my hash table comes from an imported Excel file and I would rather manipulate it while loading it to memory instead of writing an extra data step. After all, why use two data steps when you can get by with 1?
hi ... I guess I'd count lines of SAS code ...
Do Until (EOF1);
SET MyLib.HashData end=EOF1;
Var2 = VarA || VarB;
hh.Add();
End;
versus ...
data test_view / view=test_view;
length var3 $3;
set test;
var3 = catx('+',var1,var2);
run;
looks like tie to me (whatever floats your boat) ... forgot to add the "VIEW" portion is previous post
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.