calculate nth occurance

  • 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;

please help me out
0
Reply shounakadgaonkar (6) 2/10/2011 2:38:25 PM

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;
>
> please help me out

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
Reply Deepika 2/10/2011 2:54:21 PM


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
Reply data 2/10/2011 4:08:47 PM

2 Replies
357 Views

(page loaded in 0.163 seconds)

Similiar Articles:







7/21/2012 10:28:40 PM


Reply: