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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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