How to find fields with trailing or leading space

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 18
Accepted Solution

How to find fields with trailing or leading space

I have a field named Product with some fields having trailing and leading spaces. What code should I use to spot all the products with leading and trailing space out of million observations?

The spaces below are denoted as ...

Product 

Milk..  

..Banana 

Orange.. 

Pear..


Accepted Solutions
Solution
‎12-23-2012 03:51 PM
Super User
Super User
Posts: 6,498

Re: How to find fields with trailing or leading space

SAS stores character strings as fixed length fields so any short string is padded with spaces to the length of the field. Hence there is no such things as "trailing" spaces. To find character strings with leading spaces just test if it starts with a space.

if product =: ' ' then do;

....

end;

View solution in original post


All Replies
Solution
‎12-23-2012 03:51 PM
Super User
Super User
Posts: 6,498

Re: How to find fields with trailing or leading space

SAS stores character strings as fixed length fields so any short string is padded with spaces to the length of the field. Hence there is no such things as "trailing" spaces. To find character strings with leading spaces just test if it starts with a space.

if product =: ' ' then do;

....

end;

Respected Advisor
Posts: 4,641

Re: How to find fields with trailing or leading space

Trailing spaces are inconsequential in SAS as it only handles fixed length character strings; i.e. all character strings are padded with spaces. The number of leading spaces is given by LENGTH(Product) - STRIP(Product).

PG

PG
Respected Advisor
Posts: 3,124

Re: How to find fields with trailing or leading space

If what you mean "trailing" is to find how many blanks are padded, you could try:

lengthc(product)-length(product);

Haikuo

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 317 views
  • 0 likes
  • 4 in conversation