SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
epibio2021
Obsidian | Level 7

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! 

2 REPLIES 2
Tom
Super User Tom
Super User

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.

Astounding
PROC Star

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.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 2701 views
  • 2 likes
  • 3 in conversation