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-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1204 views
  • 1 like
  • 3 in conversation