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

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
Obsidian | Level 7

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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