BookmarkSubscribeRSS Feed
Loko
Barite | Level 11

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

8 REPLIES 8
Loko
Barite | Level 11

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 .

data_null__
Jade | Level 19
You can program a check for the existence of the variable AGE_SEARCH in HAVE before the data step.
ballardw
Super User

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.

 

Haikuo
Onyx | Level 15

@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.

Loko
Barite | Level 11

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;

ballardw
Super User

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.

Haikuo
Onyx | Level 15

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

 

Haikuo

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 1121 views
  • 2 likes
  • 4 in conversation