Converting numeric to string

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

Converting numeric to string

Jignesh Sutar-3
Hi,
 
I'm trying to convert a numeric field to a string format, latter to be concatenated with other text fields. I want to convert the numeric field to its simplest form, see the variable "output" the desired output I wish to achieve. I've mocked up a demo data and a couple of attempts at achieving this.
 
DATA LIST /value (F6.2) Output(A5).
BEGIN DATA
0.00  0
0.10  0.1
0.01  0.01
1.00  1
1.01  1.01
1.12  1.12
1.123 1.123
END DATA.
exe.
 
string Test1(A25).
string Test2(A25).
compute Test1=concat("Value=(",ltrim(string(value,F8.0)),")").
compute Test2=concat("Value=(",ltrim(string(value,F8.3)),")").
exe.
 
Many thanks in advance.
 
Thanks,
Jignesh
Reply | Threaded
Open this post in threaded view
|

Automatic reply: Converting numeric to string

Nan Zhou



I am out of the office and will return on Monday, July 9th.  I have limited access to email and I will respond to your email as soon as possible.

 

Thank you,

 

Nan

 

 

Reply | Threaded
Open this post in threaded view
|

Re: Converting numeric to string

Tom1234
In reply to this post by Jignesh Sutar-3
If you have the programability extension you could use python to change the variable
See this thread on the IBM Developer site:

Changing variable type via Python


"Lo there do I see my father. Lo there do I see my mother and my sisters and my brothers. Lo there do I see the line of my people, back to the beginning. Lo, they do call to me, they bid me take my place among them, in the Halls of Valhalla, where the brave may live...forever."
Reply | Threaded
Open this post in threaded view
|

Re: Converting numeric to string

Bruce Weaver
Administrator
In reply to this post by Jignesh Sutar-3
There's probably some more elegant way to to it, but this works.  (I assume you don't have negative values to worry about, given that none were included in your sample data.)

DATA LIST /value (F6.2) Output(A5).
BEGIN DATA
0.00  0
0.10  0.1
0.01  0.01
1.00  1
1.01  1.01
1.12  1.12
1.123 1.123
END DATA.

string test (a8).

* Deal with the special cases of 0 and 1 separately.
do if value EQ 0.
- compute test = "0".
else if value EQ 1.
- compute test = "1".
else.
- compute test = ltrim(rtrim(string(value,F8.3),"0")).
end if.
* Now tack on a leading 0 where needed.
if (value LT 1) and (value GT 0) test = concat("0",test).
list.

OUTPUT:

  value Output test

    .00 0      0
    .10 0.1    0.1
    .01 0.01   0.01
   1.00 1      1
   1.01 1.01   1.01
   1.12 1.12   1.12
   1.12 1.123  1.123

Number of cases read:  7    Number of cases listed:  7

HTH.


Jignesh Sutar wrote
Hi,

I'm trying to convert a numeric field to a string format, latter to be
concatenated with other text fields. I want to convert the numeric field to
its simplest form, see the variable "output" the desired output I wish to
achieve. I've mocked up a demo data and a couple of attempts at achieving
this.

DATA LIST /value (F6.2) Output(A5).
BEGIN DATA
0.00  0
0.10  0.1
0.01  0.01
1.00  1
1.01  1.01
1.12  1.12
1.123 1.123
END DATA.
exe.

string Test1(A25).
string Test2(A25).
compute Test1=concat("Value=(",ltrim(string(value,F8.0)),")").
compute Test2=concat("Value=(",ltrim(string(value,F8.3)),")").
exe.

Many thanks in advance.

Thanks,
Jignesh
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Converting numeric to string

David Marso
Administrator
In reply to this post by Tom1234
Tom1234 wrote
If you have the programability extension you could use python to change the variable
See this thread on the IBM Developer site:

Changing variable type via Python
---
Python????
"We don'ta need no stinkin Python ;-)" : The user of Sierra Madre ?
data list free/ a.
begin data
0.0
1.0
1.1
0.1
1.123
end data.
list.
STRING sa (A5).
COMPUTE sa=LTRIM(SUBSTR(STRING(a,F5.3),1,RINDEX(STRING(a,F5.3),"123456789",1))).
IF sa=" " sa="0".

       A SA

 .000000 0
