data tst1;
input a $ yr b;
datalines ;
a 2000 1
b 2000 2
b 2003 3
c 1996 4
d 2013 3
;
run;
data tst2;
input a $ yr c;
datalines ;
a 2000 10
b 2003 13
d 2000 13
;
run;
proc sort data = tst1 ; by a;run;
proc sort data = tst2 ; by a;run;
data tst3;
merge tst1 (in=x) tst2 (in=y);
by a;
if x then output;
run;
When I run this the result is
a yr b c
a 2000 1 10
b 2003 2 13
b 2003 3 13
c 1996 4 .
d 2000 3 13
Why am I getting the same year value for b? year corresponding to b =2 in tst1 is 2000
@riyaaora275 wrote:
data tst1; input a $ yr b; datalines ; a 2000 1 b 2000 2 b 2003 3 c 1996 4 d 2013 3 ; run; data tst2; input a $ yr c; datalines ; a 2000 10 b 2003 13 d 2000 13 ; run; proc sort data = tst1 ; by a;run; proc sort data = tst2 ; by a;run; data tst3; merge tst1 (in=x) tst2 (in=y); by a; if x then output; run;
When I run this the result is
a yr b c
a 2000 1 10
b 2003 2 13
b 2003 3 13
c 1996 4 .
d 2000 3 13
Why am I getting the same year value for b? year corresponding to b =2 in tst1 is 2000
You do NOT merge datasets that have variables in common other than those in the by statement.
When encountering the value "b" for variable a, this happens:
Because you only have yr=2003 for b=2 in tst2. I think this gives you your desired result?
data tst1;
input a $ yr b;
datalines ;
a 2000 1
b 2000 2
b 2003 3
c 1996 4
d 2013 3
;
run;
data tst2;
input a $ yr c;
datalines ;
a 2000 10
b 2003 13
d 2000 13
;
run;
proc sort data = tst1 ; by a yr; run;
proc sort data = tst2 ; by a yr; run;
data tst3;
merge tst1 (in=x) tst2 (in=y);
by a yr;
if x then output;
run;
@riyaaora275 wrote:
data tst1; input a $ yr b; datalines ; a 2000 1 b 2000 2 b 2003 3 c 1996 4 d 2013 3 ; run; data tst2; input a $ yr c; datalines ; a 2000 10 b 2003 13 d 2000 13 ; run; proc sort data = tst1 ; by a;run; proc sort data = tst2 ; by a;run; data tst3; merge tst1 (in=x) tst2 (in=y); by a; if x then output; run;
When I run this the result is
a yr b c
a 2000 1 10
b 2003 2 13
b 2003 3 13
c 1996 4 .
d 2000 3 13
Why am I getting the same year value for b? year corresponding to b =2 in tst1 is 2000
You do NOT merge datasets that have variables in common other than those in the by statement.
When encountering the value "b" for variable a, this happens:
@riyaaora275: I recommend setting the MSGLEVEL= system option to I (e.g. in your autoexec file):
options msglevel=I;
Then you'll get messages in the log such as
INFO: The variable yr on data set WORK.TST1 will be overwritten by data set WORK.TST2.
which will help you to understand the results in situations like this.
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 25. 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.