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!
data MonJeuDeDonnees;
input ID Nom $ Age;
datalines;
1 Jean 25
2 Marie 30
3 Pierre 22
;
run;
data MaVueBasique / view=MaVueBasique;
set MonJeuDeDonnees;
AnneeNaissance = 2025 - Age;
run;
proc print data=MaVueBasique; run;
proc contents data=MaVueBasique; run;
1
DATA MonJeuDeDonnees;
2
INPUT ID Nom $ Age;
3
DATALINES;
4
1 Jean 25
5
2 Marie 30
6
3 Pierre 22
7
;
8
RUN;
9
10
DATA MaVueBasique / view=MaVueBasique;
11
SET MonJeuDeDonnees;
12
AnneeNaissance = 2025 - Age;
13
RUN;
14
15
PROC PRINTDATA=MaVueBasique; RUN;
16
PROC CONTENTSDATA=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!
data Patients;
input PatientID Genre $ Poids Taille;
datalines;
1 H 70 175
2 F 60 160
3 H 85 180
4 F 55 165
;
run;
data VueIMC / view=VueIMC;
set Patients;
IMC = Poids / (Taille/100)**2;
format IMC 5.1;
if Genre = 'F' then Statut = 'Femme';
else Statut = 'Homme';
length Statut $ 5;
run;
proc print data=VueIMC; run;
1
DATA Patients;
2
INPUT PatientID Genre $ Poids Taille;
3
DATALINES;
4
1 H 70175
5
2 F 60160
6
3 H 85180
7
4 F 55165
8
;
9
RUN;
10
11
DATA 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;
18
RUN;
19
20
PROC PRINTDATA=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.
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!
libname mycas cas;
proc cas;
session casauto;
table.droptable / caslib='casuser' name='Employes' quiet=true;
data Employes;
input EmpID Nom $ Departement $ Salaire;
datalines;
101 Jean RH 50000
102 Marie IT 60000
103 Pierre RH 55000
104 Sophie IT 62000
;
run;
table.addtable / caslib='casuser' name='Employes' promote=true;
table.droptable / caslib='casuser' name='Projets' quiet=true;
data Projets;
input ProjectID EmpID $ NomProjet $;
datalines;
P1 101 ProjetX
P2 102 ProjetY
P3 101 ProjetZ
;
run;
table.addtable / caslib='casuser' name='Projets' promote=true;
print 'Tables CAS Employes et Projets créées.';
quit;
data VueRHProjets / view=VueRHProjets;
merge Employes (in=e) Projets (in=p);
by EmpID;
if e and p;
where Departement = 'RH';
keep EmpID Nom NomProjet Salaire;
run;
proc 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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.