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
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);
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.
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);
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.