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

Hello,

I have following codes:

data test;

input var $;

cards;

BACABBAD

;

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
Tom
Super User Tom
Super User

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;

BACABBAD

;

.BACABBAD

. X X  X

View solution in original post

9 REPLIES 9
AhmedAl_Attar
Rhodochrosite | Level 12

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

data_null__
Jade | Level 19

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. Smiley Sad

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;
BACABBAD
A
BBBB
AAAAA
;;;;
   run;
CTorres
Quartz | Level 8

Try this:

data test;

  input var $;

  cards;

BACABBAD

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

Astounding
PROC Star

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.

Scott_Mitchell
Quartz | Level 8

DATA HAVE;

ATTRIB VAR LENGTH = $8.;

INPUT VAR $;

DATALINES;

BACABADA

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;

abcd123
Fluorite | Level 6

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

Thanks.

slchen
Lapis Lazuli | Level 10

data test;

input var $;

cards;

BACABBAD

;

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;

Tom
Super User Tom
Super User

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;

BACABBAD

;

.BACABBAD

. X X  X

abcd123
Fluorite | Level 6

Thank you all for great help.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 9 replies
  • 1943 views
  • 3 likes
  • 8 in conversation