Published on :
Data Analysis CREATION_INTERNE

Creating a Grouped Table

This code is also available in: Deutsch Español Français
Awaiting validation
This SAS© Viya script uses PROC CAS to interact with the Cloud Analytic Services (CAS) service. The simple.groupBy action is applied to the 'creditqualify' table to create groups based on 'State' and 'State_FIPS'. The 'Credit_Score' variable is used to weight aggregations, and the chosen aggregator is 'MEAN' to calculate the average. Filters 'scoregt' and 'scorelt' are applied to the scores. The result of this operation is stored in a new CAS table named 'ScorePerState'. If the CAS operation is successful (severity 0), the resulting table is modified to format and label the '_Score_' column. The data is then displayed via a fetch action and the table is persistently saved as a .sashdat file.
Data Analysis

Type : CREATION_INTERNE


The examples use generated data (datalines) for the 'creditqualify' table.

1 Code Block
DATA STEP / PROC CAS / simple.groupBy Data
Explanation :
This code begins by creating a temporary SAS table named 'creditqualify' via a DATA STEP, populated with fictitious data for the variables 'State', 'State_FIPS', and 'Credit_Score'.

Next, the 'PROC CAS' procedure is used to interact with the Cloud Analytic Services (CAS) service. A CAS session is established ('session mysession').

The 'simple.groupBy' action is called to build groups based on the 'State' and 'State_FIPS' variables from the 'creditqualify' table. 'Credit_Score' is defined as the weighting variable, and 'MEAN' is specified as the aggregator to calculate the average. The 'scoregt' and 'scorelt' options filter the numeric scores from the groupings. The resulting output table is named 'ScorePerState' and is created in the active caslib, replacing any existing table with the same name.

After the 'simple.groupBy' action is executed, a check is performed on the returned severity code (s.severity). If this code is 0, indicating successful execution, an IF-THEN/DO block is executed.

Inside this block, the 'table.alterTable' action is used to modify the 'ScorePerState' table, setting a label ('Credit Score') and a format ('5.2') for the '_Score_' column.

Then, the 'table.fetch' action is called to retrieve and display the 'State', 'State_FIPS' variables, and the '_Score_' column (now formatted) from the 'ScorePerState' table.

Finally, the 'table.save' action is used to persistently save the 'ScorePerState' table as a 'ScorePerState.sashdat' file in the active caslib, ensuring its availability for future use.
Copied!
1DATA creditqualify;
2 LENGTH State $2. State_FIPS $2. Credit_Score 8;
3 INFILE DATALINES;
4 INPUT State State_FIPS Credit_Score;
5DATALINES;
6NC NC 750
7NC NC 700
8CA CA 800
9CA CA 650
10TX TX 600
11TX TX 720
12NY NY 780
13NY NY 690
14;
15RUN;
16 
17PROC CAS;
18 SESSION mysession;
19 SIMPLE.groupBy RESULT=r STATUS=s /
20 inputs={"State", "State_FIPS"},
21 weight="Credit_Score",
22 aggregator="MEAN",
23 scoregt=0,
24 scorelt=900,
25 TABLE={name="creditqualify"},
26 casout={name="ScorePerState",
27 replace=true};
28RUN;
29 IF (s.severity = 0) THEN DO;
30 TABLE.alterTable / columns={
31 {label="Credit Score", FORMAT="5.2", name="_Score_"}},
32 name="ScorePerState";
33 TABLE.fetch /
34 FORMAT=True,
35 fetchVars={"State", "State_FIPS",
36 {name="_Score_",FORMAT="5.2"}},
37 TABLE={name="ScorePerState"},
38 index=false;
39 
40 TABLE.save /
41 TABLE={name="ScorePerState"},
42 name="ScorePerState.sashdat",
43 replace=True;
44 END;
45RUN;
46QUIT;
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.
Copyright Info : Copyright © SAS Institute Inc. All Rights Reserved