Published on :
ETL CREATION_INTERNE

Creating a Group-By Table

This code is also available in: Deutsch Español Français
Awaiting validation
This example demonstrates how to create a group-by table using CAS (Cloud Analytic Services) actions on the SAS© Viya platform. The input table CreditQualify is grouped by State variables and State FIPS codes. The Credit_Score variable is used as a weighting variable to calculate the mean (MEAN) of credit scores per group. Intermediate and final results are managed using CAS actions such as simple.groupBy, table.alterTable, table.fetch, and table.save. Once grouping is complete, the output table ScorePerState is enhanced with a formatted _Score_ column and saved in .sashdat format for easy reuse. The severity code check (s.severity = 0) ensures that operations are executed only upon successful completion of the previous action.
Data Analysis

Type : CREATION_INTERNE


The example CreditQualify table is created using a DATA step with datalines to ensure the example's autonomy. The data is fictitious and includes the variables State (character string), State_FIPS (numeric), and Credit_Score (numeric).

1 Code Block
PROC CAS / DATA STEP Data
Explanation :
The initial DATA step creates a temporary table named 'CreditQualify' with example data for the 'State' (state), 'State_FIPS' (state FIPS code), and 'Credit_Score' (credit score) variables. The PROC CAS statement activates the SAS Cloud Analytic Services (CAS) session. The simple.groupBy action is then used to build groups based on the 'State' and 'State_FIPS' variables. 'Credit_Score' is defined as the weighting variable, and the aggregator is set to 'MEAN' to calculate the average credit scores per group. The 'scoregt' and 'scorelt' options define the lower and upper limits of scores to include. The result of this action is an output table named 'ScorePerState' in the active caslib. A severity code check (s.severity = 0) ensures that subsequent actions are executed only upon success. If the groupBy action succeeds, the table.alterTable action is used to modify the '_Score_' column by assigning it the label 'Credit Score' and format '5.2'. Then, the table.fetch action retrieves the 'State', 'State_FIPS', and '_Score_' variables from the 'ScorePerState' table, applying the specified format for '_Score_'. Finally, the table.save action saves the 'ScorePerState' table in .sashdat format in the active caslib.
Copied!
1DATA work.CreditQualify;
2 INPUT State $ State_FIPS Credit_Score;
3 DATALINES;
4NC 37 700
5NC 37 750
6NY 36 600
7NY 36 620
8CA 06 800
9CA 06 820
10;
11RUN;
12 
13PROC CAS;
14 SESSION mysession;
15 SIMPLE.groupBy RESULT=r STATUS=s /
16 inputs={"State", "State_FIPS"},
17 weight="Credit_Score",
18 aggregator="MEAN",
19 scoregt=0,
20 scorelt=900,
21 TABLE={name="CreditQualify"},
22 casout={name="ScorePerState",
23 replace=true};
24RUN;
25 IF (s.severity = 0) THEN DO;
26 TABLE.alterTable / columns={
27 {label="Credit Score", FORMAT="5.2", name="_Score_"}},
28 name="ScorePerState";
29 TABLE.fetch /
30 FORMAT=True,
31 fetchVars={"State", "State_FIPS",
32 {name="_Score_",FORMAT="5.2"}},
33 TABLE={name="ScorePerState"},
34 index=false;
35 
36 TABLE.save /
37 TABLE={name="ScorePerState"},
38 name="ScorePerState.sashdat",
39 replace=True;
40 END;
41RUN;
42QUIT;
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.