BookmarkSubscribeRSS Feed
arjunaj91
Fluorite | Level 6

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 

 

 

 

 

2 REPLIES 2
Kurt_Bremser
Super User

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.

Tom
Super User Tom
Super User

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: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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