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

I have two datasets:

DATA List_ID;
     INPUT ID;
DATALINES;
1
2
3
4
;
DATA Flag_ID;
    INPUT ID1 ID2;
DATALINES;
1, 5 
2, 3
6, 4
;

I am trying to create a flag if the IDs in Flag_ID appear in List_ID, adding two new columns to Flag_ID (FlagID1 FLAGID2). I used hash table to do this when flagging one ID variable, with the same name:

 

data FlagOne;
  if _N_ = 1 then do;  
    declare hash TST(dataset: "List_ID"); 
    TST.definekey("id1"); /*Pretending the ID variable is ID1 to match the other variable */
    TST.definedone();
  end; 
  set Flag_ID
  	ID1_Flag= (TST.check() = 0);
run;

How can I do a similar operation using two variables and creating two new flags? My desired output is:

 

ID1 ID2 FlagID1 FlagID2

1     5        1       0

2     3        1       1

6     4        0       1

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You need to use the key: argument to the check method:

data FlagOne;
  if _N_ = 1 then do;
    declare hash TST(dataset: "List_ID");
    TST.definekey("id");
    TST.definedone();
  end;
  set Flag_ID;
  ID1_Flag = (TST.check(key:ID1) = 0);
  ID2_Flag = (TST.check(key:ID2) = 0);
run;

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User

You need to use the key: argument to the check method:

data FlagOne;
  if _N_ = 1 then do;
    declare hash TST(dataset: "List_ID");
    TST.definekey("id");
    TST.definedone();
  end;
  set Flag_ID;
  ID1_Flag = (TST.check(key:ID1) = 0);
  ID2_Flag = (TST.check(key:ID2) = 0);
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 796 views
  • 1 like
  • 2 in conversation