DATA Step, Macro, Functions and more

why it keeps saying "QtrNum has been defined as both character and numeric", hash object definition

Reply
Frequent Contributor
Posts: 133

why it keeps saying "QtrNum has been defined as both character and numeric", hash object definition

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.

Super User
Posts: 10,028

why it keeps saying "QtrNum has been defined as both character and numeric", hash object definition

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

Frequent Contributor
Posts: 133

why it keeps saying "QtrNum has been defined as both character and numeric", hash object definition

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.

Super User
Posts: 10,028

why it keeps saying "QtrNum has been defined as both character and numeric", hash object definition

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

N/A
Posts: 1

Re: why it keeps saying "QtrNum has been defined as both character and numeric", hash object definition

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;

Ask a Question
Discussion stats
  • 4 replies
  • 491 views
  • 0 likes
  • 3 in conversation