|

SAS Tip of the Day: The _character_ array and fixing all character variables

Contrary to appearances, this is not an abandoned blog. I’ve been super-busy with 7 Generation Games, where we released two new games and a customized app for a client this month! At the same time, I’m in Santiago, Chile piloting games for our Spanish language brand, Strong Mind Studios, you can read some of my blog in Spanish here.

Santa Lucia
Santa Lucia, next door

I decided to get back to blogging with a SAS tip of the day. Today’s tip is about the _character_ array.

If you didn’t know, now you know: All character variables are in the _character_ array

Often, I want to do something to every character variable in a data set, for example, set all of the values to upper case, so “diabetes”, “Diabetes” and “DIABETES” are not counted as three, different disabilities. Because I hate to expend unnecessary effort, I don’t want to list the names of every character variable and I don’t want to count how many there are because I’ll probably count wrong and then end up with errors.

Here is an example using the _character_ array.

data fixdata ;
set fix1;
array fixchars {*} _character_ ;
** Change all character values to upper case ;
do i = 1 to dim(fixchars);
fixchars{i} = upcase(fixchars{i}) ;
end ;

Just use an ARRAY statement, give your array a name and in the {} instead of the number of elements put a *  which SAS interprets as “the number of variables in the array are however many character variables there happen to be.

You might think you’d have to use the $ to specify that the _character_ array consists of character variables, but that’s kind of overkill and you actually don’t. It will work either way.

In my DO statement, I use the DIM function which will return the dimension of the array. That is, DO I = 1 to DIM(array_name) will do the statements from the first variable to however many happen to be in the array.

As you might guess, the UPCASE function returns the value in all upper case.


Have a kid? Like kids? Feel like a kid yourself? Check out our new game, Making Camp Premium, because maturity is over-rated.

 

 

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *