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);
@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.
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));
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 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.
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.
I use strip and left, and I reverse their order. I still get the
ID_length=length(strip(left(caseID)));
same number, 9.
@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.
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
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.