|
I have a numeric variable that I need to have leading zeros on.
However, when I use the N format options it strips the decimal point. Original varaible X ranges from 0 to 99.99 (n8.2). examples: 82.17 1.07 0 Alter type X (n5.2). (produces the following: 08217 00107 00000 I need to have: 82.17 01.07 00000 For some reason I'm losing the decimal. What am I doing wrong? I could change these into strings since I'm uploading into an oracle table with a nvarchar(5) format but still need the leading zeros. Thanks in advance. David ===================== 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 |
|
Administrator
|
That is a bit curious. But I get the same thing you do. For the case where X = 0, do you really want the string to be '00000', or should it be '00.00', given that the other cases have two decimals? See below for some possible work-arounds. data list free / x (f8.2). begin data 82.17 1.07 0 end data. list. Output: x 82.17 1.07 .00 alter type x(n5.2). list. Output: x 08217 00107 00000 * As David observes, the decimal is not displayed. * But it is still there -- notice what happens when * we convert back to an F5.2 format. alter type x(f5.2). list. Output: x 82.17 1.07 .00 * Compute a copy of X and format it F5.2 . compute X2 = X. format X2 (f5.2). exe. * Now convert X2 to string, and then Left-Pad it with 0's . alter type x2 (a5). COMPUTE x2=CHAR.LPAD(ltrim(x2),5,'0'). list. Output: x X2 82.17 82.17 1.07 01.07 .00 00.00 if (X EQ 0) X2 = '00000'. /* if 0 really has to be '00000' . list. Output: x X2 82.17 82.17 1.07 01.07 .00 00000 HTH.
--
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/). |
|
In reply to this post by wsu_wright
Bruce,
Your solution: > alter type x2 (a5). > COMPUTE x2=CHAR.LPAD(ltrim(x2),5,'0'). works fine, it's a more efficient use of syntax than what I had as a work around (especially since I ultimately need it as a string): string x2(a5). compute x2=string(x,n4.2). /*inserts the leading zeros but loses the decimals. compute x2=concat(substr(x2,1,2),".",(substr(x2,3,4))). /* re-inserts the decimals. Still odd that the format n#.# strips the decimals, especially since it is documented as the preferred method in the SPSS syntax reference guide. Thanks again for your assistance. David On Mon, Aug 9, 2010 at 2:01 PM, Bruce Weaver wrote: > David Wright-6 wrote: >> >> I have a numeric variable that I need to have leading zeros on. >> However, when I use the N format options it strips the decimal point. >> >> >> Original varaible X ranges from 0 to 99.99 (n8.2). >> >> examples: >> 82.17 >> 1.07 >> 0 >> >> Alter type X (n5.2). (produces the following: >> 08217 >> 00107 >> 00000 >> >> I need to have: >> 82.17 >> 01.07 >> 00000 >> >> For some reason I'm losing the decimal. What am I doing wrong? I >> could >> change these into strings since I'm uploading into an oracle table >> with >> a nvarchar(5) format but still need the leading zeros. >> >> Thanks in advance. >> >> David >> >> > > That is a bit curious. But I get the same thing you do. For the case > where > X = 0, do you really want the string to be '00000', or should it be > '00.00', > given that the other cases have two decimals? See below for some > possible > work-arounds. > > data list free / x (f8.2). > begin data > 82.17 1.07 0 > end data. > list. > > Output: > x > 82.17 > 1.07 > .00 > > > alter type x(n5.2). > list. > > Output: > x > 08217 > 00107 > 00000 > > * As David observes, the decimal is not displayed. > * But it is still there -- notice what happens when > * we convert back to an F5.2 format. > > alter type x(f5.2). > list. > > Output: > x > 82.17 > 1.07 > .00 > > * Compute a copy of X and format it F5.2 . > > compute X2 = X. > format X2 (f5.2). > exe. > > * Now convert X2 to string, and then Left-Pad it with 0's . > > alter type x2 (a5). > COMPUTE x2=CHAR.LPAD(ltrim(x2),5,'0'). > list. > > Output: > x X2 > 82.17 82.17 > 1.07 01.07 > .00 00.00 > > > if (X EQ 0) X2 = '00000'. /* if 0 really has to be '00000' . > list. > > Output: > x X2 > 82.17 82.17 > 1.07 01.07 > .00 00000 > > HTH. > > > ----- > -- > Bruce Weaver > [hidden email] > http://sites.google.com/a/lakeheadu.ca/bweaver/ > > "When all else fails, RTFM." > > NOTE: My Hotmail account is not monitored regularly. > To send me an e-mail, please use the address shown above. > > -- > View this message in context: > http://spssx-discussion.1045642.n5.nabble.com/leading-zeros-to-strings-tp2269030p2276813.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 ===================== 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 |
|
I will be out of the office until
Thursday, August 19th. If you need immediate assistance, please call
812-856-5824. I will respond to your e-mail as soon as possible. Thank you, Shimon Sarraf Center for Postsecondary Research Indiana University at Bloomington |
|
In reply to this post by wsu_wright
Hi,
I am currently out on maternity leave and will not be back until 11/8/10. Please contact Keith Arnold ([hidden email]) for assistance.
Thank you.
Joanne Han Marketing Research Manager UCLA Extension
|
|
In reply to this post by wsu_wright
The documentation leaves a few things a bit too implicit, perhaps, but N format never shows decimals. The decimal part of a specification such as N5.2 is a statement of where an implicit decimal point is. So if you read this number under N5.2, 12345, it would be interpreted as 123.45. That's one reason why the output format assigned to an N variable is F. If you change a variable that is, say, F5.2 to N5.2, then the value 123.45 would be displayed as 12345. (This is a type of data format that is sometimes found in records really meant for computer input as it saves one position in the record.) N format, like others, does not have any effect on the variable value - it only affects how it is read and displayed. HTH, Jon Peck SPSS, an IBM Company [hidden email] 312-651-3435
Bruce, Your solution: > alter type x2 (a5). > COMPUTE x2=CHAR.LPAD(ltrim(x2),5,'0'). works fine, it's a more efficient use of syntax than what I had as a work around (especially since I ultimately need it as a string): string x2(a5). compute x2=string(x,n4.2). /*inserts the leading zeros but loses the decimals. compute x2=concat(substr(x2,1,2),".",(substr(x2,3,4))). /* re-inserts the decimals. Still odd that the format n#.# strips the decimals, especially since it is documented as the preferred method in the SPSS syntax reference guide. Thanks again for your assistance. David On Mon, Aug 9, 2010 at 2:01 PM, Bruce Weaver wrote: > David Wright-6 wrote: >> >> I have a numeric variable that I need to have leading zeros on. >> However, when I use the N format options it strips the decimal point. >> >> >> Original varaible X ranges from 0 to 99.99 (n8.2). >> >> examples: >> 82.17 >> 1.07 >> 0 >> >> Alter type X (n5.2). (produces the following: >> 08217 >> 00107 >> 00000 >> >> I need to have: >> 82.17 >> 01.07 >> 00000 >> >> For some reason I'm losing the decimal. What am I doing wrong? I >> could >> change these into strings since I'm uploading into an oracle table >> with >> a nvarchar(5) format but still need the leading zeros. >> >> Thanks in advance. >> >> David >> >> > > That is a bit curious. But I get the same thing you do. For the case > where > X = 0, do you really want the string to be '00000', or should it be > '00.00', > given that the other cases have two decimals? See below for some > possible > work-arounds. > > data list free / x (f8.2). > begin data > 82.17 1.07 0 > end data. > list. > > Output: > x > 82.17 > 1.07 > .00 > > > alter type x(n5.2). > list. > > Output: > x > 08217 > 00107 > 00000 > > * As David observes, the decimal is not displayed. > * But it is still there -- notice what happens when > * we convert back to an F5.2 format. > > alter type x(f5.2). > list. > > Output: > x > 82.17 > 1.07 > .00 > > * Compute a copy of X and format it F5.2 . > > compute X2 = X. > format X2 (f5.2). > exe. > > * Now convert X2 to string, and then Left-Pad it with 0's . > > alter type x2 (a5). > COMPUTE x2=CHAR.LPAD(ltrim(x2),5,'0'). > list. > > Output: > x X2 > 82.17 82.17 > 1.07 01.07 > .00 00.00 > > > if (X EQ 0) X2 = '00000'. /* if 0 really has to be '00000' . > list. > > Output: > x X2 > 82.17 82.17 > 1.07 01.07 > .00 00000 > > HTH. > > > ----- > -- > Bruce Weaver > [hidden email] > http://sites.google.com/a/lakeheadu.ca/bweaver/ > > "When all else fails, RTFM." > > NOTE: My Hotmail account is not monitored regularly. > To send me an e-mail, please use the address shown above. > > -- > View this message in context: > http://spssx-discussion.1045642.n5.nabble.com/leading-zeros-to-strings-tp2269030p2276813.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 ===================== 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 |
| Free forum by Nabble | Edit this page |
