From the course: SAS® 9.4 Cert Prep: Part 10 Manipulating Data with Functions

Demo: Using numeric functions

- [Instructor] In this example, we'll use numeric functions to create an anonymous student report that includes the top three quiz scores. In this data step, you can see that we're creating an assignment statement. The column quiz first is using the largest function to get the largest or maximum score from the columns quiz one through quiz five. Now notice we're taking advantage of our column list here by using the keyword of, we can reference the range of columns, Quiz1-Quiz5, which is much easier than typing out each of those column names. I'm going to copy this statement and paste it two additional times and modify the expressions to get the second and third largest quiz scores. And once I have those top three scores, I want to create another column called Top3Avg. Top3Avg will be the mean of quiz first, second and third. Next, we don't want any identifying information in the report so I want to drop name from the output data set. But I do want to give each student a random ID, a four digit number that they can use to identify their scores. So before the set statement, I'll create a new column named StudentID, and I'll use the RAND function with integer as the first argument. This generates random integers between the values specified in the second and third arguments. So if I want a four digit number, I'll use 1000 as the second argument and 9999 as the third argument. I'm placing this statement before the set statement so that Student ID will be the first column added to the PDB and the leftmost column in the output table. So let's see how it looks. That's a good start. We have our random numbers, the top three quiz scores and top three AVG, although you'll notice we have quite a bit of precision for some of these scores, probably more than we want to include in our result. So I'll go back to the code and modify the top three AVG assignment statement to use the round function. The first argument of the round function will be the number returned from the mean function. So after the mean function, I'll add the second argument to the round function, which would be the round off unit. I'll use .1 to round to the nearest tenth and close the parenthesis for the round function. All right, let's see how this looks. Perfect, where there were decimal places, they are rounded to the nearest tenth. This illustrates just a few numeric functions. There are many more that you can use to manipulate your data.

Contents