BookmarkSubscribeRSS Feed
statistician13
Quartz | Level 8

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.

2 REPLIES 2
ballardw
Super User

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.

Astounding
PROC Star

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 2352 views
  • 0 likes
  • 3 in conversation