DATA Step, Macro, Functions and more

Understanding FindC function

Reply
Occasional Contributor
Posts: 10

Understanding FindC function

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.

Super User
Super User
Posts: 8,279

Re: Understanding FindC function

That value must have some invisible characters.

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

Frequent Contributor
Posts: 112

Re: Understanding FindC function

[ Edited ]

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

Frequent Contributor
Posts: 112

Re: Understanding FindC function

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

Trusted Advisor
Posts: 1,325

Re: Understanding FindC function

[ Edited ]

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.

 

Super User
Super User
Posts: 8,279

Re: Understanding FindC function

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

Ask a Question
Discussion stats
  • 5 replies
  • 71 views
  • 0 likes
  • 4 in conversation