1.000000 1
1.100000 1.1
 .100000 .1
1.123000 1.123

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

Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Converting numeric to string

Art Kendall
In reply to this post by Jignesh Sutar-3
Here is a third way.  This approach shows in a step by step tutorial fashion how to do it.
DATA LIST /value (F6.2) Output(A5).
BEGIN DATA
0.00  0
0.10  0.1
0.01  0.01
1.00  1
1.01  1.01
1.12  1.12
1.123 1.123
END DATA.
*exe.
string simple1 to simple4(a5).
compute simple1 = string(value,f5.3).
compute simple2 = rtrim(rtrim(simple1),"0").
compute simple3 = rtrim(rtrim(simple2),".").
do if substr(simple3,1,3) eq "  ".
   compute simple4 = "0".
else if substr(simple3,1,2) eq " .".
   compute simple4 = concat("0",ltrim(simple3)).
ELSE.
   compute simple4 = simple3.
end if.
string Test1(A25).
compute Test1=concat("Value=(",simple4,")").
list.


BTW just curious why would you do this?
Art Kendall
Social Research Consultants
On 6/27/2012 4:06 AM, Jignesh Sutar wrote:
Hi,
 
I'm trying to convert a numeric field to a string format, latter to be concatenated with other text fields. I want to convert the numeric field to its simplest form, see the variable "output" the desired output I wish to achieve. I've mocked up a demo data and a couple of attempts at achieving this.
 
DATA LIST /value (F6.2) Output(A5).
BEGIN DATA
0.00  0
0.10  0.1
0.01  0.01
1.00  1
1.01  1.01
1.12  1.12
1.123 1.123
END DATA.
exe.
 
string Test1(A25).
string Test2(A25).
compute Test1=concat("Value=(",ltrim(string(value,F8.0)),")").
compute Test2=concat("Value=(",ltrim(string(value,F8.3)),")").
exe.
 
Many thanks in advance.
 
Thanks,
Jignesh


===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: Converting numeric to string

David Marso
Administrator

Ah come on Art you're gonna scare people ;-)))
Simplest way is to slam it into a string with F format and max required digits.
Find rightmost non-zero number with RINDEX.
blast everything to the right of that.
if nothing is left set result to '0'
-- see the 3 liner I posted above---
---
Art Kendall wrote
Here is a third way. 
        This approach shows in a step by step tutorial fashion how to do
        it.
      DATA LIST /value (F6.2) Output(A5).
        BEGIN DATA
        0.00  0
        0.10  0.1
        0.01  0.01
        1.00  1
        1.01  1.01
        1.12  1.12
        1.123 1.123
        END DATA.
        *exe.
        string simple1 to simple4(a5).
        compute simple1 = string(value,f5.3).
        compute simple2 = rtrim(rtrim(simple1),"0").
        compute simple3 = rtrim(rtrim(simple2),".").
        do if substr(simple3,1,3) eq "  ".
           compute simple4 = "0".
        else if substr(simple3,1,2) eq " .".
           compute simple4 = concat("0",ltrim(simple3)).
        ELSE.
           compute simple4 = simple3.
        end if.
        string Test1(A25).
        compute Test1=concat("Value=(",simple4,")").
        list.
     
      BTW just curious why would you do this?
      Art Kendall
Social Research Consultants
      On 6/27/2012 4:06 AM, Jignesh Sutar wrote:
   
   
     
        Hi,
         
        I'm trying to convert a numeric field to a string format,
          latter to be concatenated with other text fields. I want to
          convert the numeric field to its simplest form, see the
          variable "output" the desired output I wish to achieve. I've
          mocked up a demo data and a couple of attempts at achieving
          this.
         
        DATA LIST /value (F6.2) Output(A5).
          BEGIN DATA
          0.00  0
          0.10  0.1
          0.01  0.01
          1.00  1
          1.01  1.01
          1.12  1.12
          1.123 1.123
          END DATA.
          exe.
         
        string Test1(A25).
          string Test2(A25).
          compute Test1=concat("Value=(",ltrim(string(value,F8.0)),")").
          compute Test2=concat("Value=(",ltrim(string(value,F8.3)),")").
          exe.
       
         
       
          Many thanks in advance.
         
        Thanks,
        Jignesh
     
   
   
   
 


