Lesson 4, Part 3: SAS Functions, Arrays, and Loops (Additional Code Examples)¶

The SAS code snippets below calculate the overall weighted percentage total for a course 4197 student (1 observation) using the fictitious data.

Step 1: Create a SAS data set named HAVE with the following variables:

  • ID (character variable)
  • TEST1 to TEST5 (5 numeric variables containing test scores)
  • ASSIGNMENT1, MIDTERM, FINAL (3 additional numeric variables containing the respective assessment scores)
  • The DATALINES; ... ; code block inputs just one record.
InĀ [5]:
ods html close;
options nodate nonotes nosource;
data Have;
input id $ TEST1-TEST5 ASSIGNMENT1 MIDTERM FINAL;
datalines;
X1 50 30 100 70 50 72 40 80
;
13 The SAS System

NOTE: Writing HTML5(SASPY_INTERNAL) Body file: _TOMODS1
14 The SAS System

E3969440A681A2408885998500000007

Step 2: Create a second data set (HAVE2) based on HAVE. The data step does the following:

  • The CALL SORTN routine sorts the values of the TEST1-TEST5 values in the descending order (largest to smallest). This routine sorts variables within a single observation (row). It does not change the row order.

    As a sightlight, PROC SORT (not used here) sorts an entire dataset by one or more variables and thus modifies the order of the dataset itself.

  • Defines three arrays:

    • raw[7] stores the 7 numeric variables (TEST1-TEST4, ASSIGNMENT1, MIDTERM, and FINAL) after dropping the lowest test score among the TEST1 through TEST5 variables .

    • weight[7] assigns predefined weights (temporary array, not stored in the dataset).

    • wp[7] holds the weighted values for each corresponding variable.

    • The DO loop loops through each element in raw, multiplies it by its
      corresponding weight, and stores the result in wp (i.e., applying the same calculation to multiple variables).

    • wpt is the weighted total (sum(OF P:) sums all variables that starts with P).

    The DROP statement drops the loop variable i (not needed in the final dataset). Applies weights to TEST1-TEST4, ASSIGNMENT1, MIDTERM, and FINAL.

InĀ [7]:
options nocenter nodate nosource;
data have2;
 set have;
     call sortN(test5, test4, test3, test2, test1); 
	 array raw[7] TEST1-TEST4 ASSIGNMENT1  MIDTERM FINAL; 
     array weight[7] _temporary_ (.05, .05, .05, .05 
				                   ,.10,.35,.35);
	 array wp[7]  P_TEST1-P_TEST4 P_ASSIGNMENT1 
               P_MIDTERM P_FINAL; 
     do i = 1 to 7;
       wp[i] = raw[i]*weight[i];
     end;
      wpt=sum(OF P:);
 drop i;
 run;
17 The SAS System

E3969440A681A2408885998500000009

The PROC PRINT step below lists the observations from the HAVE2 dataset without observation numbers (noobs), displaying:

  • id
  • All variables starting with P_ (P_TEST1 to P_FINAL), containing the weighted value
  • wpt (overall weighted percentage total)

The FORMAT statement formats wpt to 2 decimal places.

InĀ [7]:
proc print data=work.have2 noobs; 
var id P: wpt; 
Format wpt 6.2;
run;
SAS Output

The SAS System

id P_TEST1 P_TEST2 P_TEST3 P_TEST4 P_ASSIGNMENT1 P_MIDTERM P_FINAL wpt
X1 5 3.5 2.5 2.5 7.2 14 28 62.70

Use Cases of SAS Arrays¶

SAS arrays allow you to process multiple variables efficiently in a DATA step. They are commonly used for loop-based operations, reducing repetitive code and improving maintainability.

InĀ [1]:
data training;
 miles = 30;
 do i = 1 to 6;
  miles + miles*.04;
   output;
 end;
 run;

proc print;
run;
SAS Output

The SAS System

Obs miles i
1 31.2000 1
2 32.4480 2
3 33.7459 3
4 35.0958 4
5 36.4996 5
6 37.9596 6
InĀ [3]:
data training;
 miles = 30;
 do i = 1 to 6;
  miles + miles*.04;
   *output;
 end;
 run;

proc print;
run;
SAS Output

The SAS System

Obs miles i
1 37.9596 7
InĀ [7]:
data investment;
 do year = 1 to 5;
  invest+1000;
    do month=1 to 12 by 3;
      invest+50;
      output;
    end;
 end;
 run;

proc print;
run;
SAS Output

The SAS System

Obs year invest month
1 1 1050 1
2 1 1100 4
3 1 1150 7
4 1 1200 10
5 2 2250 1
6 2 2300 4
7 2 2350 7
8 2 2400 10
9 3 3450 1
10 3 3500 4
11 3 3550 7
12 3 3600 10
13 4 4650 1
14 4 4700 4
15 4 4750 7
16 4 4800 10
17 5 5850 1
18 5 5900 4
19 5 5950 7
20 5 6000 10