## Multiplying arrays without using a loop

Occasional Contributor
Posts: 7

# Multiplying arrays without using a loop

Hi everyone,

A very good day!

Let us say I have a dataset:

Name| Var1| Var2 | Var3

A      |23     |25     |28

B      |45     |33     |32

C      |23     |65     |67

D      |13     |14     |14

E      |65     |66     |69

Where Name is character and Var1-3 are numeric.

Problem:

If the name is equal to A or B, all the numeric values should be multiplied by 100.

Is it possible to do it without looping or arrays. Something of the nature,

If Name IN ("A" "B") then do;

_numeric_ *100;

RUN;

This is just a run of the mill thought and doesn't mean it is right!

Any other solutions welcome.

Regards,

Kavindra

Occasional Contributor
Posts: 7

## Re: Multiplying arrays without using a loop

Actually the variables are named differently and using Var1-3 is not possible!

Let us say,

dollars1-24, units1-24 and packet1-24

Posts: 3,215

## Re: Multiplying arrays without using a loop

What is the reason for asking without looping or arrays?

It are programming elements that should make coding easier.

Looping over records is done automatic. SAS is doing that SQL is doing that.

As it is already happening you do not need to code by yourself.

Arrays are a feature to do a lot of the same without typing it all yourself. SAS(R) 9.4 Language Reference: Concepts

There are many automatic ways working with arays or naming conventions (* :   Variable lists)

---->-- ja karman --<-----
Occasional Contributor
Posts: 7

## Re: Multiplying arrays without using a loop

Basically the numeric variables are named very diversely. So, there is no way to rename them without tediously writing down their names. So, it would be helpful if it could be done for all numeric variables.

Super Contributor
Posts: 339

## Re: Multiplying arrays without using a loop

Hi Kavindra,

You can use _NUMERIC_ to define an array referencing all numeric variables in the data vector and then loop on that array.

data want;

set have;

array temp {*} _NUMERIC_;

if name in ("A", "B") then do;

do i=1 to dim(temp);

temp{i}=temp{i}*100;

end;

end;

drop i;

run;

It's not quite "without arrays" but it read to me as though your issue was that of getting all of your numeric variables in an array when not knowing all the variables names or, well, not wanting to hard type them all because they are not ranges. I have to agree with Jaap in that you shouldn't restrain yourself not to use arrays. They often provide the easiest solution and figuring out the appropriate syntax to solve your loop issue is what the SAS community/documentation is there for.

However, as a possible alternative to obtaining all of your numeric variables names, you can always consider using SASHELP.vcolumn automatic table. Either load the name of the numeric variables in a macro for a proc datasets rename or just to use these macros to build the list of variables for an array statement for example.

Vince

Eh apparently I got beat to it by Rick while writting up sorry for duplicate code.

Posts: 3,215

## Re: Multiplying arrays without using a loop

This looks a nice reference using the _numeric_ list.  http://www2.sas.com/proceedings/sugi30/242-30.pdf

Do not be afraid of array-s. See the allnums usage sample with the dimension automatic supplies.

Some surprise, the 24 number should be a coincidence, possible a common 2 year interval approach.

---->-- ja karman --<-----
SAS Super FREQ
Posts: 4,243

## Re: Multiplying arrays without using a loop

data Have;
input Name \$ Var1 Var2 Var3;
datalines;
A      23     25     28
B      45     33     32
C      23     65     67
D      13     14     14
E      65     66     69
;

data Want(drop i);
set have;
array x{*} _numeric_;   /* all numeric vars */
if Name IN ("A" "B") then do;
do i=1 to dim(x);
x{i} = 100*x{i};
end;
end;
run;

Posts: 3,215

## Re: Multiplying arrays without using a loop

thx Rick Vince,

I was just pulling at Kavindra's mind/brains for the why's and how's the requirements and options with questions and giving some doc's..

---->-- ja karman --<-----
Discussion stats
• 7 replies
• 2100 views
• 0 likes
• 4 in conversation