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 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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