BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
shivamarrora0
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

View solution in original post

12 REPLIES 12
art297
Opal | Level 21
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

 

shivamarrora0
Obsidian | Level 7
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;
art297
Opal | Level 21

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

 

shivamarrora0
Obsidian | Level 7
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
art297
Opal | Level 21

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

 

Reeza
Super User

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

art297
Opal | Level 21

@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

 

Reeza
Super User

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

shivamarrora0
Obsidian | Level 7

sas.PNG

i want this as output

Reeza
Super User

@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;
shivamarrora0
Obsidian | Level 7
Thank you @Reeza 🙂
Reeza
Super User

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;


 

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 12 replies
  • 1392 views
  • 3 likes
  • 3 in conversation