I am trying to merge two datasets and create a new variable based on if the data exists in both tables.
Sample Code :
DATA output;
merge source(in=a) client(in=b);
by ID;
if a and b then Level='1';
if b and not a then Level='0';
RUN;
The code above works , but does not create the new variable.
How can i create a new variable during the merge process.
I can acheive the goal in multiple steps , but is there a way to acheive the above in single step.
Well, the logic is correct so your new variable, Level, should be created.
Review your log, any drop/keep and output statements.
Without the actual code and log I don't know that we can suggest anything further.
This shows that the logic is fine:
data class1;
set sashelp.class;
keep name age height;
if name in ("Alfred" "Jane" "Mary") then
delete;
run;
data class2;
set sashelp.class;
keep name sex weight;
if name in ("John" "Joyce" "William") then
delete;
run;
proc sort data=class1;
by name;
proc sort data=class2;
by name;
data want;
merge class1 (in=a) class2(in=b);
by name;
if a and b then
level=1;
else if a and not b then
level=0;
else
level=-1;
run;
What does your log say?
It should have some sort of error/warning because your last line is missing a semicolon.
if b and not a then Level='0';
RUN;
Hi Reez,
Thanks for catching the error, It is not actual code . I have typed the code here as an example.
my actual code with similar logic runs fine but it is failing to create the new variable.
Well, the logic is correct so your new variable, Level, should be created.
Review your log, any drop/keep and output statements.
Without the actual code and log I don't know that we can suggest anything further.
This shows that the logic is fine:
data class1;
set sashelp.class;
keep name age height;
if name in ("Alfred" "Jane" "Mary") then
delete;
run;
data class2;
set sashelp.class;
keep name sex weight;
if name in ("John" "Joyce" "William") then
delete;
run;
proc sort data=class1;
by name;
proc sort data=class2;
by name;
data want;
merge class1 (in=a) class2(in=b);
by name;
if a and b then
level=1;
else if a and not b then
level=0;
else
level=-1;
run;
Should work fine, check out the below. Also note that these binary if statements are far simpler if you use ifc/ifn.
data want; merge sashelp.class (in=a) sashelp.class (in=b where=(age > 13)); by name; level=ifc(a and b,"1","0"); run;
You have posted working code. You should expect that A and B will be dropped automatically, but LEVEL will be there in the final data set.
Since what you posted is not the actual code you are working with, you may have to post something closer to the actual code to allow someone else to debug it.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: