From: Poohbear on
Hi All,

I have to use the scan function to get the '-4th' value for a string,
but sometimes the '-3rd' field of that string is a missing value. For
example, I have a string like this:

abc, def, ght, hugdy, bshdgeyrt, 234, 5476, 037199, , 03/12/2006,
01/07/2007

I wanna get the value '037199'. Since I have too many such strings, and
the number of fileds before the field '037199' are not same for each
string, so I have to use scan function to scan it from the right to
left. The problem is, not all strings have missing value for the '-3rd'
field....

Here is my statement:

id=scan(txt,-4,',');

But it seems it always give me the value of 5476 for the strings have
missing value for '-3rd' field. Anyone has a clue of how to avoid this?


Thanks a lot,
Pooh

From: dramage on
Some honey for poohbear:

According to the documentation for the SCAN function: "If there are two
or more contiguous delimiters, SCAN treats them as one".

My solution forces consecutive delimiters to be separated by a space.
This causes the SCAN function to treat them as non-consecutive.

=========================================================
data test;

txt='abc,def,ght,hugdy,bshdgeyrt,234,5476,037199,,03/12/2006,01/07/2007';
txtnew=tranwrd(txt,',,',', ,');

id=scan(txt,-4,',');
idnew=scan(txtnew,-4,',');
run;

proc print noobs;
var id idnew txt txtnew;
run;
=============================================================


Poohbear wrote:
> Hi All,
>
> I have to use the scan function to get the '-4th' value for a string,
> but sometimes the '-3rd' field of that string is a missing value. For
> example, I have a string like this:
>
> abc, def, ght, hugdy, bshdgeyrt, 234, 5476, 037199, , 03/12/2006,
> 01/07/2007
>
> I wanna get the value '037199'. Since I have too many such strings, and
> the number of fileds before the field '037199' are not same for each
> string, so I have to use scan function to scan it from the right to
> left. The problem is, not all strings have missing value for the '-3rd'
> field....
>
> Here is my statement:
>
> id=scan(txt,-4,',');
>
> But it seems it always give me the value of 5476 for the strings have
> missing value for '-3rd' field. Anyone has a clue of how to avoid this?
>
>
> Thanks a lot,
> Pooh

From: Poohbear on
Thanks a lot!!!

Pooh

dramage wrote:
> Some honey for poohbear:
>
> According to the documentation for the SCAN function: "If there are two
> or more contiguous delimiters, SCAN treats them as one".
>
> My solution forces consecutive delimiters to be separated by a space.
> This causes the SCAN function to treat them as non-consecutive.
>
> =========================================================
> data test;
>
> txt='abc,def,ght,hugdy,bshdgeyrt,234,5476,037199,,03/12/2006,01/07/2007';
> txtnew=tranwrd(txt,',,',', ,');
>
> id=scan(txt,-4,',');
> idnew=scan(txtnew,-4,',');
> run;
>
> proc print noobs;
> var id idnew txt txtnew;
> run;
> =============================================================
>
>
> Poohbear wrote:
> > Hi All,
> >
> > I have to use the scan function to get the '-4th' value for a string,
> > but sometimes the '-3rd' field of that string is a missing value. For
> > example, I have a string like this:
> >
> > abc, def, ght, hugdy, bshdgeyrt, 234, 5476, 037199, , 03/12/2006,
> > 01/07/2007
> >
> > I wanna get the value '037199'. Since I have too many such strings, and
> > the number of fileds before the field '037199' are not same for each
> > string, so I have to use scan function to scan it from the right to
> > left. The problem is, not all strings have missing value for the '-3rd'
> > field....
> >
> > Here is my statement:
> >
> > id=scan(txt,-4,',');
> >
> > But it seems it always give me the value of 5476 for the strings have
> > missing value for '-3rd' field. Anyone has a clue of how to avoid this?
> >
> >
> > Thanks a lot,
> > Pooh