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
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;
Having the data transposed from wide to long, makes it much easier to do this kind of counting (simple count() in SQL).
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.
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;
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.