Solved
Contributor
Posts: 45

# Arrays

Hi All, i want to use array and loops only

i want to multiply the 1st emp salary by .10 2nd by .25 3rd by .09 and 4th by .11

data have;

input emp_name \$ sal ;

cards;

Kevin 3000

david 2300

Kat 2100

ricky 10000 ;

run;

Accepted Solutions
Solution
‎07-10-2017 03:40 PM
Super User
Posts: 23,700

## Re: Arrays

shivamarrora0 wrote:

I am sorry if am unable to portray what i want

If you can't define your problem you can't solve it.

SAS loops through rows by default and there's a row counter variable _n_ which will work in this situation. The MOD() function will create counters from 1 to 4 or you could do a manual count. Use that variable as an index the array of the ratios. If you have more ratios you can load the temporary array from a dataset rather than type it out. I'll leave that for another day. I've separated the steps to make it more clear below.

``````data have (drop=mult:);
input emp_name \$ sal;
cards;
Kevin 3000
david 2300
Kat 2100
ricky 10000
Fred 100
Betty 2000
Shiva 0
Random .
Check 23
;
run;

data want;
set have;
array mult(4) _temporary_ (0.1, 0.25, 0.09, 0.11); *array with rates;
index=mod(_n_-1, 4)+1; *calculate which value from the array you need;
value=mult(index)*sal; *multiple the value in SAL by the respective value in the array;
run;``````

All Replies
PROC Star
Posts: 8,164

## Re: Arrays

```data have (drop=mult:);
input emp_name \$ sal ;
array mult(4) (.1,.25,.09,.11);
want=sal*mult(_n_);
cards;
Kevin 3000
david 2300
Kat 2100
ricky 10000
;
run;```

Art, CEO, AnalystFinder.com

Contributor
Posts: 45

## Re: Arrays

but if i have some more observations and want to multiply them by same , then it will give the error " Array subscript out of range"

ex:

data have (drop=mult;
input emp_name \$ sal ;
array mult(4) (.1,.25,.09,.11);
want=sal*mult(_n_);
cards;
Kevin 3000
david 2300
Kat 2100
ricky 10000
Donald 2100
Ricky 1100
;
run;
PROC Star
Posts: 8,164

## Re: Arrays

[ Edited ]

Not sure how you want to apply the multipliers. Something like the following?:

```data have (drop=mult:);
input emp_name \$ sal ;
array mult(0:3) (.11,.1,.25,.09);
want=sal*mult(mod(_n_,4));
cards;
Kevin 3000
david 2300
Kat 2100
ricky 10000
Donald 2100
Ricky 1100
;
run;
```

Art, CEO, AnalystFinder.com

Contributor
Posts: 45

## Re: Arrays

I want to multiply those 4 values to every 1-4 observations repeatedly .

like Donald will multiply by 1st value of array
then Ricky by 2nd value of array ..

This operation repeat for every 1-4 observation.

I am sorry if am unable to portray what i want
PROC Star
Posts: 8,164

## Re: Arrays

That is what the code I posted accomplished. Again it was:

```data have (drop=mult:);
input emp_name \$ sal ;
array mult(0:3) (.11,.1,.25,.09);
want=sal*mult(mod(_n_,4));
cards;
Kevin 3000
david 2300
Kat 2100
ricky 10000
Donald 2100
Ricky 1100
;
run;```

Art, CEO, AnalystFinder.com

Super User
Posts: 23,700

## Re: Arrays

@art297 Mod() starts at 1, not 0 though, and you've indexed the array to 0-3 so it's going to be off by 1? Or am I missing something?

PROC Star
Posts: 8,164

## Re: Arrays

[ Edited ]

@Reeza: You were missing something. Mod4 of 1=1, mod4 of 2=2, mod4 of 3=3 and mod4 of 4 =0. As such, I simply used an array ranging from 0 to 3, putting the fourth value first, followed by the other three values.

Your code ends up accomplishing the same thing, but requires 3 additional calculations (_n_-1, +1 and index) which weren't needed.

Art, CEO, AnalystFinder.com

Super User
Posts: 23,700

## Re: Arrays

art297 wrote:

@Reeza: You were missing something. Mod4 of 1=1, mod4 of 2=2, mod4 of 3=3 and mod4 of 4 =0. As such, I simply used an array ranging from 0 to 4, putting the fourth value first, followed by the other three values.

Your code ends up accomplishing the same thing, but requires 3 additional calculations (_n_-1, +1 and index) which weren't needed.

Art, CEO, AnalystFinder.com

Neat, thanks for the explanation!

Contributor
Posts: 45

## Re: Arrays

i want this as output

Solution
‎07-10-2017 03:40 PM
Super User
Posts: 23,700

## Re: Arrays

shivamarrora0 wrote:

I am sorry if am unable to portray what i want

If you can't define your problem you can't solve it.

SAS loops through rows by default and there's a row counter variable _n_ which will work in this situation. The MOD() function will create counters from 1 to 4 or you could do a manual count. Use that variable as an index the array of the ratios. If you have more ratios you can load the temporary array from a dataset rather than type it out. I'll leave that for another day. I've separated the steps to make it more clear below.

``````data have (drop=mult:);
input emp_name \$ sal;
cards;
Kevin 3000
david 2300
Kat 2100
ricky 10000
Fred 100
Betty 2000
Shiva 0
Random .
Check 23
;
run;

data want;
set have;
array mult(4) _temporary_ (0.1, 0.25, 0.09, 0.11); *array with rates;
index=mod(_n_-1, 4)+1; *calculate which value from the array you need;
value=mult(index)*sal; *multiple the value in SAL by the respective value in the array;
run;``````
Contributor
Posts: 45

Thank you @Reeza
Super User
Posts: 23,700

## Re: Arrays

This problem isn't conducive to arrays or loops especially if you need to extend it.

Not sure why you want to go down that route, but it's exactly the wrong approach in SAS.

Post a better example of what you're looking to accomplish here.

If this is a learning exercise, well, to learn it, you need to write the code anyways

shivamarrora0 wrote:

Hi All, i want to use array and loops only

i want to multiply the 1st emp salary by .10 2nd by .25 3rd by .09 and 4th by .11

data have;

input emp_name \$ sal ;

cards;

Kevin 3000

david 2300

Kat 2100

ricky 10000 ;

run;

☑ This topic is solved.