BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Froebel
Fluorite | Level 6

I am merging two data sets horizontally.  Is it possible to first list all the data from table 1 and table 2 columns are empty then list all the data form table 2 where tables 1 columns are empty. In other words, columns should be merged horizontally but data should be merged one table at a time. 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

If I understand you right then something like below should do.

data table1;
  set sashelp.class(obs=3);
run;
data table2;
  set sashelp.air(obs=2);
run;

data want;
  set table1 table2;
run;

proc print data=want;
run;

Patrick_0-1717028894344.png

 

 

View solution in original post

5 REPLIES 5
Patrick
Opal | Level 21

If I understand you right then something like below should do.

data table1;
  set sashelp.class(obs=3);
run;
data table2;
  set sashelp.air(obs=2);
run;

data want;
  set table1 table2;
run;

proc print data=want;
run;

Patrick_0-1717028894344.png

 

 

Froebel
Fluorite | Level 6

Thank you for the quick response. I wanted something like this, only difference is I don't have a fixed count of observations as it is dependent on number of rows for table 1 and 2. 

Tom
Super User Tom
Super User

@Froebel wrote:

Thank you for the quick response. I wanted something like this, only difference is I don't have a fixed count of observations as it is dependent on number of rows for table 1 and 2. 


The first two data steps were just to create some sample datasets to use in the actual data step that you will use.

Tom
Super User Tom
Super User

Your language is unclear.  What does it mean to merge horizontally or merge vertically.

 

In SAS you can SET datasets together and all rows from all datasets will be preserved.  Nothing is "merged" in that case.  Other than perhaps that the set of variables that exists in the new dataset is the union of the sets of variables from the various input datasets.  See the answer from @Patrick which shows an example.  But note that if the intersection of the sets of variable names is not empty there will fewer total variables in the combined dataset, since the values from the like named variables will end up in the same variable.

 

Or you can MERGE datasets by having the observations match on some key variables.  In that case the values from the merged observations will appear on the same output observation.  You use a BY statement to tell SAS which variables to use to do the merging (matching).

 

You can also use a BY statement with a SET statement to interleave the observations from the input datasets based on the values of the BY variables.

Froebel
Fluorite | Level 6
Thank you Tom for the response and sorry for not being able to explain you clearly. What Patrick has shown is what I am trying to achieve. Basically, I want both the table columns side by side but the data should display first from table 1 and then from table 2.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 5 replies
  • 815 views
  • 1 like
  • 3 in conversation