Solved
Contributor
Posts: 20

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

Accepted Solutions
Solution
‎08-14-2015 08:37 AM
Respected Advisor
Posts: 3,867

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

Posted in reply to cmajorros

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;

All Replies
Super User
Posts: 9,855

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

Posted in reply to cmajorros

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.

Frequent Contributor
Posts: 79

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

Posted in reply to cmajorros

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;

Solution
‎08-14-2015 08:37 AM
Respected Advisor
Posts: 3,867

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

Posted in reply to cmajorros

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;

Contributor
Posts: 20

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

Posted in reply to data_null__

Thank you so much for your answer. it is very helpful.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 4 replies
• 508 views
• 0 likes
• 4 in conversation