Why did you add the record counter? Are you doing a many to many merge? SAS data step is not well suited for that. For each level of your id variable (VAR1) you will get the number of observations equal to the larger of those that are in either source data set. The records will be matched one to one and when one of the datasets runs out of records for that level of the id variable the values for the other variables from the dataset will be retained (replicated) onto each extra row. So if merge (1,1),(1,2) with (1,A),(1,B),(1,C) you will get (1,1,A),(1,2,B),(1,2,C). Looks like from your code you want instead to have that third observation be (1,.,C). That can be done without the extra recno variables by adding a OUTPUT and CALL MISSING() statements. Let's assume that TAB1 has extra variable X and TAB2 has extra variable Z. DATA tabf; MERGE tab1 (IN=a) tab2 (IN=b) ; BY var1 ; IF a AND b; OUTPUT; call missing(X,Z); RUN; Normally what people want when doing many-to-many joins is a full outer join that you can get from SQL. proc sql ; create table tabf as select * from tab1 full join tab2 on tab1.var1 = tab2.var1 ; quit; So for my example about with 2 records from TAB1 and 3 records from TAB2 this would yield 6 records (2*3=6). (1,1,A), (1,1,B), (1,1,C), (1,2,A),(1,2,B),(1,2,C)
... View more