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

Can I use functions with an sql vertical join?   Something such as 

 

proc sql;

  select a, sum(b) as tot_b

  from X

  union

  select a, sum(b) as tot_b

  from Y

  group by a;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

First, there is no such thing as a "vertical join". UNION ALL does a concatenation of tables. UNION without ALL eliminates duplicates. CORR requests matching by variable names (only same named variables are concatenated).

 

So it seems like you want to concatenate X and Y first and then GROUP BY to get the sums. Something like:

 

proc sql;
select  a, sum(b) as tot_b
from 
  (select a, b
   from X
   union all corr
   select a, b
   from Y)
 group by a;
 quit;

Untested. Not sure about the use of parentheses here. You might have to experiment.

PG

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

How do you define a Vertical Join?

Batman
Quartz | Level 8

The set operator "union" allows you to append two datasets in SQL

PGStats
Opal | Level 21

Yes, you can use summary functions. Note that each subquery is processed separately. In your example, the group by clause will refer only to the second subquery. Without a group by clause, the first subquery will remerge tot_b with every row from table X.

PG
Batman
Quartz | Level 8

If there are some values of variable "a" that are in both input datasets X and Y, can values of variable "b" be grouped by "a" in the same query (i.e. the one where they are joined)?

PGStats
Opal | Level 21

First, there is no such thing as a "vertical join". UNION ALL does a concatenation of tables. UNION without ALL eliminates duplicates. CORR requests matching by variable names (only same named variables are concatenated).

 

So it seems like you want to concatenate X and Y first and then GROUP BY to get the sums. Something like:

 

proc sql;
select  a, sum(b) as tot_b
from 
  (select a, b
   from X
   union all corr
   select a, b
   from Y)
 group by a;
 quit;

Untested. Not sure about the use of parentheses here. You might have to experiment.

PG