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.
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;
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.
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.