BookmarkSubscribeRSS Feed
nrb
Fluorite | Level 6 nrb
Fluorite | Level 6

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. 

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

mkeintz
PROC Star

 

 

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

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-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
  • 3 replies
  • 893 views
  • 0 likes
  • 4 in conversation