BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Melk
Lapis Lazuli | Level 10

I have 2 datasets, call them data1 and data2. They both have the same set of variables, one of them being ID.

 

data2 is a subset of data1. I want to create a variable in data1 that takes on the value of 1 when the ID variable is present in data2, and 0 if not in data2.

 

What is the most efficient way to do this?

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Efficient means no sorting.

Like this ?

data WANT; 
  if _N_ = 1 then do;  
    declare hash VET(dataset: "DATA2"); 
    VET.definekey("KEYVAR"); 
    VET.definedone();
  end; 
  set DATA1; 
  FLAG=VAT.check();
run;

 

 

 

 

 

View solution in original post

6 REPLIES 6
Shmuel
Garnet | Level 18

If needed sort datasets by ID then use merge:

data want;
merge data1(in=in1)
      data2(in=in2 keep=ID)
 ;  by ID;
       if in1 and in2 then flag=1;
       else flag=2;
run;
Melk
Lapis Lazuli | Level 10

Hello - thanks for the response. I tried this and the merged dataset has duplicate observations. Is there a way to just add a variable, flag, to data1, with value 1 if the ID variable is in data2, an value 0 if not? (without doing a merge)?

ChrisNZ
Tourmaline | Level 20

Efficient means no sorting.

Like this ?

data WANT; 
  if _N_ = 1 then do;  
    declare hash VET(dataset: "DATA2"); 
    VET.definekey("KEYVAR"); 
    VET.definedone();
  end; 
  set DATA1; 
  FLAG=VAT.check();
run;

 

 

 

 

 

andreas_lds
Jade | Level 19

The function check returns 0 if the key was found in the hash object.

 

FLAG = (VAT.check() = 0);

Melk
Lapis Lazuli | Level 10

Thank you both! worked perfectly with the edit from andreas.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 6 replies
  • 5237 views
  • 3 likes
  • 4 in conversation