DATA A;
INPUT id name$ Height;
datalines;
1 A 1
3 B 1
5 C 2
7 D 2
9 E 2
;
run;
DATA B;
INPUT id name$ weight;
datalines;
2 A 2
4 B 3
5 C 4
7 D 5
;
run;
;
proc sort data=A;
by ID;
run;
;
proc sort data=B;
by ID;
run;
DATA xyz;
MERGE A B;
BY ID;
RUN;
Hi:
I agree, when I run the original code, I do not have any errors in the SAS log. However, the issue might be that the output is not what was expected or desired. Here's the output I get when I do a PROC PRINT on the the original work.xyz data after the merge:
Here's the log from the original code:
However, I think that this might be a case where using IN= option for the merge might be useful. Also, in this case, with the original code, it appears that there might be issues, unexpected results for ID=1, NAME=A and ID=2, NAME=A and the same with NAME=B.
So if you change the code a bit so that there are 2 different variables, NAMEA in dataset A and NAMEB in dataset B, then this code can be run as a test:
DATA A;
INPUT id nameA $ Height;
datalines;
1 A 1
3 B 1
5 C 2
7 D 2
9 E 2
;
run;
DATA B;
INPUT id nameB $ weight;
datalines;
2 A 2
4 B 3
5 C 4
7 D 5
;
run;
proc sort data=A;
by ID;
run;
proc sort data=B;
by ID;
run;
DATA xyz A_only B_only BOTH ;
MERGE A(in=froma) B(in=fromb);
BY ID;
output xyz;
** now test merge in more depth by testing in= option values;
** and outputting observations to different output data tables;
if froma = 1 and fromb = 1 then output both;
else if froma=1 and fromb = 0 then output A_only;
else if froma=0 and fromb = 1 then output B_only;
RUN;
proc print data=xyz;
title '1) XYZ data table';
run;
proc print data=A_only;
title '2) A_only data table';
run;
proc print data=B_only;
title '3) B_only data table';
run;
proc print data=both;
title '4) Both data table';
run;
title;
Then this is what is in the log:
And the PROC PRINT output:
I'm not clear on whether the original code posted has a logic program or a data problem. But using IN= might help clarify the solution.
Cynthia
Please do not type in ALL CAPITAL LETTERS.
When you get errors in the log, then please show us the entire log. In fact, please do that for this problem and all future problems. Do not paraphrase error messages.
When I run your code, I get no errors at all.
Hi:
I agree, when I run the original code, I do not have any errors in the SAS log. However, the issue might be that the output is not what was expected or desired. Here's the output I get when I do a PROC PRINT on the the original work.xyz data after the merge:
Here's the log from the original code:
However, I think that this might be a case where using IN= option for the merge might be useful. Also, in this case, with the original code, it appears that there might be issues, unexpected results for ID=1, NAME=A and ID=2, NAME=A and the same with NAME=B.
So if you change the code a bit so that there are 2 different variables, NAMEA in dataset A and NAMEB in dataset B, then this code can be run as a test:
DATA A;
INPUT id nameA $ Height;
datalines;
1 A 1
3 B 1
5 C 2
7 D 2
9 E 2
;
run;
DATA B;
INPUT id nameB $ weight;
datalines;
2 A 2
4 B 3
5 C 4
7 D 5
;
run;
proc sort data=A;
by ID;
run;
proc sort data=B;
by ID;
run;
DATA xyz A_only B_only BOTH ;
MERGE A(in=froma) B(in=fromb);
BY ID;
output xyz;
** now test merge in more depth by testing in= option values;
** and outputting observations to different output data tables;
if froma = 1 and fromb = 1 then output both;
else if froma=1 and fromb = 0 then output A_only;
else if froma=0 and fromb = 1 then output B_only;
RUN;
proc print data=xyz;
title '1) XYZ data table';
run;
proc print data=A_only;
title '2) A_only data table';
run;
proc print data=B_only;
title '3) B_only data table';
run;
proc print data=both;
title '4) Both data table';
run;
title;
Then this is what is in the log:
And the PROC PRINT output:
I'm not clear on whether the original code posted has a logic program or a data problem. But using IN= might help clarify the solution.
Cynthia
You seem to have two different KEY variables. Which one is the important one? ID or NAME?
Merging by NAME leads to a different result
than merging by ID.
Hi,
I'm not sure if your subject line has been truncated; was there more to it?
Please provide a data step with datalines to show what output data set you are expecting, along with a brief explanation of the logic rules that generate your output.
Thanks & kind regards,
Amir.
I think we need to hear from @Shailesh_R_T about what the problem is, in much more detail. Everyone guessing different things isn't really a good approach.
Hey @PaigeMiller i am beginner, currently learning SAS basics. Even i don't have solution for this, i tried with proc print it worked out but still don't know if its the right way, if anyone came across solution for this please approach or discuss. Can anyone help me with mock tests or daily quiz for practice purpose for base sas examination
@Shailesh_R_T wrote:
Hey @PaigeMiller i am beginner, currently learning SAS basics. Even i don't have solution for this, i tried with proc print it worked out but still don't know if its the right way, if anyone came across solution for this please approach or discuss
I asked to see the log. If there are errors in the log, SHOW US the log! Even if you are a beginner, you can explain better and provide us with more information (which we have specifically requested).
please have a look a discuss
Many people here refuse to download file attachments.
Please paste whatever text you want us to see into your reply.
Hi,
Part of the subject says "I am unable to execute merge...", but the log shows the merge has been executed.
Another part of the subject says "SAS is not able to detect data set A and B...", but the log shows data set A and B were detected when they were used in the merge.
Are you happy with the results of the final data set? If yes, then what is your question? If no, then what result did you expect to get instead - please supply a data step with datalines showing the output you expect.
Thanks & kind regards,
Amir.
i am not happy with the results. data set A and B has not merged in output window despite showing no error in log. As per my knowledge the code is also right. I have attached code and log window for your referance
Hi,
Please share the result that you have and also show us the result that you want (using a data step with datalines) so that we can see what the difference is between the result that you have and the result that you want.
Please also tell us some rows that you think are incorrect and why.
Thanks & kind regards,
Amir.
I got the expected result using proc print syntax at the end
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.
Ready to level-up your skills? Choose your own adventure.