The examples use data generated from the SASHELP (iris) library loaded into a CAS session.
1 Code Block
PROC CARDINALITY / DATA STEP Data
Explanation : This example illustrates the simplest use of PROC CARDINALITY. It loads the 'sashelp.iris' dataset into a CAS session under the name 'mycas.iris'. Then, it executes the CARDINALITY procedure for the 'Species' variable, generating a cardinality table ('outcard') and a level details table ('outdetails'). The results are displayed using PROC PRINT.
Copied!
CAS;
libname mycas cas;
/* Charger le jeu de données Iris de SASHELP vers la session CAS */
data mycas.iris;
set sashelp.iris;
run;
/* Exemple 1 : Utilisation Basique pour la variable Species */
proc cardinality data=mycas.iris outcard=mycas.card_species outdetails=mycas.details_species;
var Species;
run;
proc print data=mycas.card_species;
title 'Cardinalité basique pour la variable Species';
run;
proc print data=mycas.details_species;
title 'Détails des niveaux pour la variable Species';
run;
1
CAS;
2
LIBNAME mycas cas;
3
4
/* Charger le jeu de données Iris de SASHELP vers la session CAS */
5
DATA mycas.iris;
6
SET sashelp.iris;
7
RUN;
8
9
/* Exemple 1 : Utilisation Basique pour la variable Species */
title 'Cardinalité basique pour la variable Species';
16
RUN;
17
18
PROC PRINTDATA=mycas.details_species;
19
title 'Détails des niveaux pour la variable Species';
20
RUN;
2 Code Block
PROC CARDINALITY / PROC SGPLOT
Explanation : This example demonstrates the use of 'MAXLEVELS' option to limit the number of detailed levels displayed and 'DESCENDING' to sort these levels by frequency in descending order. It also generates a histogram to visualize the distribution of the 10 most frequent 'SepalLength' levels.
Copied!
/* Exemple 2 : Utilisation avec des options courantes (MAXLEVELS et DESCENDING) */
/* Analyse de SepalLength avec un nombre limité de niveaux affichés et tri décroissant */
proc cardinality data=mycas.iris outcard=mycas.card_sepal_full outdetails=mycas.details_sepal_full maxlevels=10 descending;
var SepalLength;
run;
proc print data=mycas.card_sepal_full;
title 'Cardinalité complète pour SepalLength (10 niveaux max, ordre décroissant)';
run;
proc sgplot data=mycas.details_sepal_full;
vbar _cfmt_ / freq=_freq_ categoryorder=data;
title 'Histogramme des 10 niveaux les plus fréquents de SepalLength';
run;
1
/* Exemple 2 : Utilisation avec des options courantes (MAXLEVELS et DESCENDING) */
2
/* Analyse de SepalLength avec un nombre limité de niveaux affichés et tri décroissant */
title 'Cardinalité complète pour SepalLength (10 niveaux max, ordre décroissant)';
9
RUN;
10
11
PROC SGPLOTDATA=mycas.details_sepal_full;
12
vbar _cfmt_ / freq=_freq_ categoryorder=DATA;
13
title 'Histogramme des 10 niveaux les plus fréquents de SepalLength';
14
RUN;
3 Code Block
PROC CARDINALITY / DATA STEP / PROC SGPLOT
Explanation : This example re-examines the scenario from the document, filtering the data to include only observations where 'SepalLength' is greater than 4.7. The CARDINALITY procedure is executed with 'MAXLEVELS=5', meaning that only the 5 most frequent levels will be detailed, with others grouped. A DATA step is used to prepare the output data for PROC SGPLOT, adding a '>' before grouped values to distinguish them. Finally, a histogram is generated, and maximum frequency information is displayed.
Copied!
/* Exemple 3 : Cas Avancé - Analyse des niveaux de SepalLength > 4.7 et visualisation */
proc cardinality data=mycas.iris (where=(SepalLength > 4.7))
outcard=mycas.card_sepal_gt47
outdetails=mycas.details_sepal_gt47
maxlevels=5; /* Limite à 5 niveaux pour cet exemple */
var SepalLength;
run;
/* Préparation des données pour SGPLOT : ajoute '>' aux niveaux 'Autres' pour le graphique */
data mycas.sp_gt47;
set mycas.details_sepal_gt47;
label _cfmt_='Valeur formatée de SepalLength > 4.7';
if _index_ = . then do; /* Si _index_ est manquant, c'est la catégorie 'Autres' */
_cfmt_=cats(">",_cfmt_);
end;
_cfmt_=left(_cfmt_); /* Nettoie les espaces */
run;
proc sgplot data=mycas.sp_gt47;
vbar _cfmt_ / freq=_freq_;
title 'Histogramme des niveaux de SepalLength > 4.7';
run;
/* Affichage des informations de fréquence maximale pour ce cas */
proc print data=mycas.card_sepal_gt47;
title 'Informations de fréquence maximale pour SepalLength > 4.7';
var _varname_ _mf:;
run;
1
/* Exemple 3 : Cas Avancé - Analyse des niveaux de SepalLength > 4.7 et visualisation */
maxlevels=5; /* Limite à 5 niveaux pour cet exemple */
6
var SepalLength;
7
RUN;
8
9
/* Préparation des données pour SGPLOT : ajoute '>' aux niveaux 'Autres' pour le graphique */
10
DATA mycas.sp_gt47;
11
SET mycas.details_sepal_gt47;
12
label _cfmt_='Valeur formatée de SepalLength > 4.7';
13
IF _index_ = . THENDO; /* Si _index_ est manquant, c'est la catégorie 'Autres' */
14
_cfmt_=cats(">",_cfmt_);
15
END;
16
_cfmt_=left(_cfmt_); /* Nettoie les espaces */
17
RUN;
18
19
PROC SGPLOTDATA=mycas.sp_gt47;
20
vbar _cfmt_ / freq=_freq_;
21
title 'Histogramme des niveaux de SepalLength > 4.7';
22
RUN;
23
24
/* Affichage des informations de fréquence maximale pour ce cas */
25
PROC PRINTDATA=mycas.card_sepal_gt47;
26
title 'Informations de fréquence maximale pour SepalLength > 4.7';
27
var _varname_ _mf:;
28
RUN;
4 Code Block
PROC CARDINALITY
Explanation : This example highlights the CARDINALITY procedure's ability to execute on CAS data. It analyzes the cardinality of multiple variables simultaneously. The 'OUTPUT' option with 'MINRATIO' is used to identify variables whose cardinality (number of distinct levels) exceeds a certain percentage (here, 80%) of the total number of observations, which can be useful for detecting identifiers or low-variance variables.
Copied!
/* Exemple 4 : Intégration Viya/CAS - Analyse de plusieurs variables et détection de cardinalité */
/* Cet exemple illustre l'exécution en mémoire distribuée (CAS) */
proc cardinality data=mycas.iris outcard=mycas.multi_var_card maxlevels=10;
var SepalLength SepalWidth PetalLength PetalWidth Species;
output out=mycas.high_card_vars minratio=0.8; /* Garde les variables dont la cardinalité dépasse 80% des observations */
run;
proc print data=mycas.multi_var_card;
title 'Cardinalité pour plusieurs variables';
run;
proc print data=mycas.high_card_vars;
title 'Variables avec une cardinalité supérieure à 80% des observations';
run;
1
/* Exemple 4 : Intégration Viya/CAS - Analyse de plusieurs variables et détection de cardinalité */
2
/* Cet exemple illustre l'exécution en mémoire distribuée (CAS) */
var SepalLength SepalWidth PetalLength PetalWidth Species;
5
OUTPUT out=mycas.high_card_vars minratio=0.8; /* Garde les variables dont la cardinalité dépasse 80% des observations */
6
RUN;
7
8
PROC PRINTDATA=mycas.multi_var_card;
9
title 'Cardinalité pour plusieurs variables';
10
RUN;
11
12
PROC PRINTDATA=mycas.high_card_vars;
13
title 'Variables avec une cardinalité supérieure à 80% des observations';
14
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.