Examples use SASHELP data (sashelp.cars) or internally created tables (datalines) to ensure their autonomy and reproducibility.
1 Code Block
DATA STEP Data
Explanation : This example illustrates the use of the _THREADID_ automatic variable to see which thread processes each observation in a multithreaded DATA Step on CAS. It also uses FIRST.make to identify the start of each 'make' group.
Copied!
data mycas.cars;
set sashelp.cars;
run;
data mycas.cars_processed;
set mycas.cars;
by make type;
if first.make then first_make_flag="BY Group";
else first_make_flag="";
threadid = _threadid_;
keep make type first_make_flag threadid;
run;
proc print data=mycas.cars_processed;
title 'Cars By Make By Type - Multithreaded Processing';
run;
1
DATA mycas.cars;
2
SET sashelp.cars;
3
RUN;
4
5
DATA mycas.cars_processed;
6
SET mycas.cars;
7
BY make type;
8
IF first.make THEN first_make_flag="BY Group";
9
ELSE first_make_flag="";
10
threadid = _threadid_;
11
keep make type first_make_flag threadid;
12
RUN;
13
PROC PRINTDATA=mycas.cars_processed;
14
title 'Cars By Make By Type - Multithreaded Processing';
15
RUN;
16
2 Code Block
DATA STEP Data
Explanation : This case compares default (multithreaded) execution and forced single-threaded execution (via SINGLE=YES) in CAS, using _THREADID_ to show the difference in assigned thread IDs. Only the first 10 observations are displayed for comparison.
Copied!
data mycas.cars;
set sashelp.cars;
run;
/* Exécution multithread par défaut */
data mycas.cars_multi;
set mycas.cars;
threadid = _threadid_;
keep make model threadid;
run;
/* Exécution monothread forcée */
data mycas.cars_single(single=yes);
set mycas.cars;
threadid = _threadid_;
keep make model threadid;
run;
proc print data=mycas.cars_multi(obs=10);
title 'Traitement Multithread (extrait)';
run;
proc print data=mycas.cars_single(obs=10);
title 'Traitement Monothread (extrait)';
run;
1
DATA mycas.cars;
2
SET sashelp.cars;
3
RUN;
4
5
/* Exécution multithread par défaut */
6
DATA mycas.cars_multi;
7
SET mycas.cars;
8
threadid = _threadid_;
9
keep make model threadid;
10
RUN;
11
12
/* Exécution monothread forcée */
13
DATA mycas.cars_single(single=yes);
14
SET mycas.cars;
15
threadid = _threadid_;
16
keep make model threadid;
17
RUN;
18
19
PROC PRINTDATA=mycas.cars_multi(obs=10);
20
title 'Traitement Multithread (extrait)';
21
RUN;
22
PROC PRINTDATA=mycas.cars_single(obs=10);
23
title 'Traitement Monothread (extrait)';
24
RUN;
25
3 Code Block
DATA STEP Data
Explanation : This advanced example combines the automatic variables _THREADID_, _N_ (DATA Step iteration number) and _ERROR_ (error indicator) within a DATA Step executed in CAS. It shows how these variables behave in a multithreaded environment and how _ERROR_ can be manipulated to signal specific conditions, in addition to identifying the start of BY groups.
Copied!
data _null_;
input id $ value;
datalines;
1 A
1 B
2 C
3 D
3 E
4 F
;
run;
data mycas.test_data;
set _null_;
run;
data mycas.processed_data;
set mycas.test_data;
by id;
if first.id then group_start = 1;
else group_start = 0;
record_num = _n_;
thread_id = _threadid_;
if value = 'B' then _error_ = 1;
error_flag = _error_;
keep id value group_start record_num thread_id error_flag;
run;
proc print data=mycas.processed_data;
title 'Analyse des Variables Automatiques _N_ et _ERROR_ dans CAS';
run;
1
DATA _null_;
2
INPUT id $ value;
3
DATALINES;
4
1 A
5
1 B
6
2 C
7
3 D
8
3 E
9
4 F
10
;
11
RUN;
12
13
DATA mycas.test_data;
14
SET _null_;
15
RUN;
16
17
DATA mycas.processed_data;
18
SET mycas.test_data;
19
BY id;
20
IF first.id THEN group_start = 1;
21
ELSE group_start = 0;
22
record_num = _n_;
23
thread_id = _threadid_;
24
IF value = 'B'THEN _error_ = 1;
25
error_flag = _error_;
26
keep id value group_start record_num thread_id error_flag;
27
RUN;
28
29
PROC PRINTDATA=mycas.processed_data;
30
title 'Analyse des Variables Automatiques _N_ et _ERROR_ dans CAS';
31
RUN;
32
4 Code Block
DATA STEP Data
Explanation : This example is designed for performance and debugging in a CAS environment. It creates a dataset of 100 observations divided into groups. The DATA Step uses _THREADID_ to display in the log (via the PUT statement) which thread starts processing each new group. This can be useful for understanding workload distribution and effective DATA Step parallelism on the CAS cluster, particularly for identifying potential imbalances or verifying that processing is well-distributed. The output is just an overview, with the main information being in the log.
Copied!
/* Création d'une table CAS temporaire */
data mycas.sample_data;
do i = 1 to 100;
group = ceil(i/10);
value = ranuni(0) * 100;
output;
end;
run;
/* DATA Step avec logging pour suivre les threads */
data mycas.debug_output;
set mycas.sample_data;
by group;
thread_id = _threadid_;
if first.group then do;
put 'DEBUG: Nouveau groupe ' group ' sur thread ' thread_id;
end;
output;
run;
/* Affichage des 10 premières lignes du résultat pour validation */
proc print data=mycas.debug_output(obs=10);
title 'Sortie du DATA Step avec IDs de Thread';
run;
/* Pour une analyse complète du logging, consulter le log SAS Studio */
1
/* Création d'une table CAS temporaire */
2
DATA mycas.sample_data;
3
DO i = 1 to 100;
4
group = ceil(i/10);
5
value = ranuni(0) * 100;
6
OUTPUT;
7
END;
8
RUN;
9
10
/* DATA Step avec logging pour suivre les threads */
11
DATA mycas.debug_output;
12
SET mycas.sample_data;
13
BY group;
14
thread_id = _threadid_;
15
IF first.group THENDO;
16
put 'DEBUG: Nouveau groupe ' group ' sur thread ' thread_id;
17
END;
18
OUTPUT;
19
RUN;
20
21
/* Affichage des 10 premières lignes du résultat pour validation */
22
PROC PRINTDATA=mycas.debug_output(obs=10);
23
title 'Sortie du DATA Step avec IDs de Thread';
24
RUN;
25
26
/* Pour une analyse complète du logging, consulter le log SAS Studio */
27
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.