DATA Step, Macro, Functions and more

Manipulate dataset while loading it to a hash table?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

Manipulate dataset while loading it to a hash table?

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


Accepted Solutions
Solution
‎09-21-2012 02:37 AM
Super User
Posts: 9,662

Re: Manipulate dataset while loading it to a hash table?

Yes. The best way to find answer is to make a experiment for it .

View solution in original post


All Replies
Solution
‎09-21-2012 02:37 AM
Super User
Posts: 9,662

Re: Manipulate dataset while loading it to a hash table?

Yes. The best way to find answer is to make a experiment for it .

Super Contributor
Posts: 474

Re: Manipulate dataset while loading it to a hash table?

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

Valued Guide
Posts: 765

Re: Manipulate dataset while loading it to a hash table?

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

Occasional Contributor
Posts: 15

Re: Manipulate dataset while loading it to a hash table?

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? Smiley Wink

Valued Guide
Posts: 765

Re: Manipulate dataset while loading it to a hash table?

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 324 views
  • 0 likes
  • 4 in conversation