data difference (drop=goalamount)/**/;
length goalamount 8 ;
if _n_=1 then do;
declare hash goal();
goal.definekey('QtrNum');
goal.definedata("goalamount");
goal.definedone();
/*avoid the NOTE of "var uninitialized "*/
call missing(qtrnum,goalamount);
goal.add(key:'qtr1', data:10);
goal.add(key:'qtr1', data:15);
goal.add(key:'qtr3', data:5);
goal.add(key:'qtr4', data:15);
end;
set sasuser.contrib;
goal.find();
Diff=amount-goalamount;
run;
Define a hash object, log keeps saying
"
ERROR: Variable QtrNum has been defined as both character and numeric.
"
I don't get where I cause the error? I checked Listserv, but still don't know how to fix this.
This example is from advanced sas certification, chapter 17, hash object definition, hard-code section, this is a buggy code, just doesn't work.
Did you define the type of QtrNum before defining Hash Table?
data difference (drop=goalamount)/**/;
length goalamount 8 ;
if _n_=1 then do;
length QtrNum $ 8;
declare hash goal();
goal.definekey('QtrNum');
goal.definedata("goalamount");
goal.definedone();
/*avoid the NOTE of "var uninitialized "*/
call missing(qtrnum,goalamount);
goal.add(key:'qtr1', data:10);
goal.add(key:'qtr1', data:15);
goal.add(key:'qtr3', data:5);
goal.add(key:'qtr4', data:15);
end;
set sasuser.contrib;
goal.find();
Diff=amount-goalamount;
run;
Ksharp
It still didn't work, now it says:
ERROR: Duplicate key.
ERROR: Key not found.
ERROR: Key not found.
ERROR: Key not found.
ERROR: Key not found.
ERROR: Key not found.
ERROR: Key not found.
OK. you need add a return code before Hash methods.
rc=goal.add(key:'qtr1', data:10);
rc=goal.add(key:'qtr1', data:15);
rc=goal.add(key:'qtr3', data:5);
rc=goal.add(key:'qtr4', data:15);
rc=goal.find();
Ksharp
I got the same problem, and I believe there is an typo on the book"SAS Certification Prep Guide-Advanced Programming for sas 9", page 606.
the code: "length goalamount 8;" should be changed to: "length qtrnum $8 goalamount 8;"
I submitted the following program, and it runs with no error:
data diffe (drop=goalamount);
length goalamount 8 qtrnum $4;
if _N_=1 then do;
declare hash goal();
goal.definekey("qtrnum");
goal.definedata("Goalamount");
goal.definedone();
call missing(qtrnum, goalamount);
goal.add(key:'qtr1',data:10);
goal.add(key:'qtr2',data:15);
goal.add(key:'qtr3',data:5);
goal.add(key:'qtr4',data:15);
end;
set tian.contrib;
rc=goal.find();
diff=amount-goalamount;
run;
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.