Lesson 6.2: Trim & Compress
In this dataset, we have two variables: Student Name and Phone Number. The code below will show you the TRIM and COMPRESS functions work:
Phone_Trimmed = Trim(Phone);
Phone_Compressed = COMPRESS(Phone);
Now, let's take a look at the Student2 dataset in the Proc Print output. Proc Print output lets you view the data in the output window.
As you can see, the COMPRESS function works according to our description above - to trim all the white space from the variable. The Phone_Compressed variable compresses the original Phone variable and removes all the white spaces between the numbers.
However, the newly created Phone_Trimmed variable remains unchanged from the original Phone variable! This is because the TRIM function removes the white space at the END of the variable. There is no white space to be removed there in the Phone variable.
Let’s use another example to see the effect of the TRIM function.
Info1 = First || Last;
Info2 = TRIM(First) || Last;
In this example, the double stroke ( || ) is used to combine the different texts into one variable. Both Info1 and Info2 combine the First and Last Name into one text.
Again, Let's take a look at the output:
As you can see, Info2, with the TRIM function, eliminates the white space at the end of the First variable.
DONE! You have learned the TRIM and COMPRESS functions in SAS!