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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

4 REPLIES 4
LinusH
Tourmaline | Level 20

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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

FreelanceReinh
Jade | Level 19

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;
Ksharp
Super User

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 805 views
  • 4 likes
  • 5 in conversation