Published on :
ETL INTERNAL_CREATION

Joining Tables from Multiple Caslibs with fedSql.execDirect

This code is also available in: Deutsch Español Français
Awaiting validation
The fedSql.execDirect action allows joining tables from different CAS libraries (caslibs) in a single query. For queries involving multiple caslibs, a two-level name (caslib.table_name) must be used. A query joining tables from multiple caslibs is not eligible for implicit FedSQL pass-through. This example illustrates the creation and manipulation of temporary in-memory CAS data for standalone demonstrations, covering simple joins, filters, aggregations, and the creation of FedSQL views.
Data Analysis

Type : INTERNAL_CREATION


Examples use generated data (datalines) or SASHELP for demonstrations. The Customers, Products, and Sales tables are created in CAS memory from DATA steps.

1 Code Block
PROC FEDSQL Data
Explanation :
This example shows a simple join between the 'Customers_Basic' and 'Sales_Basic' tables based on the customer ID (CUSTID). It selects CUSTID, customer name, and total sales, and stores the result in a new table 'results_basic'.
Copied!
1/* --- Données de démonstration pour l'exemple 1 --- */
2DATA Customers_data;
3 INFILE DATALINES dsd;
4 INPUT CUSTID NAME $ COUNTRY $;
5 DATALINES;
61,Peter Frank,USA
72,Jim Stewart,USA
83,Janet Chien,Japan
94,Qing Ziao,Japan
105,Humberto Sertu,Argentina
11;
12RUN;
13 
14DATA Sales_data;
15 INFILE DATALINES dsd;
16 INPUT PRODID CUSTID TOTALS;
17 DATALINES;
183421,4,781183
191424,3,555789
203975,5,899453
213421,2,2789654
223234,1,189400
231424,1,123456
243422,2,987654
25;
26RUN;
27 
28PROC CASUTIL;
29 load DATA=Customers_data outcaslib="casuser" casout="Customers_Basic" replace;
30 load DATA=Sales_data outcaslib="casuser" casout="Sales_Basic" replace;
31QUIT;
32 
33/* --- Exemple SAS --- */
34PROC CAS;
35 fedSql.execDirect query="
36 create table results_basic {options replace=true} as
37 select
38 C.CUSTID,
39 C.NAME,
40 S.TOTALS
41 from
42 casuser.Customers_Basic as C,
43 casuser.Sales_Basic as S
44 where
45 C.CUSTID = S.CUSTID
46 ";
47 fedSql.execDirect query="select * from results_basic";
48QUIT;
2 Code Block
PROC FEDSQL Data
Explanation :
This example extends the join by including the 'Products_Common' table. It filters the results to show only sales to customers located in 'USA' and sorts the results by total sales in descending order.
Copied!
1/* --- Données de démonstration pour l'exemple 2 --- */
2DATA Customers_data;
3 INFILE DATALINES dsd;
4 INPUT CUSTID NAME $ COUNTRY $;
5 DATALINES;
61,Peter Frank,USA
72,Jim Stewart,USA
83,Janet Chien,Japan
94,Qing Ziao,Japan
105,Humberto Sertu,Argentina
11;
12RUN;
13 
14DATA Products_data;
15 INFILE DATALINES dsd;
16 INPUT PRODID PRODUCT $;
17 DATALINES;
183234,Rice
191424,Corn
203421,Wheat
213422,Oat
223975,Barley
23;
24RUN;
25 
26DATA Sales_data;
27 INFILE DATALINES dsd;
28 INPUT PRODID CUSTID TOTALS;
29 DATALINES;
303421,4,781183
311424,3,555789
323975,5,899453
333421,2,2789654
343234,1,189400
351424,1,123456
363422,2,987654
37;
38RUN;
39 
40PROC CASUTIL;
41 load DATA=Customers_data outcaslib="casuser" casout="Customers_Common" replace;
42 load DATA=Products_data outcaslib="casuser" casout="Products_Common" replace;
43 load DATA=Sales_data outcaslib="casuser" casout="Sales_Common" replace;
44QUIT;
45 
46/* --- Exemple SAS --- */
47PROC CAS;
48 fedSql.execDirect query="
49 create table results_common {options replace=true} as
50 select
51 P.PRODUCT,
52 C.NAME,
53 S.TOTALS,
54 C.COUNTRY
55 from
56 casuser.Products_Common as P,
57 casuser.Sales_Common as S,
58 casuser.Customers_Common as C
59 where
60 P.PRODID = S.PRODID and
61 C.CUSTID = S.CUSTID and
62 C.COUNTRY = 'USA'
63 order by S.TOTALS desc
64 ";
65 fedSql.execDirect query="select * from results_common";
66QUIT;
3 Code Block
PROC FEDSQL Data
Explanation :
This advanced example calculates the total sales for each product by joining 'Products_Adv' and 'Sales_Adv', then filters the results to include only products with total sales exceeding 1,000,000. Results are sorted by total sales.
Copied!
1/* --- Données de démonstration pour l'exemple 3 --- */
2DATA Customers_data;
3 INFILE DATALINES dsd;
4 INPUT CUSTID NAME $ COUNTRY $;
5 DATALINES;
61,Peter Frank,USA
72,Jim Stewart,USA
83,Janet Chien,Japan
94,Qing Ziao,Japan
105,Humberto Sertu,Argentina
11;
12RUN;
13 
14DATA Products_data;
15 INFILE DATALINES dsd;
16 INPUT PRODID PRODUCT $;
17 DATALINES;
183234,Rice
191424,Corn
203421,Wheat
213422,Oat
223975,Barley
23;
24RUN;
25 
26DATA Sales_data;
27 INFILE DATALINES dsd;
28 INPUT PRODID CUSTID TOTALS;
29 DATALINES;
303421,4,781183
311424,3,555789
323975,5,899453
333421,2,2789654
343234,1,189400
351424,1,123456
363422,2,987654
37;
38RUN;
39 
40PROC CASUTIL;
41 load DATA=Customers_data outcaslib="casuser" casout="Customers_Adv" replace;
42 load DATA=Products_data outcaslib="casuser" casout="Products_Adv" replace;
43 load DATA=Sales_data outcaslib="casuser" casout="Sales_Adv" replace;
44QUIT;
45 
46/* --- Exemple SAS --- */
47PROC CAS;
48 fedSql.execDirect query="
49 create table top_selling_products {options replace=true} as
50 select
51 P.PRODUCT,
52 SUM(S.TOTALS) as TOTAL_SALES
53 from
54 casuser.Products_Adv as P,
55 casuser.Sales_Adv as S
56 where
57 P.PRODID = S.PRODID
58 group by
59 P.PRODUCT
60 having
61 SUM(S.TOTALS) > 1000000
62 order by
63 TOTAL_SALES desc
64 ";
65 fedSql.execDirect query="select * from top_selling_products";
66QUIT;
4 Code Block
PROC FEDSQL Data
Explanation :
This example demonstrates deeper integration with Viya by creating a FedSQL view named 'customer_sales_view' from the 'Customers_Viya' and 'Sales_Viya' tables. Subsequently, the view is queried to display records where total sales exceed 500,000.
Copied!
1/* --- Données de démonstration pour l'exemple 4 --- */
2DATA Customers_data;
3 INFILE DATALINES dsd;
4 INPUT CUSTID NAME $ COUNTRY $;
5 DATALINES;
61,Peter Frank,USA
72,Jim Stewart,USA
83,Janet Chien,Japan
94,Qing Ziao,Japan
105,Humberto Sertu,Argentina
11;
12RUN;
13 
14DATA Sales_data;
15 INFILE DATALINES dsd;
16 INPUT PRODID CUSTID TOTALS;
17 DATALINES;
183421,4,781183
191424,3,555789
203975,5,899453
213421,2,2789654
223234,1,189400
231424,1,123456
243422,2,987654
25;
26RUN;
27 
28PROC CASUTIL;
29 load DATA=Customers_data outcaslib="casuser" casout="Customers_Viya" replace;
30 load DATA=Sales_data outcaslib="casuser" casout="Sales_Viya" replace;
31QUIT;
32 
33/* --- Exemple SAS --- */
34PROC CAS;
35 /* Créer une vue FedSQL sur des tables CAS */
36 fedSql.execDirect query="
37 create view customer_sales_view {options replace=true} as
38 select
39 C.NAME,
40 C.COUNTRY,
41 S.TOTALS
42 from
43 casuser.Customers_Viya as C,
44 casuser.Sales_Viya as S
45 where
46 C.CUSTID = S.CUSTID
47 ";
48 
49 /* Interroger la vue FedSQL */
50 fedSql.execDirect query="select * from customer_sales_view where TOTALS > 500000";
51QUIT;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Banner
Expert Advice
Expert
Michael
Responsable de l'infrastructure Viya.
« f you frequently join a small "lookup" table from one caslib to a massive "fact" table in another, ensure the small table is duplicated across all CAS nodes (using the REPLICATE option during load). This allows the join to occur locally on every worker node, drastically reducing network traffic and execution time. »