1

I'm reading through the book, "SAS Functions by Example - Second Edition" and having trouble trying to understand a certain function due to the example and output they get.

Function: FINDC
Purpose: To locate a character that appears or does not appear within a string. With optional arguments, you can define the starting point for the search, set the direction of the search, ignore case or trailing blanks, or look for characters except the ones listed.

Syntax: FINDC(character-value, find-characters <,'modifiers'> <,start>)

Two of the modifiers are i and k:

i ignore case
k count only characters that are not in the list of find-characters

So now one of the examples has this:

Note: STRING1 = "Apples and Books"

FINDC(STRING1,"aple",'ki')

For the Output, they said it returns 1 because the position of "A" in Apple. However this is what confuses me, because I thought the k modifier says to find characters that are not in the find-characters list. So why is it searching for a when the letter "A", case-ignored, is in the find-characters list. To me, I feel like this example should output 6 for the "s" in Apples.

Is anyone able to help explain the k modifier to me any better, and why the output for this answer is 1 instead of 6?

Edit 1

Reading the SAS documentation online, I found this example which seems to contradict the book I'm reading:

Example 3: Searching for Characters and Using the K Modifier
This example searches a character string and returns the characters that do not appear in the character list.

data _null_;
   string = 'Hi, ho!';
   charlist = 'hi';
   j = 0;
   do until (j = 0);
      j = findc(string, charlist, "k", j+1);
      if j = 0 then put +3 "That's all";
      else do;
         c = substr(string, j, 1);
         put +3 j= c=;
      end;
   end;
run;

SAS writes the following output to the log: 
   j=1 c=H
   j=3 c=,
   j=4 c= 
   j=6 c=o
   j=7 c=!
   That's all

So, is the book wrong?

amallard
  • 1,189
  • 1
  • 12
  • 24
  • 1
    This is one of those cases where internal consistency ends up with something that doesn't make much sense. `k` does this because it first showed up in `compress`, where it tells compress instead of removing characters in [list] it should `k`eep them. Makes no sense for this to be `k` here, but since it already means this in `compress` and it guarantees we won't have any other collisions ... – Joe Jul 27 '17 at 18:04

1 Answers1

3

The book is wrong.

511  data _null_;
512    STRING1 = "Apples and Books" ;
513    x=FINDC(STRING1,"aple",'ki');
514    put x=;
515    if x then do;
516      ch=char(string1,x);
517      put ch=;
518    end;
519  run;

x=6
ch=s
Tom
  • 47,574
  • 2
  • 16
  • 29