Hash DefineData('Var1') but get Var2 and Var3 also

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

Hash DefineData('Var1') but get Var2 and Var3 also

Hi!

I am still fairly new to Hash Tables.  I've really been working with them a lot lately and have noticed something.  Even though I specify only certain variables in the DefineData statement, I end up getting all of the fields in the dataset that I'm using as my hash table.  For example, say I have a dataset called Hash_Data with variables My_Key, Var_1, Var_2, ..., Var_6.  I only want to add Var_1 and Var_2 to my main dataset.  However, the hash will add Var_3 through Var_6 as well.

Is this normal or am I doing something wrong?  I know I can fix by simply utilizing DROP and KEEP, but would rather not have to do this when I have datasets with a lot of variables and only want 1 or 2 columns.  Below is a sample of the type of code I am using:

Data Hash_Out;

Declare Hash EW (dataset: "Hash_Data");

     rc = EW.DefineKey('My_Key');

     rc = EW.DefineData('Var_1', 'Var_2');

     rc = EW.DefineDone();

Do Until (EOF1);

     Set Main_Data end=EOF1;

          Call Missing('Var_1', 'Var_2');  /*Initiallizing my vars*/

          rc = EW.Find();

          OUTPUT;

Stop;Run;

Thanks!!!

Jonathan


Accepted Solutions
Solution
‎09-11-2012 04:57 PM
Trusted Advisor
Posts: 1,301

Re: Hash DefineData('Var1') but get Var2 and Var3 also

Posted in reply to railfan1975

Hash component objects were still pre-production in 9.1.3, if you truely are using them as much as you say you should really update your version of SAS.  In 9.2 and higher this problem you are having does not exist.  Also, you cannot use data set options of the dataset value in the delcare statement.  The best thing to do would be to use a drop or keep statement, like you said.  In short you aren't really doing anything wrong per-se, you are however using pre-production software that has since been improved drastically in later versions.

Data Hash_Out;

call missing(var_1,var_2); /* initialize vars, once */

Declare Hash EW (dataset: "Hash_Data");

   EW.DefineKey('My_Key');

   EW.DefineData('Var_1', 'Var_2');

EW.DefineDone();

Do Until (EOF1);

     Set Main_Data end=EOF1;

          rc = EW.Find();

          OUTPUT;

End;

drop rc var_3-var_6;

Stop;

Run;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,301

Re: Hash DefineData('Var1') but get Var2 and Var3 also

Posted in reply to railfan1975

What version of SAS are you using?

Respected Advisor
Posts: 3,156

Re: Hash DefineData('Var1') but get Var2 and Var3 also

Posted in reply to railfan1975

If you use ew.output method, then your hash() definition becomes relevant, otherwise, you just 'set' main_data to hash_out.

Haikuo

Occasional Contributor
Posts: 15

Re: Hash DefineData('Var1') but get Var2 and Var3 also

Posted in reply to railfan1975

Egg, sorry, forgot to state that.  I'm using 9.1.3.

Hai.kuo, I'm not quite following you.  Can you give me a quick code example?

Solution
‎09-11-2012 04:57 PM
Trusted Advisor
Posts: 1,301

Re: Hash DefineData('Var1') but get Var2 and Var3 also

Posted in reply to railfan1975

Hash component objects were still pre-production in 9.1.3, if you truely are using them as much as you say you should really update your version of SAS.  In 9.2 and higher this problem you are having does not exist.  Also, you cannot use data set options of the dataset value in the delcare statement.  The best thing to do would be to use a drop or keep statement, like you said.  In short you aren't really doing anything wrong per-se, you are however using pre-production software that has since been improved drastically in later versions.

Data Hash_Out;

call missing(var_1,var_2); /* initialize vars, once */

Declare Hash EW (dataset: "Hash_Data");

   EW.DefineKey('My_Key');

   EW.DefineData('Var_1', 'Var_2');

EW.DefineDone();

Do Until (EOF1);

     Set Main_Data end=EOF1;

          rc = EW.Find();

          OUTPUT;

End;

drop rc var_3-var_6;

Stop;

Run;

Occasional Contributor
Posts: 15

Re: Hash DefineData('Var1') but get Var2 and Var3 also

Ironically, I have 9.2 available on my PC, but the server where my data resides (50 million records) is still 9.1.3.  However, we will soon be running 9.3 if all goes well.  Given that, how would you change the code to fit the improvements in 9.2 and 9.3?


Trusted Advisor
Posts: 1,301

Re: Hash DefineData('Var1') but get Var2 and Var3 also

Posted in reply to railfan1975

There wouldn't be anything necessary to change as this issue you are having does not occur in 9.2 or 9.3.  I believe in a early version of 9.2 this was still a problem but it could be solved there using dataset options since that feature was added in 9.2 as follows:

declare hash ew(dataset:'hash_data(keep=my_key var_1 var_2)');

The benefits from the newer 9.2 and 9.3 versions include additional methods, multi-data keys, increased performance and stability, etc...

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 431 views
  • 0 likes
  • 3 in conversation