Hi Folks:
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;
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.
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
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
' ' is blank
id_strip=strip(compress(Municipality,' ','xc'));
Try displaying the variable with a hex format to see what character are there.
Maybe the indent is not a result of blanks.
check the left most character by:
indent = substr(municipality,1,1);
put indent $hex2.;
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:
data want;
set have;
x = rank(first(municipality));
run;
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.
@Cruise wrote:
Thanks Ballardw, what would 227 indicate?
Anything above 127 is font dependent. Typically used for "foreign language" characters from an American English speakers point of view (Hint: what does the A in ASCII stand for?). Or graphic characters.
For kicks you could run this code:
data _null_; file print; do x= 32 to 255; z= byte(x); put x= z=; end; run;
save the result to a text file. Open the file and change the display font to see things change.
You should be able to use Byte(227) as a character to remove with COMPRESS.
compress(municipality,byte(227))
May still need to use COMPBL to remove blanks depending on result.
@Cruise Just extract the need if thats ok?
data want;
set have;
length id_strip $15;
id_strip=substr(municipality,anyalpha(municipality));
check_length=lengthn(id_strip);
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.