DATA Step, Macro, Functions and more

Operate on Every Other Column or Use Array with Mixed Data Types?

Reply
Contributor
Posts: 57

Operate on Every Other Column or Use Array with Mixed Data Types?

[ Edited ]

Does anyone know if it's possible to either operate on every other column of a dataset or (preferably) load mixed data types into an array (even possibly a multidimensional one, if needed) in SAS?  I have a huge dataset where the columns alternate between numeric and character and I need to operate identically on all the columns, regardless of type. Of course, I can do this easily by outputting the numeric columns to one dataset and the character to another, but given the size of these datasets, I am not thrilled with this solution since I would have to iterate over the dataset at least three times, and I'd rather just make one pass through the data.  I suppose I could also do something like this in IML, but I'd prefer to stick with base SAS.  I'm consdiering switching to R and this might be the nail in the coffin if I can't find a workable solution in SAS.

 

I tried to do something likes this:

data work.temp;
set work.temp;
array myarray {*} $ Char_Q2 Numeric_Q3;
do i=1 to 4 by 2;
/*    operations would go here here */
end;
run;

but, this throws the error:

ERROR: All variables in array list must be the same type, i.e., all numeric or character.

Any ideas?

 

Thanks.

Super User
Posts: 10,500

Re: Operate on Every Other Column or Use Array with Mixed Data Types?

Array cannot contain mixed type, either all numeric or all character.

Since the variables are of different types please describe the operation "I need to operate identically on all the columns". Since very few SAS functions or operations work on both data types a example of what you are actually attempting will help.

 

There are TWO special variable lists that could create separate character and numeric arrays, namely _character_ and _numeric_.

 

Array c (*) _character_;

Array n (*) _numeric_;

would declare the arrays.

Then you could loop over each array separately:

Do i = 1 to dim(c);

  <what ever op>

end;

do i=1 to dim(n);

  <what ever op>

end;

 

Which would at least be possibly feasible as the functions or operations could be specified to match the the variable type.

Super User
Posts: 5,084

Re: Operate on Every Other Column or Use Array with Mixed Data Types?

While ballardw has the right idea, you might find it easier to think about it in this way:

 

do i=1 to dim(c);

   n(i) = ....;

   c(i) = .....;

end;

 

You could process each pair (one numeric, one character) in the same iteration of the DO loop.

Ask a Question
Discussion stats
  • 2 replies
  • 128 views
  • 0 likes
  • 3 in conversation