DATA Step, Macro, Functions and more

How to combine data through Concatenate and Interleaving

Reply
Occasional Contributor nrb
Occasional Contributor
Posts: 11

How to combine data through Concatenate and Interleaving

Duncan Hines 

------------------------------------------------

Flavor          Height in cm. 

------------------------------------------------

Devil's food   39

Devil's food   36.5

White            30.5

White            34.5

Yellow          37.0

Yellow          35.0

 

Betty Crocker

------------------------------------------------

Flavor        Height in cm.

------------------------------------------------

Devil's food    35.5

Devil's food   36.0

White             35.5

White             37.5

Yellow            32.5

Yellow           32.5

 

So let's say I have these two data sets above one for Duncan Hines and one for Betty crocker, how do I 

a. Combine these two data sets by Concatenating

b. Combine these two data sets interleaving by flavor

*the variables should be read as how they are spaced and the spacing not be replaced by underscore. 

Super User
Super User
Posts: 7,942

Re: How to combine data through Concatenate and Interleaving

Whilst it is good you have provided test data, it is also important to post these as datasteps (i.e. with datalines) so that we can see the structure also.  I am going to assume therefore in the below that they are exactly the same structure - i.e. type/length:

a) 

data want;

  set duncan betty;

run;

 

b)

as a, then add:

proc sort data=want;

  by flavour;

run;

Trusted Advisor
Posts: 1,018

Re: How to combine data through Concatenate and Interleaving

 

 

data concat;

   either

     set  dh bc;

  or

     set bc dh;

run;

 

data interleave;

   set dh bc;    by flavor;

or

  set bc dh;    by flavor;

run;

 

Take a look at the difference between the  two concat results, and at the difference between the two interleave results  Each difference is attributable to the order of the incoming dataset names in the respective SET statements.

 

Note that the "data interleave" step assumes the each  inoming dataset (DH and BC) are already sorted by flavor.

Super User
Posts: 11,343

Re: How to combine data through Concatenate and Interleaving

Another approach to the interleave is MERGE by flavor which would look like:

 

data want;

    merge duncan betty;

    by flavor;

run;

 

However you need to 1) have each of those sets sorted using the BY variable mentioned and

2) Other variables with the same name from Betty will replace the value from Duncan, so you need to rename them.

 

BUT any combination of data sets you should specify what the result looks like be starting to combine them. MANY approaches will lose the information of which set the values come from if not careful.

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