From the course: SAS® 9.4 Cert Prep: Part 04 Preparing Data

Demo: Using character functions

- [Instructor] We're going to use some character functions to manipulate existing character values. So let's start looking at the data. So notice the Name column is all in uppercase. I would like it to be in proper case. Basin, we've been observing for a while now that we have some lowercase codes for Basin. I'd like to make sure that all codes are in uppercase. We have two separate columns for the hemispheres, north, south, and east, west. I'd like to concatenate those two characters into a single column. And finally, I'd like to look at the column Basin and extract the last letter of Basin to represent the ocean code, either Atlantic, Pacific, or Indian. So that's my goal. I'll close the table and take a look at this program. So for my data step, I'm creating a new temporary table named storm_new, and reading the existing pg1.storm_summary. Let's go ahead and add the assignment statements. So the first thing I want to do is convert Basin to uppercase, and to do that I'll build an assignment statement. Basin=upcase(basin); So notice in this statement I'm using the same column as both the input to the function as well as the column name that we're writing to. So we will overwrite Basin codes with the uppercase value. And I'll do something similar for Name. But instead, with proper case. Next I'll create the Hemisphere column. To concatenate the Hemisphere letters together, I'll use the cats function, and the arguments will be Hem_NS and Hem_EW; Remember the cats function will remove any leading or trailing blanks around the values of those arguments before concatenating them together. And finally, I'll add an assignment statement to create Ocean. And for Ocean, I'll use the substring function, and the first argument will be the column I want to extract the value from. So that's Basin. The second argument is the start position that I want to read. So that will be position number 2. And the third argument is how many characters I want to read. I just want the last character. And that's the conclusion of my data step, so let's run the program. And notice that Name is now in proper case, Basin is in uppercase. I've created the two new columns, Hemisphere as well as my Ocean code. It looks great. There are so many things you can do with character functions to manipulate your data.

Contents