BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BenConner
Pyrite | Level 9


Hi,

I'm using 3 hash tables in a data step to pull in data for a large dataset.  In many cases the key won't be found, and in those situations I just want to ignore the condition.  What I'm seeing is each time this happens, an error gets sent to the log file in the form of:

ERROR: Key not found.

I didn't spot any obvious way to suppress these. ??

Thanks!

--Ben

1 ACCEPTED SOLUTION

Accepted Solutions
MikeZdeb
Rhodochrosite | Level 12

hi ... I suspect you are just using something like ... o.find() ... rather than ... rc=o.find()

no error messages in the following ...

data one;

input x y @@;

datalines;

1 100 2 200 3 200

;

data two;

input x @@;

datalines;

1 9 2 9 3

;

data three (drop=rc);

declare hash o (dataset:'one');

o.definekey ('x');

o.definedata ('y');

o.definedone();

do until(done);

  set two end=done;

  rc=o.find();

  if rc then call missing(y);

  output;

end;

stop;

run;

DATA SET THREE

y     x

100    1

  .    9

200    2

  .    9

200    3

View solution in original post

8 REPLIES 8
MikeZdeb
Rhodochrosite | Level 12

hi ... I suspect you are just using something like ... o.find() ... rather than ... rc=o.find()

no error messages in the following ...

data one;

input x y @@;

datalines;

1 100 2 200 3 200

;

data two;

input x @@;

datalines;

1 9 2 9 3

;

data three (drop=rc);

declare hash o (dataset:'one');

o.definekey ('x');

o.definedata ('y');

o.definedone();

do until(done);

  set two end=done;

  rc=o.find();

  if rc then call missing(y);

  output;

end;

stop;

run;

DATA SET THREE

y     x

100    1

  .    9

200    2

  .    9

200    3

BenConner
Pyrite | Level 9

OH!  Yes, one of the find() was w/o a rc= context.  Didn't realize that would generate a 20 meg log file.

Much appreciated!

--Ben

Linlin
Lapis Lazuli | Level 10

Hi Mike,

Can you explain why you need 'stop'?   Thanks!

MikeZdeb
Rhodochrosite | Level 12

hi ... sure

LOG with stop ...

NOTE: There were 3 observations read from the data set WORK.ONE.

NOTE: There were 5 observations read from the data set WORK.TWO.

NOTE: The data set WORK.THREE has 5 observations and 2 variables.

LOG without stop ...

NOTE: There were 3 observations read from the data set WORK.ONE.

NOTE: There were 3 observations read from the data set WORK.ONE.

NOTE: There were 5 observations read from the data set WORK.TWO.

NOTE: The data set WORK.THREE has 5 observations and 2 variables.

without the stop, the data step cycles back to the start to check if there is any more data to read and it does all the hash stuff a second time before it hits the DOW loop that reads the data

the job works either way, just "better" with stop

OK?

Linlin
Lapis Lazuli | Level 10

Thank you Mike!

below is the how I code hash:

data three (drop=rc);

  if _n_=1 then do;

   if 0 then set one;

declare hash o (dataset:'one');

o.definekey ('x');

o.definedata ('y');

o.definedone();

  end;

do until(done);

  set two end=done;

  rc=o.find();

  if rc then call missing(y);

  output;

end;

run;

Linlin

MikeZdeb
Rhodochrosite | Level 12

hi ... without the stop, the data step still goes back to the start with your code to check if there's any more data to be read

try this and look at the LOG, you'll see two lines were written ... still works, but does that extra, unnecessary check

data three (drop=rc);

  if _n_=1 then do;

   if 0 then set one;

declare hash o (dataset:'one');

o.definekey ('x');

o.definedata ('y');

o.definedone();

  end;

put "HI LINLIN ... " _n_;

do until(done);

  set two end=done;

  rc=o.find();

  if rc then call missing(y);

  output;

end;

run;

Linlin
Lapis Lazuli | Level 10

Thank you Mike! I will remember to add 'stop'. See you on Tuesday.  - Linlin

sharmas
Calcite | Level 5

Hey,

A bit confused here. Thanks for your help in advance. This is what I have. Col1-49 is a symmetric 49*49 matrix and they represent distance from one another.

Country     GDP         Col1      Col2     Col3.............Col49

Germany      5             0          3           1                  2

France         4              3           0          2                 1

Belgium        1              1         2           0                  1

So basically Market potential for Germany = (GDP of france)/Distance between germany and france (3 in this case)  + (GDP of Belgium)/(Distance between Ger and Bel (1 in this case).

So I got a help from Ksharp here, but it says ERROR: Key not found.

data have;

set have;

k=cats('Col',_n_);

run;

data want;

if _n_ eq 1 then do;

  if 0 then set have(keep=k rgdpinmi rename=(rgdpinmi=_gdp));

  declare hash ha(dataset:'have(keep=k rgdpinmi rename=(rgdpinmi=_gdp))');

   ha.definekey('k');

   ha.definedata('_gdp');

   ha.definedone();

end;

set have;

array x{*} col: ;

sum=0;

do i=1 to dim(x);

if _n_ ne i then do;

                   k=vname(x{i});

                   ha.find();

                     sum + _gdp/x{i};

                      end;

end;

run;



Can you help me solve the problem please? Thanks for your help. (The program worked when there were just 3 dataset (3*3 matrix), but doesn't work with my dataset 49*49 matrix. I realize it has to do with ha.find(), but couldn't figure out a way to solve it. Thanks.


sas-innovate-2024.png

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.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 9155 views
  • 1 like
  • 4 in conversation