DATA Step, Macro, Functions and more

How to maintain the row order after joining?

Accepted Solution Solved
Reply
Contributor
Posts: 74
Accepted Solution

How to maintain the row order after joining?

Hello,

 

I am joining two tables, testA, testB.code is as below, but how can I maintain the order of column name value, as H C D, instead of C D H after joining? Thank you so much!

 

data testA;
input name $ number;
datalines;
H 4
C 7
D 9
;
run;

data testB;
input name $ class;
datalines;
H 88
C 99
D 0
;
run;

proc sql;
create table final as
select a.*,b.* from testA a left join testB b on a.name=b.name;
quit;


Accepted Solutions
Solution
‎09-01-2016 05:01 PM
Super User
Posts: 11,343

Re: How to maintain the row order after joining?

One way to implement @Reeza's suggestion:

data testA;
input name $ number;
order = _n_;
datalines;
H 4
C 7
D 9
;
run;

data testB;
input name $ class;
datalines;
H 88
C 99
D 0
;
run;

proc sql;
create table final as
select a.*,b.* from testA a left join testB b on a.name=b.name
order by order;
quit;

View solution in original post


All Replies
Super User
Posts: 19,772

Re: How to maintain the row order after joining?

You can't by default. If you have a variable you can sort by add that your order by statement. 

 

 

If you don't have a sort var you'll have to create one. 

Solution
‎09-01-2016 05:01 PM
Super User
Posts: 11,343

Re: How to maintain the row order after joining?

One way to implement @Reeza's suggestion:

data testA;
input name $ number;
order = _n_;
datalines;
H 4
C 7
D 9
;
run;

data testB;
input name $ class;
datalines;
H 88
C 99
D 0
;
run;

proc sql;
create table final as
select a.*,b.* from testA a left join testB b on a.name=b.name
order by order;
quit;
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 270 views
  • 0 likes
  • 3 in conversation