- Back to Home »
- Arrays , Learning , SAS »
- Learning SAS: Arrays in SAS
Posted by : Netbloggy
Saturday, September 19, 2015
Unlike many other programming languages, Arrays in SAS don't store the values but just the pointers to the actual values (very much like C Pointers).
Problem:
Using the SAS data set Scores, create a new, temporary SAS data set (reversed)
where the values of the variables test1–test5 are reversed as follows: 1 -> 5; 2
-> 4; 3 -> 3; 4 -> 2; 5 -> 1.
Solution:
data A15001.A01_scores; input ID $ test1-test5; datalines; 001 90 88 92 95 90 002 64 64 77 72 71 003 68 69 80 75 70 004 88 77 66 77 67 ; run; data A15001.A01_reversed; set A15001.A01_scores; array test{5}; array ques{5} ques1-ques5; j = 5; do i = 1 to 5; ques{j} = test{i}; put j= i= ques{j}= test{i}=; j + (-1); end; do i = 1 to 5; test{i} = ques{i}; end; drop ques1-ques5 i j; run; Title 'The actual scores'; proc print data=A15001.A01_scores;run; title; Title 'The reversed scores'; proc print data=A15001.A01_reversed; run; title;
Output:
Learning:
- How to efficiently use Arrays to reverse a series of numbers
- Drop not-required Arrays
Problem:
The passing score on each of five tests is 65, 70, 60, 62, and 68. Using the data here, use a temporary array to count the number of tests passed by each student.
Solution:
data A15001.A01_passing; set A15001.A01_scores; array passscore{5} _temporary_ (65,70,60,62,68); array test[5]; rawscore = 0; do tst = 1 to 5; rawscore + (test{tst} ge passscore{tst}); end; drop tst; run; proc print data=A15001.A01_passing; run;
Output:
Learning:
- Creating temporary array using _temporary_
Download the code here