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

Hello,

I would like to inquire about the length of the text ' EC1R0000' and 'ER3R00053', why did the result come back showing the same?   I expect the first one is 8 and the second one is 9.  Please help.

ID_cat=length(ID);
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@ybz12003 wrote:

I use strip and left, and I reverse their order.  I still get the

ID_length=length(strip(left(caseID)));

same number, 9.

 

 


PUT the value with HEX. You have something besides a space character.

View solution in original post

11 REPLIES 11
ballardw
Super User

 

What is the reported length? If 9 it is because the first value has a leading space, assuming you have shown the correct definition of the value.

 

Length function will not count trailing blanks in a value but will count leading spaces. If you don't want that behavior then use LEFT or STRIP functions inside the length call:  Length(left(somevar));

ybz12003
Rhodochrosite | Level 12
I use strip function, they still show the same result.
sbxkoenk
SAS Super FREQ

remove the leading space in the first ID and it will be 8.
Use left function for example (to remove leading space).

 

data _NULL_;
ID=' EC1R0000'; ID_cat=length(ID); put ID_cat=; 
ID=' EC1R0000'; ID=left(ID); ID_cat=length(ID); put ID_cat=; 
ID='ER3R00053'; ID_cat=length(ID); put ID_cat=;
run;

 

Koen

ybz12003
Rhodochrosite | Level 12
Even with Strip function, both results show 9.
ballardw
Super User

@ybz12003 wrote:
Even with Strip function, both results show 9.

Which means that the leading character is NOT a space character but something else. Possibly ASCII 255 (null character).

 

Try putting that variable with a $HEX format. Below is an example with the first character an ASCII 255, which displays in hex as A0 below, 31 is a 1, 32 is 2, 33 is 3, 34 is 4 and 20 is a space character

64   data _null_;
65     char = ' 1234 ';
66     put char $hex.;
67    run;

A03132333420

Strip and Left remove spaces, not other non-printing characters. Might even be a tab or similar.

ybz12003
Rhodochrosite | Level 12
I got two different results with the same IDs 'EC1R00002' by running two different codes.
What are they meaning?
The first one showed "EC1R00002" with HEX value, "454331523030303032".
The second one showed "EC1R0000" with HEX value, "094543315230303030".
Tom
Super User Tom
Super User

So your second string has a TAB character in the front.  To remove the tabs use COMPRESS() function. To convert to space use the TRANSLATE() function.

compress(ID,'09'x)
translate(ID,' ','09'x)

You might want to investigate how you got the TAB character into your variable.  Note that the SAS editor in SAS/Studio has options to replace tabs with spaces when editing your program files.  You should enable this so that your program files do not accidentally get populated with tab characters.

Screenshot 2022-03-03 135528.jpg

ybz12003
Rhodochrosite | Level 12
I will create another question in the community.
ybz12003
Rhodochrosite | Level 12

I use strip and left, and I reverse their order.  I still get the

ID_length=length(strip(left(caseID)));

same number, 9.

 

 

ballardw
Super User

@ybz12003 wrote:

I use strip and left, and I reverse their order.  I still get the

ID_length=length(strip(left(caseID)));

same number, 9.

 

 


PUT the value with HEX. You have something besides a space character.

sbxkoenk
SAS Super FREQ

I agree with @ballardw : PUT the value with HEX. You have something besides a space character.

 

You can also check if you have non-printable characters with the NOTPRINT function.

NOTPRINT Function Searches a character string for a nonprintable character, and returns the first position at which that character is found.

 

You can also compress with the 's' modifier:

compress function with 's' modifier.

s or S adds space characters (blank, horizontal tab, vertical tab, carriage return, line feed, form feed, and NBSP ('A0'x, or 160 decimal ASCII) to the list of characters.

 

Cheers,

Koen

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
  • 11 replies
  • 970 views
  • 8 likes
  • 4 in conversation