Trailing negative signs

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

Trailing negative signs

Dan Meir
Hello listers,

I have been looking for a solution to convert (string) data that includes
trailing negative signs into regular numeric data.  REPLACE is not an
option as I am using version 13.0.  Is there a way to use RTRIM?  It
hasn't  been useful so far.

Thanks everyone.

Dan
Reply | Threaded
Open this post in threaded view
|

Re: Trailing negative signs

Norton, John
Hi Dan,

Assuming that your data look like the following, you can use an INDEX(), NUMBER() and a SUBSTR() function:

Assume your data look like this (and is called "str_var"):
1-
12-
123-
1234-
12345-

You can extract the numeric information by using the following COMPUTE statement:

COMPUTE num_var = NUMBER(SUBSTR(str_var,1,INDEX(str_var,"-")-1),F8).

HTH

John Norton
SPSS Inc.



-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Dan Meir
Sent: Monday, July 02, 2007 8:18 AM
To: [hidden email]
Subject: Trailing negative signs

Hello listers,

I have been looking for a solution to convert (string) data that includes
trailing negative signs into regular numeric data.  REPLACE is not an
option as I am using version 13.0.  Is there a way to use RTRIM?  It
hasn't  been useful so far.

Thanks everyone.

Dan
Reply | Threaded
Open this post in threaded view
|

Re: Trailing negative signs

Albert-Jan Roskam
Hi Dan,

You were right, RTRIM is an option:

* your data.
data list free / x (a8).
begin data
1-
12-
123-
1234-
12345-
end data.

* the new variable.
compute y = number(rtrim(rtrim (x,' '),'-'),F6.0).
exe.


Cheers!!
Albert-Jan

--- "Norton, John" <[hidden email]> wrote:

> Hi Dan,
>
> Assuming that your data look like the following, you
> can use an INDEX(), NUMBER() and a SUBSTR()
> function:
>
> Assume your data look like this (and is called
> "str_var"):
> 1-
> 12-
> 123-
> 1234-
> 12345-
>
> You can extract the numeric information by using the
> following COMPUTE statement:
>
> COMPUTE num_var =
> NUMBER(SUBSTR(str_var,1,INDEX(str_var,"-")-1),F8).
>
> HTH
>
> John Norton
> SPSS Inc.
>
>
>
> -----Original Message-----
> From: SPSSX(r) Discussion
> [mailto:[hidden email]] On Behalf Of Dan
> Meir
> Sent: Monday, July 02, 2007 8:18 AM
> To: [hidden email]
> Subject: Trailing negative signs
>
> Hello listers,
>
> I have been looking for a solution to convert
> (string) data that includes
> trailing negative signs into regular numeric data.
> REPLACE is not an
> option as I am using version 13.0.  Is there a way
> to use RTRIM?  It
> hasn't  been useful so far.
>
> Thanks everyone.
>
> Dan
>


Cheers!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Did you know that 87.166253% of all statistics claim a precision of results that is not justified by the method employed? [HELMUT RICHTER]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



____________________________________________________________________________________
Pinpoint customers who are looking for what you sell.
http://searchmarketing.yahoo.com/
Reply | Threaded
Open this post in threaded view
|

Re: Trailing negative signs

Richard Ristow
In reply to this post by Norton, John
At 10:07 AM 7/2/2007, Norton, John wrote:

>Assuming that your data look like the following, you can use an
>INDEX(), NUMBER() and a SUBSTR() function:
>
>COMPUTE num_var = NUMBER(SUBSTR(str_var,1,INDEX(str_var,"-")-1),F8).

That'll work (and thanks for the explanation) as long as *all* the
string values have trailing minus signs. Trailing minus signs are often
used for what you'd think: to indicate a number is negative. That means
that,
a.) A lot of numbers won't have them.
b.) Those that do should be given negative numeric values.

Try the following. SPSS 15 draft output (WRR:not saved separately).
I've used scratch variables to break the long COMPUTE into its
constituent parts.

|-----------------------------|---------------------------|
|Output Created               |02-JUL-2007 12:39:52       |
|-----------------------------|---------------------------|
[INPUT]

str_var

1-
12
123-
1234
12345-

Number of cases read:  5    Number of cases listed:  5


DATASET COPY     OUTPUT.
DATASET ACTIVATE OUTPUT WINDOW=FRONT.

STRING  #DIGITS (A8) /* Digits only, of the number   */.
COMPUTE #SGN.IDX =   /* Location of the sign, if any */
                  INDEX(str_var,"-")

DO IF   #SGN.IDX GT 0.
.  COMPUTE #DIGITS  = SUBSTR(str_var,
                             1,
                             #SGN.IDX-1).
ELSE.
.  COMPUTE #DIGITS  = str_var.
END IF.

COMPUTE num_var = NUMBER(#DIGITS,F8).
IF #SGN.IDX GT 0
         num_var = -1*num_var.
FORMATS num_var (F8).
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |02-JUL-2007 12:39:54       |
|-----------------------------|---------------------------|
[OUTPUT]

str_var   num_var

1-             -1
12             12
123-         -123
1234         1234
12345-     -12345

Number of cases read:  5    Number of cases listed:  5