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: 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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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