Help using Base SAS procedures

Merge/Concat

Reply
N/A
Posts: 0

Merge/Concat

Hi,.

I use the code to merge the two files , the code is -

Data One;
INFILE "C:\sas\data_A.dat";
INPUT Time $1-8
Date $9-18
Model $19-24
Unit 25-27
INT 28;
RUN;


proc print data = Data One;
run;

Data Two;
INFILE "C:\sas\data_B.dat";
INPUT Time $1-8
Note $9-16;
RUN;

PROC PRINT DATA = Two;
RUN;

/*Concatenate data One and Two*/
data CONCAT;
SET One and Two;
RUN;

/*Sort One and Two by Time*/
PROC SORT DATA = CONCAT OUT = CAT_SORT;
BY Time;
RUN;

Proc Print Data = CAT_SORT;
RUN;


Thius works well and the files get merged , the problem is that each row is repeated twice with the variables from the first file in the first line and the variable "Note" in the second line with the same variable "Time". So, if I need to export this in Excel then i get ,say, 400 rows when actually the count should be 200. Is there a way that the varible "Note" from second data is printed in the same line as are the other 5 variables from the first data ?

Kindest Regards,
Mark
Trusted Advisor
Posts: 2,113

Re: Merge/Concat

I'm surprised that you didn't get an error from this step:

/*Concatenate data One and Two*/
data CONCAT;
SET One and Two;
RUN;

"and" is treated as a separate dataset.

Your PROC PRINT's also have incorrect syntax.

The SET statement concatenates the two datasets. Concatenate means to "stack" vertically. If you want to string out two datasets to make longer rows, then you need the MERGE statement. Probably something like this will work:

PROC SORT DATA=one; BY time; RUN;
PROC SORT DATA=two, BY time; RUN;

*Merge data One and Two;
data CONCAT;
MERGE One Two;
BY time;
RUN;
Super Contributor
Super Contributor
Posts: 3,174

Re: Merge/Concat

There are three key concepts here: merge, concatenate and interleave, for your consideration, depending on the output requirements, and also, as much, your input data and considering compatability (again, information given up in the SAS log).

Recommended SAS support website - Google advanced search argument below this topic / post:

data step programming merge concatenate site:sas.com

Scott Barry
SBBWorks, Inc. Message was edited by: sbb
SAS Super FREQ
Posts: 8,744

Re: Merge/Concat

Hi:
In addition to Doc and Scott's suggestions, these previous forum postings both have concrete examples of concatenating versus merging SAS datasets:
http://support.sas.com/forums/thread.jspa?messageID=19272䭈
http://support.sas.com/forums/thread.jspa?messageID=2613ਵ

cynthia
Ask a Question
Discussion stats
  • 3 replies
  • 139 views
  • 0 likes
  • 4 in conversation