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

hello,

 

I have two dataset and I want to horizontal stacking them. the two dataset have same variable name but different rows. I would like to keep all cols and can add a_ , b_ in front of original col names. how can I achieve this?

data df_a;
infile datalines dsd truncover;
input id sex $ stop;
datalines;
1,Female,0
1,M,1
1,Male,0
1,F,0
1,,1
;

data df_b;
infile datalines dsd truncover;
input id sex $ stop;
datalines;
2,Female,0
1,M,1
1,Male,0
2,F,0
;

 there is no need to merge by id. just simply put two tables together. the output will look like below:

stataq_0-1713460802043.png

any suggestion? 

1 ACCEPTED SOLUTION

Accepted Solutions
sbxkoenk
SAS Super FREQ
/* below df_c will contain only 4 observations --> wrong */
data df_c;
 set df_a(rename=(id=a_id sex=a_sex stop=a_stop));
 set df_b(rename=(id=b_id sex=b_sex stop=b_stop));
run;

/* below df_c will contain      5 observations --> okay  */
options MERGENOBY=WARN;
data df_c;
 merge df_a(rename=(id=a_id sex=a_sex stop=a_stop))
       df_b(rename=(id=b_id sex=b_sex stop=b_stop));
run;
/* end of program */

Koen

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26
data want;
    set df_a df_b;
run;
--
Paige Miller
sbxkoenk
SAS Super FREQ
proc datasets library=work NoList NoDetails memtype=data;
 delete want; run;
QUIT;
proc append base=want data=df_a; run;
proc append base=want data=df_b; run;

Koen

stataq
Quartz | Level 8

simple set statement will create vertical staking instead. 

sbxkoenk
SAS Super FREQ

Oh sorry. You want horizontal stacking (instead of vertical). 
PROC APPEND cannot do this.
Let me re-write.

 

Koen

sbxkoenk
SAS Super FREQ
/* below df_c will contain only 4 observations --> wrong */
data df_c;
 set df_a(rename=(id=a_id sex=a_sex stop=a_stop));
 set df_b(rename=(id=b_id sex=b_sex stop=b_stop));
run;

/* below df_c will contain      5 observations --> okay  */
options MERGENOBY=WARN;
data df_c;
 merge df_a(rename=(id=a_id sex=a_sex stop=a_stop))
       df_b(rename=(id=b_id sex=b_sex stop=b_stop));
run;
/* end of program */

Koen

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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