DATA Step, Macro, Functions and more

Key hash variable does not exist in data set

Reply
Super Contributor
Posts: 308

Key hash variable does not exist in data set

Hello,

 

How can I force SAS to throw a message , a note or anything to make me careful in case I use, by mistake, a variable

in hash find method, variable which does not exist in data step.

Consider the following example (only to better explain my question):

 

data want;
set sashelp.class;
if _n_=1 then
        do;
                declare hash pr(dataset:"sashelp.class");
                pr.definekey('age');
                pr.definedata('name');
                pr.definedone();
        end;

rc=pr.find(key:agee);
if rc eq 0 then x=1;
run;

 

SAS does not throw any note, message in the log. It only creates the numeric variable agee which is missing and as a consequence nothing is find in hash.

 

Is there a simple way , maybe an option or something similar, to identify such typing mistakes ?

 

10x

Respected Advisor
Posts: 3,799

Re: Key hash variable does not exist in data set

pr.find()
Super Contributor
Posts: 308

Re: Key hash variable does not exist in data set

Posted in reply to data_null__

yes this will help in case I have to search the same variabe, but if i need to search a different variable?

 

To be more precise. Let's say I have the following syntax:

data want;
set have;
if _n_=1 then
        do;
                call missing(age);
                declare hash pr(dataset:"sashelp.class");
                pr.definekey('age');
                pr.definedata('name');
                pr.definedone();
        end;

rc=pr.find(key:age_search);
if rc eq 0 then x=1;
run;

 

At one moment somebody changes the database and the variable age_search is no more present in the database have.

How can I force SAS to drop a note or even stop so I can change the program before the users see the incorect results .

Respected Advisor
Posts: 3,799

Re: Key hash variable does not exist in data set

You can program a check for the existence of the variable AGE_SEARCH in HAVE before the data step.
Super User
Posts: 11,343

Re: Key hash variable does not exist in data set

One way though not the most robust:

data want;
   set have;
   if missing(age_search) then do;
      put "ERROR: Missing value for variable age_search in dataset have";
      stop;
   end;
   if _n_=1 then
           do;
                   call missing(age);
                   declare hash pr(dataset:"sashelp.class");
                   pr.definekey('age');
                   pr.definedata('name');
                   pr.definedone();
           end;

   rc=pr.find(key:age_search);
   if rc eq 0 then x=1;
run;

If your data source is likely to change many of your variables then you would check for each one. The Want data set will have no observations but will be created.

 

Respected Advisor
Posts: 3,156

Re: Key hash variable does not exist in data set

[ Edited ]

Loko wrote:

 

SAS does not throw any note, message in the log. It only creates the numeric variable agee which is missing and as a consequence nothing is find in hash.

 

SAS is just a software, so it does what it is told and it can't read your mind. In your case, SAS does not know whether or not it is your intention to create a new variable like that. What you are asking, I am afraid, is beyond what any software  can offer. 

 

You can, though, leverage Macro languge, to kinda get what you want . However, it still does not change SAS's behavior.

Super Contributor
Posts: 308

Re: Key hash variable does not exist in data set

Ok, I was affraid there is no simple for solution for that.

 

@Haikuo I don't think is about reading my mind, I believe it is just something that can be improved in SAS since , for example it can throw "Uninitialise notes" in situations like:

 

data want;
set sashelp.class;
if xxx < 9 then c=9;
a=yyy+1;
run;

Super User
Posts: 11,343

Re: Key hash variable does not exist in data set

In a somewhat snarky mode, I would say if this is a production data base then however arbitrarily changes a data table needs to have a serious attitude adjustment.

If you are working with a developing database then your operating procedures should have something about when such changes are made and notification rules.

Respected Advisor
Posts: 3,156

Re: Key hash variable does not exist in data set

@Loko In light of your example, I agree that it does seem to have some inconsistency here.

 

Haikuo

Ask a Question
Discussion stats
  • 8 replies
  • 279 views
  • 2 likes
  • 4 in conversation