Fluorite | Level 6

## SAS index function

Hello,

I have following codes:

data test;

input var \$;

cards;

;

data test;

set test;

position=index(var,"A");

run;

here, position=2, which is the position of first "A" in the string. Now, my question is: how can I get the position of third (or second, fourth, ...) "A" in the string?

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: SAS index function

You can use the FIND() function. It will let you specify a starting position for the search.  If you want to search for any single character from a list of characters then use the FINDC() function.

Example:

data test;

input var \$;

put var / @ ;

position=find(var,"A");

do while (position);

put @position 'X' @ ;

position=find(var,"A",position+1);

end;

cards;

;

. X X  X

9 REPLIES 9
Rhodochrosite | Level 12

## Re: SAS index function

I would recommend you look into Help Document for the FINDC function.

I think it would be more suitable to what you want to do.

Hope this helps,

Ahmed

## Re: SAS index function

```AhmedAl-Attar wrote:

I would recommend you look into Help Document for the FINDC function.
I think it would be more suitable to what you want to do.

Hope this helps,
Ahmed
```

This is a good answer but requires RTM and no one does that anymore.

data test;
input var \$;
retain target 'A';

do s = 1 by 0;
r = findc(var,target,s);

if r eq 0 and s=1 then output;

if r eq 0 then leave;
output;
s = r+
1;

end;

drop s;
cards;
A
BBBB
AAAAA
;;;;
run;
Quartz | Level 8

## Re: SAS index function

Try this:

data test;

input var \$;

cards;

AAAAAAAA

XXXXXXXX

XXAAXXAX

;

run;

data test(keep=var msg);

set test;

length msg \$ 80;

n=count(var,'A');

if n=0 then msg="Any letter 'A' found";

else do;

msg="Letter 'A' found in positions ";

var2=var;

do i=1 to n;

p=index(var2,'A');

msg=strip(msg) || put(p,2.);

substr(var2,p,1)='*';

end;

end;

run;

CTorres

PROC Star

## Re: SAS index function

Your best bet is to learn the PRX functions, such as PRXPOSN.  If you had to do this the "old school" way, here is an approach to find the 3rd occurrence:

data want;

set have;

position_n=3;

string_to_search = value || ' ';

position=0;

do _n_=1 to position_n;

found_location = index(string_to_search, "A");

if found_location then do;

position + found_location;

string_to_search = substr(string_to_search, found_location + 1);

end;

else do;

position = 0;

_n_ = position_n;

end;

end;

run;

There can be (solvable) complications when the string you are searching for is a blank, rather than "A".  The code is untested, so you may need to tweak it mildly.

Good luck.

Quartz | Level 8

## Re: SAS index function

DATA HAVE;

ATTRIB VAR LENGTH = \$8.;

INPUT VAR \$;

DATALINES;

AAAAAAAA

;

RUN;

%LET DIM  = ;

%LET DSID = %SYSFUNC(OPEN(HAVE,I));

%LET DIM  = %SYSFUNC(VARLEN(&DSID,%SYSFUNC(VARNUM(&DSID,VAR))));

%LET RC   = %SYSFUNC(CLOSE(&DSID));

%PUT ********&DIM;

DATA WANT;

SET HAVE;

ATTRIB VAR1 LENGTH=\$&DIM..;

ARRAY POS APOS1-APOS&DIM.;

DO I = 1 TO COUNT(VAR,"A");

IF I = 1 THEN DO;

POS{I} = INDEX(VAR,"A");

VAR1   = SUBSTR(VAR,INDEX(VAR,"A")+1);

END;

ELSE DO;

POS{I} = SUM(INDEX(VAR1,"A"),POS{I-1});

VAR1   = SUBSTR(VAR1,INDEX(VAR,"A")+1);

END;

END;

DROP VAR1;

RUN;

Fluorite | Level 6

## Re: SAS index function

Thank you all. I heard about rgx in sas but never tried; I will try the rgx.

Thanks.

Lapis Lazuli | Level 10

## Re: SAS index function

data test;

input var \$;

cards;

;

run;

data A_at;

set test;

do until (index(var,'A')=0);

position=index(var,"A");

var=substr(var,position+1);

A_at+position;

output;

end;

drop position;

run;

Super User

## Re: SAS index function

You can use the FIND() function. It will let you specify a starting position for the search.  If you want to search for any single character from a list of characters then use the FINDC() function.

Example:

data test;

input var \$;

put var / @ ;

position=find(var,"A");

do while (position);

put @position 'X' @ ;

position=find(var,"A",position+1);

end;

cards;

;

. X X  X

Fluorite | Level 6

## Re: SAS index function

Thank you all for great help.

Discussion stats
• 9 replies
• 1943 views
• 3 likes
• 8 in conversation