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;
@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;
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
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
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
@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?
@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
@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!
i want this as output
@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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.