BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cmajorros
Calcite | Level 5

Hi All

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

TextNeed_Position_of_4Result
12341234444439
1112222223400120044444218

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

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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;

Capture.PNG

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

MadhuKorni
Quartz | Level 8

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;

data_null__
Jade | Level 19

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;

Capture.PNG
cmajorros
Calcite | Level 5

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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