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

Hello all;

I have two datasets, one and two. dataset two is subset of dataset one with less observations. I want to add a numerical binary variable (z) to dataset one, that the value of z=1 for observations that are in dataset two and z=0 for those observations that are not in dataset two. Appreciate for any help.

data one;
input x1 ;
datalines;
...
;
run;

data two;
input x1;
datalines;
...
run;
1 ACCEPTED SOLUTION

Accepted Solutions
qoit
Pyrite | Level 9
data one;
	input x1$;
	datalines;
a
b
c
d
;
run;

data two;
	input x1$;
	datalines;
a
b
run;

proc sql;
	create table three as
		select one.x1,two.x1 as x2,
			case 
				when one.x1 = two.x1 /*STRIP if needed*/
	then 1 else 0 end as z
		from one full join two
			on one.x1 = two.x1;
quit;

Does the above help, SQL is efficient as there is no previous sort required, but will need creating a third dataset.

View solution in original post

2 REPLIES 2
qoit
Pyrite | Level 9
data one;
	input x1$;
	datalines;
a
b
c
d
;
run;

data two;
	input x1$;
	datalines;
a
b
run;

proc sql;
	create table three as
		select one.x1,two.x1 as x2,
			case 
				when one.x1 = two.x1 /*STRIP if needed*/
	then 1 else 0 end as z
		from one full join two
			on one.x1 = two.x1;
quit;

Does the above help, SQL is efficient as there is no previous sort required, but will need creating a third dataset.

Reeza
Super User

Is this based on a single value in the data set or does the whole line need to match?
You need to provide more details but otherwise, just do a MERGE and look at the IN option.

data want;
merge one (in=a) two (in=b);
by <you need to fill in>; *you have not specified the problem well enough to indicate what goes here;
if a & b then z=0;
else if b then z=1;
run;


See Example 5 in the documentation for more details.
https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lestmtsref&docsetTarget=n...

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 652 views
  • 2 likes
  • 3 in conversation