COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### calculate nth occurance

• Email
• Follow

```Hi,
I have a character field separated by '-' I. I want to find the
character after 3rd occurance of "-" how can i do that

data abc;
input a \$16.;
cards;
a-b-c-def
abc-d-
ef-g-hi-
ab-cc-d-efgh-
run;

I have tried following but i am stuck on logic. What needs to be done
as length is different.
data temp;
set abc;
b=length(a);
d=count(a,'-'); /* Counting how many times - occured
e=indexc(a,"-");/*Find only occurence of -
c=scan(a,(b-d),'-');/*This is where i am stuck
run;

```
 0

See related articles to this posting

```On Feb 10, 9:38=A0am, saa <shounakadgaon...@gmail.com> wrote:
> Hi,
> I have a character field separated by '-' I. I want to find the
> character after 3rd occurance of "-" how can i do that
>
> data abc;
> input a \$16.;
> cards;
> a-b-c-def
> abc-d-
> ef-g-hi-
> ab-cc-d-efgh-
> run;
>
> I have tried following but i am stuck on logic. What needs to be done
> as length is different.
> data temp;
> set abc;
> b=3Dlength(a);
> d=3Dcount(a,'-'); /* Counting how many times - occured
> e=3Dindexc(a,"-");/*Find only occurence of -
> c=3Dscan(a,(b-d),'-');/*This is where i am stuck
> run;
>

Hi there,

try this for your question is " I have a character field separated by
'-' I. I want to find the > character after 3rd occurance of "-" how
can i do that"
I'm not sure if this is the only requirement you have...

data abc;
input val \$16.;
cards;
a-b-c-def
abc-d-
ef-g-hi-
ab-cc-d-efgh-
;
run;

data temp;
length c \$ 1;
set abc;
cnt=3Dcount(val,'-');
if cnt >=3D 3 then /* do this step when the count of  - is >=3D 3*/
do;
c=3Dsubstr(scan(val,4,'-'),1,1); /*scan for the 4th value after - and
then take 1 char using substr */
end;
run;

I hope this helps....
```
 0

```What if dash follow dash?

data test;
input a \$16.;
has3 = ifn(countc(a,'-') ge 3,1,0);
length fdash fdash2 \$1;
fdash = scan(a,3,'-');

call scan(a,3,p,l,'-','M');
fdash2 = substrn(a,p,1);

cards;
a-b-c-def
abc-d-
ef-g-hi-
ab-cc-d-efgh-
ab-cc--d--efgh-

run;

proc print;
run;
```
 0