Date: Thu, 8 Jul 1999 21:43:12 +0200
Reply-To: Rolf Kjoeller <rolf.kjoeller@GET2NET.DK>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: Rolf Kjoeller <rolf.kjoeller@GET2NET.DK>
Subject: Sv: Shuffling characters within a string and padding WAS Why
won't this work?
Content-Type: text/plain; charset="iso-8859-1"
Hi, here is another way to do the padding with zeros:
COMPUTE numbers=RPAD(RTRIM(LTRIM(numbers)),5,"0") .
----- Oprindelig meddelelse -----
Fra: Frank Milthorpe <MILTHORF@TRANSPORT.NSW.GOV.AU>
Sendt: 8. juli 1999 06:43
Emne: Shuffling characters within a string and padding WAS Why won't this work?
> In reply to
> Date: Thu, 8 Jul 1999 09:29:47 +930CST
> From: Stan Bordeaux <nisb@NISU.FLINDERS.EDU.AU>
> Subject: Why won't this work?
> >I have a number of files with 26 string variables say (v1 to v26) all with an
> A6 format, with over 300,000 cases in any one file.
> >Strings within each variable can be any of 3, 4, or 5 characters. I need to
> add zeros ie '0' to the end of any string which is less than 5 characters ie if
> the string is 123 the result should be 12300 and 1234 should be 12340.
> >Simple hey? Well, blow me down with a force 6 hurricane. I can't get
> anything to work. The following syntax, I'm assured from my manual,
> should do the trick: I've tried these two with no joy
> Generally (possibly always) your strings will be of length 6. I am yet to determine under what circumstances SPSS will read in srings which are not maximum (defined) lengeth.
> I suspect that if you do a
> COMPUTE L1 =LENGTH(V1)
> it will report a length of six.
> Here is a quick framework.
> The theory is to check for 5 blanks on the right, if so shift across.
> Otherwise check for 4 blanks and shift across.
> and etc.
> * N.B.
> * ---Untested code ---.
> DO IF (substr(v1,1,5) = ' ').
> . compute v1 = concat(substr(v1,1,1), '00000').
> . ELSE IF (substr(v1,1,4) = ' ').
> and etc.
> END IF.
> * The above code assumes that there are not trailing spaces in the original data.
> * If there is they can be simplied substitued by replacing a blank with a 0.
> To save lots of typing this should be placed inside a DO REPEAT LOOP.
> DO REPEAT VAR = V1 TO V26.
> * code in here.
> * use var instead of my v1 from above.
> END REPEAT PRINT.
> * You can drop the PRINT once you are convinced the loop is doing what you want.
> I hope this helps.
> Frank Milthorpe ...
> Frank Milthorpe
> Transport Modelling Manager
> Transport Data Centre - NSW Department of Transport
> Sydney - AUSTRALIA
> Ph: +61 2 9268 2937
> Fax: +61 2 9268 2853
> Email: email@example.com