Arrays in SAS

Accepted Solution Solved
Reply
Contributor
Posts: 70
Accepted Solution

Arrays in SAS

Hello

 

I have a data set which looks as 

 

ID Test_1 Test_2 Test_3 Test_4 Test_5
SH101 90 88 92 95 90
SH102 64 64 77 72 71

 

Trying to count the number of tests passed by each student using temporary sas array values initialized as (65,70,60,62,68).

 

DATA new;
SET Test_scores;
array test(5) Test_1 -- Test_5;
ARRAY num[5] _TEMPORARY_ (65,70,60,62,68);
do i=1 to dim(num);
if Test(i) < num(i) then flag="RA";
drop i;
end;
Run;

 

Request you to help me with the codes. I would like to check across columns.

 

Kafeel


Accepted Solutions
Solution
‎03-24-2016 04:42 AM
Super User
Posts: 9,878

Re: Arrays in SAS

Since you want count the number , why you make a character variable FLAG?

 

DATA new;
SET Test_scores;
array test(5) Test_1 -- Test_5;
ARRAY num[5] _TEMPORARY_ (65,70,60,62,68);

pass=0;
do i=1 to dim(num);
if Test(i) >= num(i) then pass+1;
drop i;
end;
Run;

View solution in original post


All Replies
Super User
Posts: 5,391

Re: Arrays in SAS

Having the data transposed from wide to long, makes it much easier to do this kind of counting (simple count() in SQL).

Data never sleeps
Super User
Super User
Posts: 7,725

Re: Arrays in SAS

As @LinusH has said, normalising your data, so that it goes down the page is easier to work with - if you need transposed do it before the output step.  As for your array question the code:

data have;
  input id $ test_1 test_2 test_3 test_4 test_5;
datalines;
SH101 90 88 92 95 90
SH102 64 64 77 72 71
;
run;

data want (drop=i);
  set have;
  array test_{5};
  array num{5} _temporary_ (65,70,60,62,68);
  do i=1 to 5;
    if test_{i} < num{i} then flag="RA";
  end;
run;

Works fine per the logic given, what is the problem?  Also note the use of consistent case in the code, consitent indentations etc.  You can use the {i} above the post window to enter a code window.

Trusted Advisor
Posts: 1,116

Re: Arrays in SAS

Hello Kafeel,

 

To count the number of tests passed by each student you could insert two lines of code into your program:

 

After the IF statement:

else num_passed+1;

This sum statement implies a RETAIN for variable NUM_PASSED. Therefore, the counter has to be reset before the DO statement:

num_passed=0;
Solution
‎03-24-2016 04:42 AM
Super User
Posts: 9,878

Re: Arrays in SAS

Since you want count the number , why you make a character variable FLAG?

 

DATA new;
SET Test_scores;
array test(5) Test_1 -- Test_5;
ARRAY num[5] _TEMPORARY_ (65,70,60,62,68);

pass=0;
do i=1 to dim(num);
if Test(i) >= num(i) then pass+1;
drop i;
end;
Run;

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 367 views
  • 4 likes
  • 5 in conversation