Calcite | Level 5

## How can I find the position of a character in a text.

Hi All

Is it possible to find the position of a character in a text like these examples

 Text Need_Position_of_4 Result 123412344444 3 9 1112222223400120044444 2 18

For the first example I need 3th 4 and the second one I need the second 4.

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: How can I find the position of a character in a text.

Using FINDC you can search "Need Position of 4" number of times as follows.

data find;
infile cards expandtabs;

input s :\$30. o r;
p=0;

do _n_ = 1 to o until(p eq 0);
p = findc(s,'4',p+1);
end;

drop startpos;
cards;
123412344444   3  9
1112222223400120044444  2  18
1112222223400120044444  10 .
;;;;
run;
proc print;

run;

4 REPLIES 4
Diamond | Level 26

## Re: How can I find the position of a character in a text.

What is logic, i.e. why is it to find the first, or the second?  Post some test data in the form of a datastep, and required output, with an explanation of what you require.  If you want search functions (and you can find these out from the help documentation or google them): find, findw, findc, index etc.

Quartz | Level 8

## Re: How can I find the position of a character in a text.

data Have;

input Text : \$ 20.;

cards;

123412344444

1112222223400120044444

;

data Want;

set Have;

if _n_=1 then do;

Need_Position_of_4 = 3;

Result = findc(Text,'4',findc(Text,'4',index(Text,'4')+1)+1);

end;

else do;

Need_Position_of_4 = 2;

Result = findc(Text,'4',index(Text,'4')+1);

end;

run;

proc print ;run;

## Re: How can I find the position of a character in a text.

Using FINDC you can search "Need Position of 4" number of times as follows.

data find;
infile cards expandtabs;

input s :\$30. o r;
p=0;

do _n_ = 1 to o until(p eq 0);
p = findc(s,'4',p+1);
end;

drop startpos;
cards;
123412344444   3  9
1112222223400120044444  2  18
1112222223400120044444  10 .
;;;;
run;
proc print;

run;

Calcite | Level 5