DATA Step, Macro, Functions and more

Compare two or more columns in two dataset and add one more field with 1 or 0

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

Compare two or more columns in two dataset and add one more field with 1 or 0

Hi All,

 

I've two datasets , called test1 and test2 , i want to create one more dataset based upon "name" fields with all the data of test1 and test2. I want to add one more field like "action_flag"  with value 1 or 0. if name match then action_flag=1 else 0

 

data test1;
input id name $;
cards;
1 ab
2 cf
3 er
;
run;
data test2;
input id name $;
cards;
1 ab
2 df
3 er
4 hj
;
run;

 

Thank you in advance.

 


Accepted Solutions
Solution
‎02-21-2018 03:41 AM
Super User
Super User
Posts: 9,790

Re: Compare two or more columns in two dataset and add one more field with 1 or 0

Posted in reply to singhsahab
proc sort data=test1;
  by name;
run;
proc sort data=test2;
  by name;
run;
data want;
  merge test1 (in=a) test2 (in=b);
  by name;
  action_flag=ifn(a and b,1,0);
run;

View solution in original post


All Replies
Solution
‎02-21-2018 03:41 AM
Super User
Super User
Posts: 9,790

Re: Compare two or more columns in two dataset and add one more field with 1 or 0

Posted in reply to singhsahab
proc sort data=test1;
  by name;
run;
proc sort data=test2;
  by name;
run;
data want;
  merge test1 (in=a) test2 (in=b);
  by name;
  action_flag=ifn(a and b,1,0);
run;
Super User
Posts: 6,899

Re: Compare two or more columns in two dataset and add one more field with 1 or 0

Posted in reply to singhsahab

I'm interpreting the problem slightly differently:

 

proc sort data=test1;

by id;

run;

proc sort data=test2;

by id;

run;

data want;

merge test1 (rename=(name=name1))

test2 (rename=(name=name2));

by id;

action_flag = (name1=name2);

run;

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 112 views
  • 1 like
  • 3 in conversation