The examples use the SASHELP.CARS dataset, which is loaded into CAS memory using PROC CASUTIL to ensure the autonomy of the examples.
1 Code Block
DATA STEP Data
Explanation : This example illustrates the simplest use of a DATA step in CAS. It loads the SASHELP.CARS dataset into CAS memory, then creates a new CAS table named 'cars_with_eff'. A new variable 'Efficiency_Category' is added based on city fuel consumption (MPG_City). The use of 'mycas' librefs for input and output ensures the DATA step executes in CAS. A PROC PRINT is used to display the first observations of the new table, and the temporary table is dropped at the end.
Copied!
libname mycas cas; /* Crée un libref CAS */
proc casutil outcaslib="casuser" replace;
load data=sashelp.cars casout="cars_cas";
run;
data mycas.cars_with_eff;
set mycas.cars_cas;
if MPG_City > 25 then Efficiency_Category = 'Haute';
else Efficiency_Category = 'Standard';
run;
proc print data=mycas.cars_with_eff(obs=5);
title 'Exemple Basique: Voitures avec catégorie d''efficacité';
run;
proc casutil outcaslib="casuser" drop table="cars_with_eff";
run;
1
LIBNAME mycas cas; /* Crée un libref CAS */
2
3
PROC CASUTIL outcaslib="casuser" replace;
4
load DATA=sashelp.cars casout="cars_cas";
5
RUN;
6
7
DATA mycas.cars_with_eff;
8
SET mycas.cars_cas;
9
IF MPG_City > 25THEN Efficiency_Category = 'Haute';
10
ELSE Efficiency_Category = 'Standard';
11
RUN;
12
13
PROC PRINTDATA=mycas.cars_with_eff(obs=5);
14
title 'Exemple Basique: Voitures avec catégorie d''efficacité';
15
RUN;
16
17
PROC CASUTIL outcaslib="casuser" drop TABLE="cars_with_eff";
18
RUN;
19
2 Code Block
DATA STEP Data
Explanation : This intermediate example shows how to filter data and select specific variables when executing a DATA step in CAS. It selects only 'Sports' type cars with highway fuel consumption (MPG_Highway) greater than 20, using a WHERE clause. The 'KEEP=' option is used to retain only the 'Make', 'Model', 'Type', 'Origin', and 'MSRP' variables in the output CAS table named 'sporty_cars'. This optimizes the final table by storing only relevant information.
Copied!
libname mycas cas;
proc casutil outcaslib="casuser" replace;
load data=sashelp.cars casout="cars_cas";
run;
data mycas.sporty_cars(keep=Make Model Type Origin MSRP);
set mycas.cars_cas;
where Type = 'Sports' and MPG_Highway > 20;
run;
proc print data=mycas.sporty_cars(obs=5);
title 'Exemple Intermédiaire: Voitures de sport économes en carburant';
run;
proc casutil outcaslib="casuser" drop table="sporty_cars";
run;
1
LIBNAME mycas cas;
2
3
PROC CASUTIL outcaslib="casuser" replace;
4
load DATA=sashelp.cars casout="cars_cas";
5
RUN;
6
7
DATA mycas.sporty_cars(keep=Make Model Type Origin MSRP);
8
SET mycas.cars_cas;
9
where Type = 'Sports' and MPG_Highway > 20;
10
RUN;
11
12
PROC PRINTDATA=mycas.sporty_cars(obs=5);
13
title 'Exemple Intermédiaire: Voitures de sport économes en carburant';
14
RUN;
15
16
PROC CASUTIL outcaslib="casuser" drop TABLE="sporty_cars";
17
RUN;
18
3 Code Block
DATA STEP Data
Explanation : This advanced example demonstrates the use of automatic variables specific to distributed execution in CAS, such as '_N_', '_THREADID_', and '_HOSTNAME_'. It creates a new 'Row_ID' variable based on '_N_' and a 'Thread_Info' variable to capture the details of the thread and CAS worker node processing the observation. Additionally, it uses CALL SYMPUTX to create macro variables for specific observations, which can be useful for auditing or tracking distributed processing. A macro is included to verify the creation of these macro variables.
Copied!
libname mycas cas;
proc casutil outcaslib="casuser" replace;
load data=sashelp.cars casout="cars_cas";
run;
data mycas.cars_processed;
set mycas.cars_cas;
Row_ID = _N_;
Thread_Info = cats('Thread: ', _THREADID_, ' on node: ', _HOSTNAME_);
if (_N_ between 1 and 10) or (mod(_N_, 100) = 0) then call symputx(cats('Obs',_N_), Make);
run;
%macro check_macros;
%global Obs1 Obs10 Obs100;
%if %symexist(Obs1) %then %put Observation 1 (Make): &Obs1.;
%if %symexist(Obs10) %then %put Observation 10 (Make): &Obs10.;
%if %symexist(Obs100) %then %put Observation 100 (Make): &Obs100.;
%mend;
%check_macros;
proc print data=mycas.cars_processed(obs=10);
title 'Exemple Avancé: Traitement et informations de thread';
run;
proc casutil outcaslib="casuser" drop table="cars_processed";
run;
1
LIBNAME mycas cas;
2
3
PROC CASUTIL outcaslib="casuser" replace;
4
load DATA=sashelp.cars casout="cars_cas";
5
RUN;
6
7
DATA mycas.cars_processed;
8
SET mycas.cars_cas;
9
Row_ID = _N_;
10
Thread_Info = cats('Thread: ', _THREADID_, ' on node: ', _HOSTNAME_);
11
IF (_N_ between 1 and 10) or (mod(_N_, 100) = 0) THEN call symputx(cats('Obs',_N_), Make);
title 'Exemple Avancé: Traitement et informations de thread';
24
RUN;
25
26
PROC CASUTIL outcaslib="casuser" drop TABLE="cars_processed";
27
RUN;
28
4 Code Block
DATA STEP Data
Explanation : This example focuses on data management and conditional writing in a DATA step in CAS, a common practice in the Viya environment for data quality assurance. It begins by creating a local data table with some simulated missing values, then loads it into CAS. The DATA step reads this table and writes observations to two separate output CAS tables: 'cars_clean' for complete data and 'cars_problematic' for observations containing missing values in the fuel consumption columns. A calculated variable 'Avg_MPG' is created for clean data. The use of the '_ERROR_' keyword also allows error conditions to be reported in the log, which is useful for debugging in a distributed environment. This is a typical use case for preparing data before further analysis in CAS.
Copied!
libname mycas cas;
/* Création d'une table d'entrée avec quelques données manquantes */
data cars_data_local;
input Make $ Model $ Type $ MPG_City MPG_Highway;
datalines;
Toyota Camry Sedan 28 39
Honda Civic Sedan 30 40
Ford F-150 Truck . 22
Chevrolet Silverado Truck 16 .
BMW X5 SUV 20 27
;;
run;
proc casutil outcaslib="casuser" replace;
load data=cars_data_local casout="cars_with_missing";
run;
data mycas.cars_clean mycas.cars_problematic;
set mycas.cars_with_missing;
/* Vérifier les valeurs manquantes pour la consommation de carburant */
if missing(MPG_City) or missing(MPG_Highway) then do;
output mycas.cars_problematic; /* Écrire les lignes avec problèmes dans une table séparée */
_ERROR_ = 1; /* Marquer l'observation comme ayant une erreur pour le log */
end;
else do;
Avg_MPG = (MPG_City + MPG_Highway) / 2;
output mycas.cars_clean;
end;
run;
proc print data=mycas.cars_clean(obs=5);
title 'Exemple Viya: Voitures sans données manquantes';
run;
proc print data=mycas.cars_problematic(obs=5);
title 'Exemple Viya: Voitures avec données manquantes';
run;
proc casutil outcaslib="casuser" drop table="cars_with_missing" casout="cars_clean" casout="cars_problematic";
run;
1
LIBNAME mycas cas;
2
3
/* Création d'une table d'entrée avec quelques données manquantes */
/* Vérifier les valeurs manquantes pour la consommation de carburant */
22
IF missing(MPG_City) or missing(MPG_Highway) THENDO;
23
OUTPUT mycas.cars_problematic; /* Écrire les lignes avec problèmes dans une table séparée */
24
_ERROR_ = 1; /* Marquer l'observation comme ayant une erreur pour le log */
25
END;
26
ELSEDO;
27
Avg_MPG = (MPG_City + MPG_Highway) / 2;
28
OUTPUT mycas.cars_clean;
29
END;
30
RUN;
31
32
PROC PRINTDATA=mycas.cars_clean(obs=5);
33
title 'Exemple Viya: Voitures sans données manquantes';
34
RUN;
35
36
PROC PRINTDATA=mycas.cars_problematic(obs=5);
37
title 'Exemple Viya: Voitures avec données manquantes';
38
RUN;
39
40
PROC CASUTIL outcaslib="casuser" drop TABLE="cars_with_missing" casout="cars_clean" casout="cars_problematic";
41
RUN;
42
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.