find method issue

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

find method issue

Hi all,

 

I am trying to use a hashtable  to output the data from a loop up data set in a given set when the find() method finds a match, and in

another set otherwise.

 

The problem is that when there is no match, the output I get contains the data in the hashtable corresponding to the previous

observations for which there was a match. Hence the find() method. 

 

Here is the code:

 

data matchAdmin nonmatchAdmin ;
       if 0 then set master_Eir nobs=Nb;
      if Nb^=0 then
            do;
                declare Hash masterHash (dataset:'master_Eir');
                masterHash.DefineKey ('master_eircode_checked');
                masterHash.DefineData(ALL:'YES');
                masterHash.DefineDone();
               do k=1 to n;
                    set admin_Eir point=k nobs=n;
                    rc=masterHash.find(key:admin_eircode_checked);
                    if rc=0 then do;
                         matchFound=1;
                         output matchAdmin;
                     end;
                    else do;
                       matchFound=0;
                      output nonmatchAdmin;
                   end;
        end;
end;
run;

 

Thanks for your help,

 

Thierry


Accepted Solutions
Solution
‎01-05-2017 01:00 PM
Super User
Posts: 5,352

Re: find method issue

Yes, there is nothing in your code that would re-set those variables if the FIND fails.  There are a couple of ways to go about this. 

 

(1) Before outputting to NONMATCHADMIN, hard-code statements that set these variables to missing. 

 

(2) Drop the variables from NONMATCHADMIN that should be missing:

 

data MatchAdmin nonMatchAdmin (drop=list of some variables here);

View solution in original post


All Replies
Super User
Posts: 11,101

Re: find method issue

Example data is helpful if you want code tested/debugged.

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... has instructions on how to create datastep code from an existing data set that you can paste into a text box here and let us generate data for testing.

Solution
‎01-05-2017 01:00 PM
Super User
Posts: 5,352

Re: find method issue

Yes, there is nothing in your code that would re-set those variables if the FIND fails.  There are a couple of ways to go about this. 

 

(1) Before outputting to NONMATCHADMIN, hard-code statements that set these variables to missing. 

 

(2) Drop the variables from NONMATCHADMIN that should be missing:

 

data MatchAdmin nonMatchAdmin (drop=list of some variables here);

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 143 views
  • 0 likes
  • 3 in conversation