DATA Step, Macro, Functions and more

Array to replace if then statement 9.4

Accepted Solution Solved
Reply
Contributor
Posts: 32
Accepted Solution

Array to replace if then statement 9.4

Need to create an array with a comparison that enables a cumulative count after importing a text file , can get there with if then and sum statement but am stuck trying to create an array to do same.

 

DATA Answers;

infile grades;

input id $ score1 score2 score3 score4 score5;

      if score1 > 64 then Pass1 = 1;

      if score2 > 64 then Pass2 = 1;

      if score3 > 64 then Pass3 = 1;

      if score4 > 64 then Pass4 = 1;

      if score5 > 64 then Pass5 = 1;

            Passed = Sum(of Pass1-Pass5);

            drop Pass1 - Pass5;

RUN;

PROC PRINT NOOBS;

RUN;


Accepted Solutions
Solution
‎03-18-2018 12:15 PM
Respected Advisor
Posts: 2,659

Re: Array to replace if then statement 9.4

[ Edited ]
data answers;
    infile grades;
    input id $ score1 score2 score3 score4 score5;
    array score score1-score5;
    array pass pass1-pass5;
    do i = 1 to dim(score);
        if score(i)>65 then pass(i)=1;
    end;
passed=sum(pass1-pass5);
run;

etc.

--
Paige Miller

View solution in original post


All Replies
Super User
Posts: 6,542

Re: Array to replace if then statement 9.4

I think this is what you are after:

 

data want;

set have;

array scores {5} score1-score5;

passed = 0;

do k=1 to 5;

   if scores{k} > 64 then passed + 1;

end;

drop k;

run;

Solution
‎03-18-2018 12:15 PM
Respected Advisor
Posts: 2,659

Re: Array to replace if then statement 9.4

[ Edited ]
data answers;
    infile grades;
    input id $ score1 score2 score3 score4 score5;
    array score score1-score5;
    array pass pass1-pass5;
    do i = 1 to dim(score);
        if score(i)>65 then pass(i)=1;
    end;
passed=sum(pass1-pass5);
run;

etc.

--
Paige Miller
PROC Star
Posts: 8,114

Re: Array to replace if then statement 9.4

You already have a couple of answers, but heres a slightly different way:

data Answers;
  infile cards;
  input id $ score1-score5;
  array scores score1-score5;
  do over scores;
    Passed=sum(Passed,scores gt 64);
  end;
  cards;
0 1 1 1 1 1
1 1 70 1 1 1
2 90 2 2 2 88
3 3 65 3 66 67
;

Art, CEO, AnalystFinder.com

 

 

Contributor
Posts: 32

Re: Array to replace if then statement 9.4

TY for the quick responses everyone

Contributor
Posts: 32

Re: Array to replace if then statement 9.4

all of the solutions work curious as to which is most efficient if anyone knows

Contributor
Posts: 32

Re: Array to replace if then statement 9.4

found some posts with do over no longer supported in 9.4 but liked by many

PROC Star
Posts: 8,114

Re: Array to replace if then statement 9.4

I don't think "do over" has ever been documented, but I've always found it to be convenient. However, it's major problem is that (since it's not documented), is that it may not be available in future releases.

 

As for @Astounding's comment about all of the proposed solutions can't be correct .. I agree! The one you selected as the solution doesn't provide the correct answer.

 

As for which method is fastest, I also agree with @Astounding. I tested the various solutions on a 2 million record file: (1) "do over" was slightly slower than a regular do loop and (2) using do i= 1 to dim(arrayname) was slightly slower than using do i= 1 to 5. However, even on a 2 million record file, the cpu times only ranged between around .18 seconds and .22 seconds.

 

The fastest method was:

data want;
  set have;
  Passed=0;
  Passed+(score1 gt 64);
  Passed+(score2 gt 64);
  Passed+(score3 gt 64);
  Passed+(score4 gt 64);
  Passed+(score5 gt 64);
run;

It ran twice as fast as any of the methods but, of course, required more time to type.

 

Art, CEO, AnalystFinder.com

 

Super User
Posts: 6,542

Re: Array to replace if then statement 9.4

[ Edited ]

That can't possibly be that all of the solutions work.  They don't all produce the same result so at least one of them must be wrong.  I would suggest you test them on a few dozen observations.

 

Most likely, efficiency means choosing the solution that you are most comfortable with, the one that you could modify and adapt as needed.  Unless you are dealing with millions of observations, it is unlikely you could measure a difference in speed.

Contributor
Posts: 32

Re: Array to replace if then statement 9.4

Posted in reply to Astounding

DATA Answers;

  infile grades;

  input id $ score1 score2 score3 score4 score5;

      array score score1-score5;

      array pass pass1-pass5;

      do i = 1 to dim(score);

            if score(i) > 65 then Pass(i) = 1;

            end;

            Passed = Sum(of Pass1-Pass5);

            drop i Pass1 - Pass5;

      RUN;

PROC PRINT NOOBS;

RUN;

Super User
Posts: 6,542

Re: Array to replace if then statement 9.4

That covers one of the issues, adding the word "of".

 

Next, try using some scores that are exactly 65.  That's the last issue that I noticed.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 192 views
  • 1 like
  • 4 in conversation