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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
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

10 REPLIES 10
Astounding
PROC Star

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;

PaigeMiller
Diamond | Level 26
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
art297
Opal | Level 21

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

 

 

RickyS
Quartz | Level 8

TY for the quick responses everyone

RickyS
Quartz | Level 8

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

RickyS
Quartz | Level 8

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

art297
Opal | Level 21

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

 

Astounding
PROC Star

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.

RickyS
Quartz | Level 8

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;

Astounding
PROC Star

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 10 replies
  • 1342 views
  • 1 like
  • 4 in conversation