turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Arrays

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-10-2017 02:39 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to shivamarrora0

07-10-2017 03:34 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to shivamarrora0

07-10-2017 02:48 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

07-10-2017 03:04 PM

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;

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;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to shivamarrora0

07-10-2017 03:09 PM - edited 07-10-2017 03:10 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

07-10-2017 03:21 PM

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

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to shivamarrora0

07-10-2017 03:27 PM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

07-10-2017 03:36 PM

@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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

07-10-2017 03:48 PM - edited 07-10-2017 04:15 PM

@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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

07-10-2017 04:06 PM

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!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to art297

07-10-2017 03:36 PM

i want this as output

Solution

07-10-2017
03:40 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to shivamarrora0

07-10-2017 03:34 PM

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;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

07-10-2017 03:42 PM

Thank you @Reeza

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to shivamarrora0

07-10-2017 03:14 PM

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;