Extract Number Syntax

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Extract Number Syntax

Brian Moore-3
Hi all-

Raynald has provided (on his website) a routine to extract numbers from
strings.

COMPUTE #nb=0.
LOOP cnt=1 TO LENGTH(RTRIM(qq342)).
COMPUTE #c=RANGE(SUBSTR(qq342,cnt,1),'0','9').
IF #c=1 #nb=#nb*10+NUMBER(SUBSTR(qq342,cnt,1),F1).
END LOOP.
COMPUTE QQ342_n=#nb.
EXE.

I'm wondering how it might be modified to also include the decimal point.
Such that strings containing:
3.49
34.9
349

would hold their relative values in the QQ342_n variable.

Thanks,
Brian
Reply | Threaded
Open this post in threaded view
|

Re: Extract Number Syntax

Jason Burke
Hi Brian,

Replace the following command:

COMPUTE #c=RANGE(SUBSTR(qq342,cnt,1),'0','9').

with:

COMPUTE #c=ANY(SUBSTR(qq342,cnt,1),'0','1','2','3','4','5','6','7','8','9','.').

Cheers,


Jason

On 2/22/07, Brian Moore <[hidden email]> wrote:

> Hi all-
>
> Raynald has provided (on his website) a routine to extract numbers from
> strings.
>
> COMPUTE #nb=0.
> LOOP cnt=1 TO LENGTH(RTRIM(qq342)).
> COMPUTE #c=RANGE(SUBSTR(qq342,cnt,1),'0','9').
> IF #c=1 #nb=#nb*10+NUMBER(SUBSTR(qq342,cnt,1),F1).
> END LOOP.
> COMPUTE QQ342_n=#nb.
> EXE.
>
> I'm wondering how it might be modified to also include the decimal point.
> Such that strings containing:
> 3.49
> 34.9
> 349
>
> would hold their relative values in the QQ342_n variable.
>
> Thanks,
> Brian
>
Reply | Threaded
Open this post in threaded view
|

Re: Extract Number Syntax

Art Kendall
In reply to this post by Brian Moore-3
If there is no extraneous data in the string
numeric qq342_n (f10.5).
compute qq342_n = string(qq342,f10.5).

Art Kendall
Social Research Consultants


Brian Moore wrote:

> Hi all-
>
> Raynald has provided (on his website) a routine to extract numbers from
> strings.
>
> COMPUTE #nb=0.
> LOOP cnt=1 TO LENGTH(RTRIM(qq342)).
> COMPUTE #c=RANGE(SUBSTR(qq342,cnt,1),'0','9').
> IF #c=1 #nb=#nb*10+NUMBER(SUBSTR(qq342,cnt,1),F1).
> END LOOP.
> COMPUTE QQ342_n=#nb.
> EXE.
>
> I'm wondering how it might be modified to also include the decimal point.
> Such that strings containing:
> 3.49
> 34.9
> 349
>
> would hold their relative values in the QQ342_n variable.
>
> Thanks,
> Brian
>
>
>
Art Kendall
Social Research Consultants