Hello,
I am trying to determine the level of detail in character variables by counting the number of characters in the values.
As an example, variable 'comments' has values like 'very good', 'excellent', and 'poor service'. I want to generate a variable that will count all the characters in the 'comments' variable and will return something like this:
very good - 9 characters
excellent - 9 characters
poor service - 12 characters.
I have to do this for multiple variables, but I don't know how to approach this.
Thank you in advance!
The LENGTH() function will tell you how many bytes until the last non-blank value. That looks like what you want.
And for single byte encoding it should work. But if you are using a multi-byte encoding, like UTF-8, then some characters take more than one byte to be represented. In that case use KLENGTH() function instead. That will count characters instead of bytes.
Some of the loose ends that you need to wrap up before programming begins ...
You have already indicated that embedded blanks should be counted. What about leading blanks? Should they also be counted?
Do you have an exact count of how many variables you want to do this for? Is it every character variable in your data set?
You will need to invent variables to hold the results, such as L1 for the length of the first variable, L2 for the length of the second variable, etc.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.