An instructive exchange on the user forum perfectly illustrates this scenario with the use of Arrays and DO blocks.
Note : The Guilty Code
A user attempts to copy values from one array (ais) to another (trhead) under a condition, inside a loop.
Here is the submitted code:
array region{22} region_1 - region_22;
array ais{22} ais_1 - ais_22;
array trhead{22} trhead1-trhead22;
do i = 1 to dim(ais) ;
/* La ligne problématique */
if region(i)='HEAD' then do trhead(i)=ais(i); end;
end;
ERROR: The variable type of trhead is invalid in this context.ERROR: Illegal reference to the array trhead.ERROR 73-322: Expecting an =.
At first glance, one might think that the trhead array is improperly declared or that there is a type mismatch (numeric vs. character).
Note : The Diagnosis: The THEN DO Trap
As quickly identified by the forum experts, the problem has nothing to do with the arrays. It's a classic punctuation error.
The DO statement is a standalone instruction. It must end with a semicolon.
The correction: You must add a semicolon right after the DO.
if region(i)='HEAD' then do; trhead(i)=ais(i); end;
1
IF region(i)='HEAD'THENDO;
2
trhead(i)=ais(i);
3
END;
4
Note : The Golden Rule (Rule #1)
A user named David recalls in the discussion a fundamental rule taught to beginners but often forgotten by experts in the heat of the moment:
Formatting Tip
To avoid this specific error, it's recommended not to write the THEN DO and the following statement on the same line. Adopt the following indentation to make the error visually obvious:
if region(i)='HEAD' then do; /* Le point-virgule est bien visible ici */
trhead(i) = ais(i);
end;
Les codes et exemples fournis sur WeAreCAS.eu sont à but pédagogique. Il est impératif de ne pas les copier-coller aveuglément sur vos environnements de production. La meilleure approche consiste à comprendre la logique avant de l'appliquer. Nous vous recommandons vivement de tester ces scripts dans un environnement de test (Sandbox/Dev). WeAreCAS décline toute responsabilité quant aux éventuels impacts ou pertes de données sur vos systèmes.
SAS et tous les autres noms de produits ou de services de SAS Institute Inc. sont des marques déposées ou des marques de commerce de SAS Institute Inc. aux États-Unis et dans d'autres pays. ® indique un enregistrement aux États-Unis. WeAreCAS est un site communautaire indépendant et n'est pas affilié à SAS Institute Inc.
Ce site utilise des cookies techniques et analytiques pour améliorer votre expérience.
En savoir plus.