Help using Base SAS procedures

Looping with an Array

Reply
Occasional Contributor
Posts: 16

Looping with an Array

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

120102FoundationsSkillsetAthletic 22501500500.......311500
120103Athletic 1500.........1111500
120104MilitaryAthleticCorporateAthleticWorkFoundationsFoundations 1500150037504500475052503750...2111500
120104MilitaryAthleticCorporateAthleticWorkFoundationsFoundations 1500150037504500475052503750...4114500
120106TalentFoundationsAthleticLoanAthleticSkillset 375050027505007505000....3112750
120106TalentFoundationsAthleticLoanAthleticSkillset 375050027505007505000....511750
120111LoanCorporateState GrantAthleticFoundationsMilitaryReligiousFoundations 2250525027501750175047502250500..4111750
120112InternalAcademicReligiousLoanMilitaryFoundationsAthletic 1750475030001000525015001500...7111500
120116AcademicCorporateAthleticInternalCorporateCorporateAcademicFoundationsReligious 5001000175035004250250525042505000.3111750
120119MilitaryAthleticAcademicMilitary 3750375050001750......2113750

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

Super User
Posts: 7,758

Re: Looping with an Array

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

!!

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,497

Re: Looping with an Array

Posted in reply to KurtBremser

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.

Occasional Contributor
Posts: 16

Re: Looping with an Array

Posted in reply to KurtBremser

Kurt,

Oh yeah, I am missing a reference.  Thank you both for your assistance.  Much appreciated!

David

Super User
Super User
Posts: 7,942

Re: Looping with an Array

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"...

Occasional Contributor
Posts: 16

Re: Looping with an Array

Correct, I gave that a try as well.

PROC Star
Posts: 7,467

Re: Looping with an Array

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;

Occasional Contributor
Posts: 16

Re: Looping with an Array

Oooh, I like this approach better Art; and, it ignores missing values.

Thanks again,

David

Ask a Question
Discussion stats
  • 7 replies
  • 282 views
  • 0 likes
  • 5 in conversation