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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.