Published on :
DATA Step CREATION_INTERNE

Create a DATA Step View

This code is also available in: Deutsch Español Français
Awaiting validation
A DATA Step view can be created from a CAS table by executing the DATA step locally in a Base SAS© session, not in a CAS server session. Views must be stored in a Base SAS© library (created with the V9 engine). Although the DATA Step executed in CAS does not support the VIEW= option, a SAS© view can be created using a DATA step executed in SAS©, which uses a CAS table as input. Views can contain VARCHAR variables and can read values from these variables from SAS© views. However, the DATA step cannot directly read VARCHAR variables contained in CAS tables or other data sources without going through a view. A DATA Step view can include VARCHAR variables, but a SAS© dataset cannot directly. Finally, views can access datasets stored in non-Base libraries (including CAS), but update views are not supported.
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) or tables created directly in CAS to ensure their autonomy.

1 Code Block
DATA STEP Data
Explanation :
This example demonstrates the creation of a simple DATA Step view 'MaVueBasique'. A source dataset 'MonJeuDeDonnees' is first created with datalines. Then, the DATA step uses the VIEW= option to define a view that calculates the birth year based on age. No physical table is created for 'MaVueBasique'; only its descriptor is stored. PROC PRINT and PROC CONTENTS procedures are used to visualize the content and structure of the view.
Copied!
1DATA MonJeuDeDonnees;
2 INPUT ID Nom $ Age;
3 DATALINES;
41 Jean 25
52 Marie 30
63 Pierre 22
7;
8RUN;
9 
10DATA MaVueBasique / view=MaVueBasique;
11 SET MonJeuDeDonnees;
12 AnneeNaissance = 2025 - Age;
13RUN;
14 
15PROC PRINT DATA=MaVueBasique; RUN;
16PROC CONTENTS DATA=MaVueBasique; RUN;
2 Code Block
DATA STEP Data
Explanation :
This example creates a DATA Step view named 'VueIMC' that performs more elaborate calculations and applies formatting. After creating a 'Patients' dataset, the DATA step calculates the Body Mass Index (BMI) and creates a 'Statut' variable based on gender. The BMI variable is formatted to display only one decimal place. This illustrates how views can incorporate transformation and formatting logic without materializing the transformed data.
Copied!
1DATA Patients;
2 INPUT PatientID Genre $ Poids Taille;
3 DATALINES;
41 H 70 175
52 F 60 160
63 H 85 180
74 F 55 165
8;
9RUN;
10 
11DATA VueIMC / view=VueIMC;
12 SET Patients;
13 IMC = Poids / (Taille/100)**2;
14 FORMAT IMC 5.1;
15 IF Genre = 'F' THEN Statut = 'Femme';
16 ELSE Statut = 'Homme';
17 LENGTH Statut $ 5;
18RUN;
19 
20PROC PRINT DATA=VueIMC; RUN;
3 Code Block
DATA STEP, PROC CAS, PROC CONTENTS Data
Explanation :
This advanced example shows how a DATA Step view interacts with a CAS table containing VARCHAR variables. First, a CAS table 'MaTableCAS' is created and loaded with data including a 'Description' column of type VARCHAR. Then, a DATA Step view 'VueCasVARCHAR' is defined from this CAS table. This view calculates the length of the VARCHAR variable. This example is crucial because it demonstrates that, although CAS tables handle VARCHARs, a DATA Step view can read and manipulate them efficiently, even when the DATA Step runs locally with a remote CAS table as input.
Copied!
1LIBNAME mycas cas;
2 
3DATA _null_;
4 file _webout;
5 put '{ "casServer": { "server": "cas-shared-default", "port": 80 } }';
6RUN;
7 
8PROC CAS;
9 SESSION casauto;
10 TABLE.droptable / caslib='casuser' name='MaTableCAS' quiet=true;
11 DATA = {{name='ID', type='double', LENGTH=8},
12 {name='Description', type='varchar', LENGTH=50}};
13 TABLE.addtable / caslib='casuser' name='MaTableCAS' promote=true
14 columns=DATA;
15 DATA.appendtable / caslib='casuser' target='MaTableCAS'
16 casdata={{ID=1, Description='Premier article'},
17 {ID=2, Description='Deuxième article plus long'},
18 {ID=3, Description='Troisième'}};
19 PRINT 'Table CAS MaTableCAS créée avec VARCHAR.';
20QUIT;
21 
22DATA VueCasVARCHAR / view=VueCasVARCHAR;
23 SET mycas.MaTableCAS;
24 LongueurDescription = LENGTH(Description);
25RUN;
26 
27PROC PRINT DATA=VueCasVARCHAR; RUN;
28PROC CONTENTS DATA=VueCasVARCHAR; RUN;
4 Code Block
DATA STEP, PROC CAS Data
Explanation :
This example illustrates an advanced capability of DATA Step views: joining multiple CAS tables and filtering the results. Two CAS tables, 'Employes' and 'Projets', are first created and loaded. Then, a DATA Step view 'VueRHProjets' is defined by merging these two tables via the MERGE statement and using the 'EmpID' variable for the join (BY EmpID). A filter (WHERE Departement = 'RH') is applied to include only employees from the human resources department, and only the relevant columns are kept. This demonstrates the power of views to create complex aggregations or subsets of data without generating new physical tables, thereby optimizing the use of CAS memory resources.
Copied!
1LIBNAME mycas cas;
2 
3PROC CAS;
4 SESSION casauto;
5 TABLE.droptable / caslib='casuser' name='Employes' quiet=true;
6 DATA Employes;
7 INPUT EmpID Nom $ Departement $ Salaire;
8 DATALINES;
9 101 Jean RH 50000
10 102 Marie IT 60000
11 103 Pierre RH 55000
12 104 Sophie IT 62000
13 ;
14 RUN;
15 TABLE.addtable / caslib='casuser' name='Employes' promote=true;
16 
17 TABLE.droptable / caslib='casuser' name='Projets' quiet=true;
18 DATA Projets;
19 INPUT ProjectID EmpID $ NomProjet $;
20 DATALINES;
21 P1 101 ProjetX
22 P2 102 ProjetY
23 P3 101 ProjetZ
24 ;
25 RUN;
26 TABLE.addtable / caslib='casuser' name='Projets' promote=true;
27 PRINT 'Tables CAS Employes et Projets créées.';
28QUIT;
29 
30DATA VueRHProjets / view=VueRHProjets;
31 MERGE Employes (in=e) Projets (in=p);
32 BY EmpID;
33 IF e and p;
34 where Departement = 'RH';
35 keep EmpID Nom NomProjet Salaire;
36RUN;
37 
38PROC PRINT DATA=VueRHProjets; RUN;
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.