DATA Step, Macro, Functions and more

SAS Match Merge -Create new variable

Accepted Solution Solved
Reply
Contributor
Posts: 27
Accepted Solution

SAS Match Merge -Create new variable

[ Edited ]

 

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.


Accepted Solutions
Solution
‎08-08-2016 09:14 AM
Super User
Posts: 19,815

Re: SAS Match Merge -Create new variable

Posted in reply to mgorripati

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;

View solution in original post


All Replies
Super User
Posts: 19,815

Re: SAS Match Merge -Create new variable

Posted in reply to mgorripati

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;

Contributor
Posts: 27

Re: SAS Match Merge -Create new variable

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.

Solution
‎08-08-2016 09:14 AM
Super User
Posts: 19,815

Re: SAS Match Merge -Create new variable

Posted in reply to mgorripati

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;
Super User
Super User
Posts: 7,955

Re: SAS Match Merge -Create new variable

Posted in reply to mgorripati

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;
Super User
Posts: 5,509

Re: SAS Match Merge -Create new variable

Posted in reply to mgorripati

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.

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 634 views
  • 1 like
  • 4 in conversation