How to find an ARRAY name that does not conflict with existing variable names (Daily tip for 2025-Feb-19)
Have you ever tried to generate a generic data step that can be used against any input dataset which uses an ARRAY statement to allow applying the same code to multiple variable and then run into issues when the source dataset already has a variable with the name the code wants to use for the ARRAY and/or the variable used to index into the array?
SAS will let you use special variable list keywords _CHARACTER_ or _NUMERIC_ as array names, but not as variable names.
158 data test3; 159 set test; 160 array _character_ [*] xx; 161 _character_[1]=xx; 162 _numeric_=xx; ERROR: Cannot use _numeric_ as a variable name. 163 run;
So if you use one of those as the array name you reduce the risk of conflict to almost zero. (Try RENAME xx=_numeric_ as a statement or dataset option).
You can also repurpose _N_ as the index into the array and avoid another potential name conflict. If you really need the iteration value that SAS will assign to _N_ you just need to take the value before modifying _N_ for your other purposes. Or make you own iteration counter using a sum statement.
So a data step that applies the same operations to every character variable might look like:
data want;
set have;
array _character_ [*] _character_;
do _n_=1 to dim( _character_);
_character_[_n_]=upcase(_character_[_n_]);
end;
run;
Or if you want to use implicit array and DO OVER like this:
data want;
set have;
array _character_ (_n_) _character_;
do over _character_;
_character_=upcase(_character_);
end;
run;
How Do I Assess and Mitigate Models Bias With SAS Fair AI Tools?
Join this webinar to learn how to assess and mitigate model bias using SAS Viya. Register for this free Ask the Expert webinar to learn more.
Premiering February 25, 11 am ET
Register Now