BookmarkSubscribeRSS Feed
🔒 This topic is locked. We are no longer accepting replies to this topic. Need further help? Please sign in and ask a new question.
Tom
Super User Tom
Super User

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;

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Visit a random SAS tip This SAS Tips board is not open for replies or comments, but we welcome your feedback and questions. Have a question or comment about this tip? Start a new topic in one of our discussion boards, and reference this tip topic.
Discussion stats
  • 0 replies
  • 940 views
  • 5 likes
  • 1 in conversation