=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Converting numeric to string

Jignesh Sutar
I've had to include the 4th line to treat ".10" to be displayed as "0.1". The reason behind doing is this because I am creating a customised datamap/dictionary and mapping values to their value labels.
 
data list free/ a.
begin data
0.0
1.0
1.1
0.1
1.123
end data.
list.
STRING sa (A5).
COMPUTE
sa=LTRIM(SUBSTR(STRING(a,F5.3),1,RINDEX(STRING(a,F5.3),"123456789",1))).
IF sa=" " sa="0".
IF (substr(sa,1,1)=".") sa=concat("0",sa).

 

 
On 27 June 2012 17:46, David Marso <[hidden email]> wrote:
Ah come on Art you're gonna scare people ;-)))
Simplest way is to slam it into a string with F format and max required
digits.
Find rightmost non-zero number with RINDEX.
blast everything to the right of that.
if nothing is left set result to '0'
-- see the 3 liner I posted above---
---

Art Kendall wrote
>
> Here is a third way.&nbsp;
>         This approach shows in a step by step tutorial fashion how to do
>         it.
>       DATA LIST /value (F6.2) Output(A5).
>         BEGIN DATA
>         0.00&nbsp; 0
>         0.10&nbsp; 0.1
>         0.01&nbsp; 0.01
>         1.00&nbsp; 1
>         1.01&nbsp; 1.01
>         1.12&nbsp; 1.12
>         1.123 1.123
>         END DATA.
>         *exe.
>         string simple1 to simple4(a5).
>         compute simple1 = string(value,f5.3).
>         compute simple2 = rtrim(rtrim(simple1),"0").
>         compute simple3 = rtrim(rtrim(simple2),".").
>         do if substr(simple3,1,3) eq "&nbsp; ".
>         &nbsp;&nbsp; compute simple4 = "0".
>         else if substr(simple3,1,2) eq " .".
>         &nbsp;&nbsp; compute simple4 = concat("0",ltrim(simple3)).
>         ELSE.
>         &nbsp;&nbsp; compute simple4 = simple3.
>         end if.
>         string Test1(A25).
>         compute Test1=concat("Value=(",simple4,")").
>         list.
>
>       BTW just curious why would you do this?
>       Art Kendall
> Social Research Consultants
>       On 6/27/2012 4:06 AM, Jignesh Sutar wrote:
>
>
>
>         Hi,
>         &nbsp;
>         I'm trying to convert a numeric field to a string format,
>           latter to be concatenated with other text fields. I want to
>           convert the numeric field to its simplest form, see the
>           variable "output" the desired output I wish to achieve. I've
>           mocked up a demo data and a couple of attempts at achieving
>           this.
>         &nbsp;
>         DATA LIST /value (F6.2) Output(A5).
>           BEGIN DATA
>           0.00&nbsp; 0
>           0.10&nbsp; 0.1
>           0.01&nbsp; 0.01
>           1.00&nbsp; 1
>           1.01&nbsp; 1.01
>           1.12&nbsp; 1.12
>           1.123 1.123
>           END DATA.
>           exe.
>         &nbsp;
>         string Test1(A25).
>           string Test2(A25).
>           compute Test1=concat("Value=(",ltrim(string(value,F8.0)),")").
>           compute Test2=concat("Value=(",ltrim(string(value,F8.3)),")").
>           exe.
>
>         &nbsp;
>
>           Many thanks in advance.
>         &nbsp;
>         Thanks,
>         Jignesh
>
>
>
>
>
>
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> LISTSERV@.UGA (not to SPSSX-L), with no body text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the command
> INFO REFCARD
>


--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Converting-numeric-to-string-tp5713822p5713848.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD