* For response to post: . * Date: Mon, 16 Feb 2004 14:26:10 -0000 . * Reply-To: "Smith, Anne" . * Sender: "SPSSX(r) Discussion" . * From: "Smith, Anne" . * Subject: Re: leading zero . * "While the N format is useful in certain situations it must be . * used with caution. It cannot cope with negative values . * (converting them to system missing) and it loses any decimal . * places." . * This is an old trick, common on systems that can't format with . * leading zeroes but can convert numbers to strings: . * To add leading zeroes to a minimum of N digits, add 10**N so . * there will be a leading 1; convert; and remove the leading '1'. . * This handles decimals as well as integers, and will add as many . * leading zeroes as necessary. . DATA LIST LIST/DCM_VAL (F13.6). BEGIN DATA. 12345 1234.5 123.45 12.345 1.2345 .12345 .012345 END DATA. STRING SP (A1). COMPUTE SP = ' '. STRING LZ1 (A12). COMPUTE LZ1 = SUBSTR(STRING(1E5+DCM_VAL,F13.6),2). * NOTE: Second argument to "SUBSTR", above, is 2, regardless of . * the number of digits before the decimal point. . LIST DCM_VAL SP LZ1. * In SPSS, if you over-specify the format, the leading '1' may . * display. To fix this, use LTRIM before SUBSTR: . STRING LZ2 LZ3 (A12). COMPUTE LZ2 = SUBSTR(STRING(1E5+DCM_VAL,F14.6),2). COMPUTE LZ3 = SUBSTR(LTRIM(STRING(1E5+DCM_VAL,F14.6)),2). LIST DCM_VAL SP LZ2 SP LZ3. * In direct form, this will NOT work for negative numbers. It . * displays them in "complementary" form, which has its uses but . * is not what one normally wants: . COMPUTE NEG_VAL = -1*DCM_VAL. FORMATS NEG_VAL(F13.6). STRING LZ4(A12). COMPUTE LZ4 = SUBSTR(STRING(1E5+NEG_VAL,F13.6),2). LIST NEG_VAL SP LZ4. * A somewhat awkward, but workable, fix is to add the sign . * explicitly: . COMPUTE MIX_VAL = DCM_VAL*((-1)**$CASENUM). FORMATS MIX_VAL(F13.6). STRING #SIGN(A1). STRING #FMT(A12). STRING LZ5(A12). COMPUTE #SIGN = ' '. IF (MIX_VAL < 0) #SIGN = '-'. COMPUTE #FMT = SUBSTR(LTRIM(STRING(1E5+ABS(MIX_VAL),F13.6)),2) COMPUTE LZ5=CONCAT(#SIGN,#FMT). LIST MIX_VAL SP LZ5. * Finally, as a coding *tour de force*, the above can be done . * with a single "COMPUTE" statement: . STRING LZ6 (A12). COMPUTE LZ6 = CONCAT(SUBSTR(' -',1+(MIX_VAL<0),1) ,SUBSTR(LTRIM(STRING(1E5+ABS(MIX_VAL),F13.6)),2)). LIST MIX_VAL SP LZ6.