I have three tables and I want to vertically merge those tables
Table A
Name Money
X 2000
Y 4000
Table B
Name Money
X 600
Z 400
Table C
Name Money
Y 8000
Z 600
I want to combine these tables into single table like this using proc sql so that I can save the table:
Name Money_1 Money_2 Money_3
X 2000 600 0
Y 4000 0 8000
Z 0 400 600
Like this I have 50 tables and I want to merge them vertically using proc sql
One solution is joining two tables at once using full join but in select statement choose the Name variable of table with the most rows. That works for few tables but i have over 50 tables and I need a feasible solution to this problem
There is no "vertical merge". You can either stack datasets vertically, or join/merge them horizontally.
What you want can be most simply achieved by first stacking, with an interleave, and then transposing:
data long;
length dname $41 dsname $32;
set
table_a
table_b
table_c
indsname=dname
;
by name;
dsname = scan(dname,2,"."); /* removes library name */
run;
proc transpose
data=long
out=want
;
by name;
var money;
id dsname;
run;
With additional logic in the data step, you can create variable names for the transpose to your liking.
The only thing that should be automated is the list of dataset names for the SET statement. You can use a colon wildcard there (lib.table:), or retrieve the list from DICTIONARY.TABLES in SQL into a macro variable.
I would just leave it in a tall structure. You can then produce that report using a reporting procedure like PROC REPORT, TABLULATE or FREQ.
data tall;
length indsname dsname $41 ;
set A B C indsname=indsname;
by name;
dsname=indsname;
run;
Obs dsname Name Money 1 WORK.A X 2000 2 WORK.B X 600 3 WORK.A Y 4000 4 WORK.C Y 8000 5 WORK.B Z 400 6 WORK.C Z 600
proc freq ;
tables name*dsname;
weight money ;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.