BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RichardM
Calcite | Level 5

I need to combine two data tables as below: DATA table1; INPUT  Time Results; Lines; 0  255  6  263  12  236  18  233  ; RUN; DATA table2; INPUT  Time Results_2; Lines; 0  250  6  237  12  233  18  234  ; RUN; The output that I need is as below: Time Results Time Results 0 255 0 250 6 263 6 237 12 236 12 233 18 233 18 234 The usual horizontal concatenation procedures such as MERGE and SET would combine the Time column.  If need to simply paste together the tables side-by-side, by row numbers (in general, I can have more than 2 data sets and may not have the same number of rows), how can I do that? Thanks, -Richard

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

If you can modify the tables to include an actual rownumber variable then just merge by that.

Otherwise you can work a little harder and get SAS to do it from the original data sets.

Make sure that the variable names are unique.

data table1;

input time1 result1 @@;

cards;

0 255 6 263 12 236 18 233

run;

data table2;

input time2 result2 @@;

cards;

0 250 6 237 18 234

run;

data want1 ;

  if (eof1 and eof2) then stop;

  if not eof1 then set table1 end=eof1 ;

  if not eof2 then set table2 end=eof2 ;

  output;

  call missing(of _all_);

run;

Obs    time1    result1    time2    result2

1        0       255         0       250

2        6       263         6       237

3       12       236        18       234

4       18       233         .         .


View solution in original post

6 REPLIES 6
RichardM
Calcite | Level 5

Sorry, my post scrambled the data format.  The output that I need is below.  How can I do that if I have two separate tables (each having two columns of Time and Results) and I need to paste them together side-by-side?

TimeResultsTimeResults
02550250
62636237
1223612233
1823318234
Tom
Super User Tom
Super User

If you can modify the tables to include an actual rownumber variable then just merge by that.

Otherwise you can work a little harder and get SAS to do it from the original data sets.

Make sure that the variable names are unique.

data table1;

input time1 result1 @@;

cards;

0 255 6 263 12 236 18 233

run;

data table2;

input time2 result2 @@;

cards;

0 250 6 237 18 234

run;

data want1 ;

  if (eof1 and eof2) then stop;

  if not eof1 then set table1 end=eof1 ;

  if not eof2 then set table2 end=eof2 ;

  output;

  call missing(of _all_);

run;

Obs    time1    result1    time2    result2

1        0       255         0       250

2        6       263         6       237

3       12       236        18       234

4       18       233         .         .


RichardM
Calcite | Level 5

Thank you very much Tom!  The command using eof worked for my application.

Linlin
Lapis Lazuli | Level 10

you could merge the two tables without by variables:

data want;

merge table1 table2;

run;

RichardM
Calcite | Level 5

Thanks Linlin.  I initially tried what you suggested using:

data want;

merge table1 table2;

run;

but without giving distinct column names for Results, it does not work the way I need it.  With distinct names, then it does work.  Now I know two methods to do this - the usual merge command and the eof commands Tom suggested.

Linlin
Lapis Lazuli | Level 10

you need to rename the variables:

data table1;
input time result @@;
cards;
0 255 6 263 12 236 18 233
run;

data table2;
input time result @@;
cards;
0 250 6 237 18 234
run;
data want;
merge table1(rename=(time=time1 result=result1)) table2(rename=(time=time2 result=result2)) ;
run;
proc print;run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 4726 views
  • 3 likes
  • 3 in conversation