BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10
data have1; input x y; cards; 1 2 3 4 ; run; data have2; input r s; cards; 5 6 7 8 9 0 ; run; data combine; set have1 have2; run; data combine creates a four column dataset. is there a way to stack have1 and have2 so that have2 is underneath have1 despite having different variable names. Objective is to only have 2 columns
2 REPLIES 2
ballardw
Super User

Please reformat that example and probably better to place in a text box.

 

You can use data set options to rename the variables that should align

Data want;
    set one
        two (rename=(r=x s=y))
   ;
run;

for example renames the R to X and S to Y if that is the way you want them aligned (not stated). HOWEVER when you do this it is up to you make sure that the variable types are the same in both sets.

This rename is one of the data set options can be used anywhere, not just in a data step set statement. Other options can drop or keep variables, subset data and create a number of sometimes useful temporary variables.

Patrick
Opal | Level 21

One option is to use a SQL Union ALL. The variable names in the combined data set will have the names as per the first select statement.

data have1;
  input x y;
  cards;
1 2 
3 4 
5 6
;

data have2;
  input r s;
  cards;
5 6 
7 8 
9 0 
;

proc sql;
  create table combined as
  select x, y
  from have1
  union all
  select r, s
  from have2
  ;
quit;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 1060 views
  • 0 likes
  • 3 in conversation