BookmarkSubscribeRSS Feed
shahsn11
Fluorite | Level 6

Hi,

 

I am trying to understand FindC function with the help of the below example

 

data test;
input MEDICARE_ID $ 1-64;
datalines;
_2E8a
X3628970
A2
_N_
_______
315
_ab12A:5
_error_
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabc
;
run;
data test1;
 set test;
whereisit=findc(trim(upcase(MEDICARE_ID)), '2');
run;

If you run the above code, i am getting the below output

FindC.PNGI am fine with all the output except the 3rd one where i was expecting "2" , because 2 is at 2nd location. 

 

So, i would like to know what is wrong in my understanding.

 

Thanks.

5 REPLIES 5
Tom
Super User Tom
Super User

That value must have some invisible characters.

Put the value using $HEX format to see what they are.

hashman
Ammonite | Level 13

@Tom:

 

It could hardly be explained by the presence of invisible characters, either: The string is searched left-to-right and begins with "A2", all of whose characters are printable. Most likely, the real data are different from the sample presented in the original post. 

 

Paul D.

hashman
Ammonite | Level 13

@shahsn11:

 

It can be explained only if you run your test test against data different from the sample data you've presented here. When I run it against your data verbatim, I get the output just as expected - and as should be expected. The FINDC function, the way you call it, finds the leftmost occurrence of "2" in each string where "2" is present. For "A2", it returns 2. No one can help you with a problem if it cannot be replicated.

 

Paul D. 

FreelanceReinh
Jade | Level 19

Hi @shahsn11,

 

When I run your program and print dataset TEST1, two previously invisible characters appear in the output window. Both are displayed as question marks.

 

Partial output:

Obs    MEDICARE_ID                                                whereisit

 1     _2E8?a                                                         2
 2     X3628970                                                       4
 3     A?2                                                            3

Obviously, the second occurrence has an impact on the result.

 

However, when I copy your program into a Notepad (notepad.exe) window, save the file using UTF-8 coding and then open it in a hex editor, the above two "question marks" turn out to be three characters each: The sequence of hex codes is E2 81 AA in both cases. This would explain exactly the result whereisit=5 for the third observation.

 

Tom
Super User Tom
Super User

Note that if you data is not using a single byte character set then you need to use KINDEXC() function.

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=nlsref&docsetTarget=p1pca...

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
  • 5 replies
  • 1406 views
  • 0 likes
  • 4 in conversation