Hello,
I'm attempting to accumulate/sum the amounts associated with all the "Athletic" fund_types. However, my loop essentially ends at first iteration. What am I missing (see code below)?
student_id Fund_Type1 Fund_Type2 Fund_Type3 Fund_Type4 Fund_Type5 Fund_Type6 Fund_Type7 Fund_Type8 Fund_Type9 amount1........................................................................amount10 j k Athletic
120102 | Foundations | Skillset | Athletic | 2250 | 1500 | 500 | . | . | . | . | . | . | . | 3 | 11 | 500 | |||||||
120103 | Athletic | 1500 | . | . | . | . | . | . | . | . | . | 1 | 11 | 1500 | |||||||||
120104 | Military | Athletic | Corporate | Athletic | Work | Foundations | Foundations | 1500 | 1500 | 3750 | 4500 | 4750 | 5250 | 3750 | . | . | . | 2 | 11 | 1500 | |||
120104 | Military | Athletic | Corporate | Athletic | Work | Foundations | Foundations | 1500 | 1500 | 3750 | 4500 | 4750 | 5250 | 3750 | . | . | . | 4 | 11 | 4500 | |||
120106 | Talent | Foundations | Athletic | Loan | Athletic | Skillset | 3750 | 500 | 2750 | 500 | 750 | 5000 | . | . | . | . | 3 | 11 | 2750 | ||||
120106 | Talent | Foundations | Athletic | Loan | Athletic | Skillset | 3750 | 500 | 2750 | 500 | 750 | 5000 | . | . | . | . | 5 | 11 | 750 | ||||
120111 | Loan | Corporate | State Grant | Athletic | Foundations | Military | Religious | Foundations | 2250 | 5250 | 2750 | 1750 | 1750 | 4750 | 2250 | 500 | . | . | 4 | 11 | 1750 | ||
120112 | Internal | Academic | Religious | Loan | Military | Foundations | Athletic | 1750 | 4750 | 3000 | 1000 | 5250 | 1500 | 1500 | . | . | . | 7 | 11 | 1500 | |||
120116 | Academic | Corporate | Athletic | Internal | Corporate | Corporate | Academic | Foundations | Religious | 500 | 1000 | 1750 | 3500 | 4250 | 250 | 5250 | 4250 | 5000 | . | 3 | 11 | 1750 | |
120119 | Military | Athletic | Academic | Military | 3750 | 3750 | 5000 | 1750 | . | . | . | . | . | . | 2 | 11 | 3750 |
Data Work.types_aid_merged;
merge schol_types_rot
Sasuser.Student_aid;
by student_id;
array fund_type{*} fund_type:;
array amount{*} amount:;
do j=1 to dim(fund_type);
if fund_type{j}="Athletic" then do;
do k=1 to dim(amount);
Athletic=0;
Athletic+amount{j};
end;
output;
end;
end;
RUN;
Thank you,
David
I don't find a reference for your variable_name: notation in the SAS array documentation. Use fund_type1-fund_type9 and amount1-amount9 instead.
[Update]
The notation works fine.
But:
Write your programs so that corresponding do and end statements line up on the same column.
I also find that
Athletic=0;
Athletic+amount{j};
give the same result as
Athletic=amount{j};
!!
In terms of your original program, this likely means that you should move the statement:
Athletic=0;
It probably belongs one line above, before looping with K.
Kurt,
Oh yeah, I am missing a reference. Thank you both for your assistance. Much appreciated!
David
Hi,
Well, if you data was normalized e.g.:
STUDENT_ID FUND_TYPE AMOUNT
120012 Athletic 123
120012 Military 567
...
It would be a simple sum() where FUND_TYPE="Athletic"...
Correct, I gave that a try as well.
David,
I would try something like the following. My data have is using the file resulting from your merge:
data want;
set have;
array fund_type{*} fund_type:;
array amount{*} amount:;
Athletic=0;
do j=1 to dim(fund_type);
if fund_type{j}="Athletic" then Athletic+amount{j};
end;
run;
Oooh, I like this approach better Art; and, it ignores missing values.
Thanks again,
David
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.