Sas remove leading blanks in string

I'm trying to remove the leading indent from a variable. None of trim, trimn, strip and compbl functions work.

What is not working here? I appreciate your suggestions.

data test_urb1; set test_urb; ID_strip=strip(Municipality); ID_trim=trim(left(Municipality)); ID_trimn=trimn(left(Municipality)); ID_compbl=compbl(Municipality); run; 
1 ACCEPTED SOLUTION
Accepted Solutions Super User Re: Leading blank in strings Posted 05-05-2020 07:17 PM (2143 views) | In reply to Cruise

Repeats of the same 3 characters is making me think that your actual data may be DBCS.

And perhaps the KCOMPRESS, KTRIM may be wanted.

21 REPLIES 21 Tourmaline | Level 20 Re: Leading blank in strings Posted 05-05-2020 06:43 PM (1625 views) | In reply to Cruise

Please check the length's of each value of the Municipality variable in test_urb1 vs Id_strip

using length function.

len=lengthn(id_strip) and notice the difference in the lengths. This should give you some idea than deceiving visuals

Ammonite | Level 13 Re: Leading blank in strings Posted 05-05-2020 06:48 PM (1611 views) | In reply to novinosrin

Interesting. What shall I do? Take it as a success in using strip function?

Tourmaline | Level 20 Re: Leading blank in strings Posted 05-05-2020 06:55 PM (1580 views) | In reply to Cruise

Nope all values being 12 bytes of length concerns me.

You may try to remove hexadecimal characters and control chars if any using:

X is hexadecimal characters

C is control chars

id_strip=strip(compress(Municipality,' ','xc'));
Super User Re: Leading blank in strings Posted 05-05-2020 06:44 PM (1615 views) | In reply to Cruise

Try displaying the variable with a hex format to see what character are there.

Garnet | Level 18 Re: Leading blank in strings Posted 05-05-2020 06:48 PM (1609 views) | In reply to Cruise

Maybe the indent is not a result of blanks.

check the left most character by:

indent = substr(municipality,1,1); put indent $hex2.;
Ammonite | Level 13 Re: Leading blank in strings Posted 05-05-2020 07:02 PM (1556 views) | In reply to Shmuel Thanks Shmuel. It returned E3. What would E3 indicate? I have no idea. Ammonite | Level 13 Re: Leading blank in strings Posted 05-05-2020 07:03 PM (1554 views) | In reply to Shmuel Variable indent is black. Took no values. I'm confused. Super User Re: Leading blank in strings Posted 05-05-2020 06:53 PM (1598 views) | In reply to Cruise

Use the data step generator to show what values you actually have:
Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat. will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the <> icon or attached as text to show exactly what you have and that we can test code against.

Warning: if you are using some double-byte character set this may not help.

Or use the RANK function to see what the first character may be:

X would be the order in the ASCII (or EBCDIC) character set. If the value is not 32 you do not have a blank. Note that Rank will return the Hex number.

I'm not sure if this will work but the second string below starts with a null character that can be entered on some systems using Alt plus the digits 255 and if the box works correctly the second X= should show 160 (the hex equiv of 255).

data example; string = ' abcdef'; x = rank(first(string)); put x=; string = ' abcdef'; x = rank(first(string)); put x=; run;

The functions you tried would not remove the null as they are not blanks, i.e. ASCII 32.