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;
The codes and examples provided on WeAreCAS.eu are for educational purposes. It is imperative not to blindly copy-paste them into your production environments. The best approach is to understand the logic before applying it. We strongly recommend testing these scripts in a test environment (Sandbox/Dev). WeAreCAS accepts no responsibility for any impact or data loss on your systems.
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.