Help using Base SAS procedures

SAS index function

Accepted Solution Solved
Reply
Contributor
Posts: 51
Accepted Solution

SAS index function

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?


Accepted Solutions
Solution
‎07-13-2013 12:31 PM
Super User
Super User
Posts: 6,502

Re: SAS index function

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


All Replies
Regular Contributor
Posts: 213

Re: SAS index function

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

Respected Advisor
Posts: 3,777

Re: SAS index function

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;
Regular Contributor
Posts: 180

Re: SAS index function

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

Super User
Posts: 5,099

Re: SAS index function

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.

Super Contributor
Posts: 297

Re: SAS index function

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;

Contributor
Posts: 51

Re: SAS index function

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

Thanks.

Super Contributor
Posts: 275

Re: SAS index function

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;

Solution
‎07-13-2013 12:31 PM
Super User
Super User
Posts: 6,502

Re: SAS index function

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

Contributor
Posts: 51

Re: SAS index function

Thank you all for great help.

🔒 This topic is solved and locked.

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

Discussion stats
  • 9 replies
  • 439 views
  • 3 likes
  • 8 in conversation