BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Cruise
Ammonite | Level 13

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. 

 

blanks.png

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
ballardw
Super User

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.

View solution in original post

21 REPLIES 21
novinosrin
Tourmaline | Level 20

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

Cruise
Ammonite | Level 13

@novinosrin 

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

NOVINOSIRIN.png

novinosrin
Tourmaline | Level 20

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'));
Reeza
Super User

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

 

 

Shmuel
Garnet | Level 18

Maybe the indent is not a result of blanks.

check the left most character by:

indent = substr(municipality,1,1);
put indent $hex2.;
Cruise
Ammonite | Level 13
Thanks Shmuel. It returned E3. What would E3 indicate? I have no idea.
Cruise
Ammonite | Level 13
Variable indent is black. Took no values. I'm confused.
ballardw
Super User

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
Ammonite | Level 13

@ballardw 

thank you, pls see rank results below. testing your code on my data now. 

 

balardw.png

Cruise
Ammonite | Level 13
Thanks Ballardw, what would 227 indicate?
ballardw
Super User

@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.

 

ballardw
Super User

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
Ammonite | Level 13

@ballardw I got leading blank replaced by weird symbols as shown below. 

 

balardw2.png

novinosrin
Tourmaline | Level 20

@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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 21 replies
  • 1303 views
  • 7 likes
  • 6 in conversation