BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Linlin
Lapis Lazuli | Level 10

Hi,

Under the influence of Ksharp I want to learn hash. I post the log file below. how can I correct the problem? 

Thank you!

35   data work.difference (drop= goalamount);

36   length goalamount 8;

37   if _N_ = 1 then do;

38   declare hash goal( );

39   goal.definekey("QtrNum");

40   goal.definedata("GoalAmount");

41   goal.definedone( );

42   call missing(qtrnum, goalamount);

43   goal.add(key:’qtr1’, data:10 );

                  -

                  22

                  200

ERROR: DATA STEP Component Object failure.  Aborted during the COMPILATION phase.

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string,

              a numeric constant, a datetime constant, a missing value, arrayname, (, +, -, INPUT,

              NOT, PUT, ^, _NEW_, ~.

ERROR 200-322: The symbol is not recognized and will be ignored.

NOTE: The SAS System stopped processing this step because of errors.

NOTE: DATA statement used (Total process time):

      real time           0.00 seconds

      cpu time            0.00 seconds

44   goal.add(key:’qtr2’, data:15 );

45   goal.add(key:’qtr3’, data: 5 );

46   goal.add(key:’qtr4’, data:15 );

47   end;

48   set sasuser.contrib;

49   goal.find();

50   Diff = amount - goalamount;

51   run;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Hi . LinLin.

I am glad that you start to learn Hash. It is wonderful tool. I bet you will like it.

Does these code is what you copy and paste form somewhere or PDF?

The single quote in your code is not right. You should correct it by your hand.

data work.difference (drop= goalamount);
 if _N_ = 1 then do;
  length goalamount 8 QtrNum $ 8;
 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 sasuser.contrib;
 goal.find();
 Diff = amount - goalamount;
run;

Ksharp

View solution in original post

7 REPLIES 7
Ksharp
Super User

Hi . LinLin.

I am glad that you start to learn Hash. It is wonderful tool. I bet you will like it.

Does these code is what you copy and paste form somewhere or PDF?

The single quote in your code is not right. You should correct it by your hand.

data work.difference (drop= goalamount);
 if _N_ = 1 then do;
  length goalamount 8 QtrNum $ 8;
 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 sasuser.contrib;
 goal.find();
 Diff = amount - goalamount;
run;

Ksharp

Linlin
Lapis Lazuli | Level 10

Hi Ksharp,

Yes, I copied the code from a PDF file. Thank you!

Linlin
Lapis Lazuli | Level 10

Hi Ksharp,

Another problem. I typed in this time. I will email you a sas program that will generate all the sas datasets I am using.
Thank you very much for your help!

512 data work.report;

513 if 0 then

514 set sasuser.acities (keep=code city name);

515 if _n_ =1 then do;

516 declare hash airports (dataset: "sasuser.acities");

517 airports.definekey("Code");

518 airports.definedata("City","Name");

519 airports.definedone();

520 end;

521 set sasuser.Revenue;

522 airports.find(key:origin);

523 origincity=city;

524 originairport=name;

525 airports.find(key:dest);

526 destcity=city;

527 destairport=name;

528 run;

NOTE: There were 50 observations read from the data set SASUSER.ACITIES.

ERROR: Key not found.

ERROR: Key not found.

ERROR: Key not found.

ERROR: Key not found.

ERROR: Key not found.

ERROR: Key not found.

NOTE: The SAS System stopped processing this step because of errors.

NOTE: There were 142 observations read from the data set SASUSER.REVENUE.

WARNING: The data set WORK.REPORT may be incomplete. When this step was stopped there were 142

observations and 14 variables.

WARNING: Data set WORK.REPORT was not replaced because this step was stopped.

NOTE: DATA statement used (Total process time):

real time 0.01 seconds

cpu time 0.01 seconds

Linlin
Lapis Lazuli | Level 10

data work.report;

  if _n_=1 then do;

    if 0 then set sasuser.acities (keep=code city name);

          declare hash airports (dataset: "sasuser.acities");

          airports.definekey("Code");

          airports.definedata("City","Name");

          airports.definedone();

          end;

          set sasuser.revenue;

          rc=airports.find(key:origin);

          if rc=0 then do;

          ocity=city;

          oairport=name;end;

          else do;ocity= ' ';

                  oairports=' ';

            end;

          run;

FriedEgg
SAS Employee

Linlin, looks like you found your own solution.... Yes, when using the find method for hash object you will recieve an error if you try to lookup a non-existant key your original way.  Assigning the result to a scalar varaible like you did here is one option to avoid this issue.  Another is to first use the check method and then find.

*generate some data;

data countries;

infile cards dlm='|' missover;

input country :$upcase45. area area_rank coastline (arable_land crops other_land) (:percent8.2) lat lng;

cards;

Nauru|21|239|30|0%|0%|100%|-0.547613|166.919103

Fiji|18274|157|1129|10.95%|4.65%|84.4%|-17.752158|177.451212

Tonga|747|190|419|20%|14.67%|65.33%|-21.242532|-175.142206

American Samoa|199|215|116|10%|15%|75%|-14.331667|-170.711389

Nonsense|

; *data is from CIA Factbook;

run;

*generate some data;

data airports;

infile cards dlm='|' missover;

input (country city airport_code airport_name) (:$upcase45.) runway_length runway_elevation;

cards;

Nauru|Nauru Island|INU|Nauru International|7054|22

Fiji|Nadi|NAN|Nadi International|10500|63

Tonga|Nuku'alofa|TBU|Fua'amotu International|8795|126

American Samoa|Pago Pago|PPG|Pago Pago International Airport|13800|32

Bad Row|

; *data is from Wikipedia;

run;

*bring it together;

data _null_;

if 0 then set maps.oceania2(keep=cont96_geo) airports countries(keep=country lat lng); *setup scalar;

if _n_=1 then

  do;

   declare hash ha(dataset:'countries(keep=country lat lng)');

    ha.definekey('country');

          ha.definedata('lat','lng');

          ha.definedone();

   declare hash mp(dataset:'maps.oceania2(rename=(idname=country))'); *to satisfy gmap;

    mp.definekey('country');

          mp.definedata('country','cont96_geo');

          mp.definedone();

   declare hiter iter('mp'); *will need to move data from mp to ou later;

   declare hash ou(ordered:'a'); *use this hash object to get sorted output later by country;

    ou.definekey('country');

    ou.definedata('country','cont96_geo','airport_name','runway_length','runway_elevation');

          ou.definedone();

  end;

do until(done); *cycle through dataset;

  set airports end=done;

   r1=ha.find(); *if just use ha.find() without r1= then if no match found, get error;

   r2=mp.find();

   if r1+r2=0 then ou.add(); *only want to output records I found in all datasets;

end;

call missing(of _all_);

do while(iter.next()=0); *add countries I did not include earlier that I have no data for.  This is so I get a full map later;

  if ou.check() ne 0 then ou.add(); *avoid trying to add a duplicate;

end;

ou.output(dataset:'report');

run;

*now produce a map with proc gmap;

Linlin
Lapis Lazuli | Level 10

Thank you very much!

Ksharp
Super User

Nice.

Keep moving.

Ksharp

Smiley Happy

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 2612 views
  • 6 likes
  • 3 in conversation