lpad function

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

lpad function

Keval Khichadia
Hi,
I have a numeric variable call (F11.0) which I need to be a string variable of length 4. If the call number is not 4 digits it needs to be padded with 0's. (1 should be 0001).
I tried the syntax which I am sure is not the most effiecient one, but even then I am not getting the right results:
formats call (f4.0).
string newcall (a4).
compute newcall = string(call, f4.0).
string newcall2 (a4).
compute newcall2 = lpad(newcall,4, '0').
delete variables call newcall.
rename variables newcall2 = call.
I appreciate any help.
Thanks,
Keval

=====================
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
Reply | Threaded
Open this post in threaded view
|

Re: lpad function

ViAnn Beadle
This should do it assuming that no values actually exceed 4 digits:
String newcall(a4).
Compute newcall=string(call,n4.0).

Is there some reason why you want to replace the original variable?

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Keval Khichadia
Sent: Monday, June 23, 2008 6:05 PM
To: [hidden email]
Subject: lpad function

Hi,
I have a numeric variable call (F11.0) which I need to be a string variable
of length 4. If the call number is not 4 digits it needs to be padded with
0's. (1 should be 0001).
I tried the syntax which I am sure is not the most effiecient one, but even
then I am not getting the right results:
formats call (f4.0).
string newcall (a4).
compute newcall = string(call, f4.0).
string newcall2 (a4).
compute newcall2 = lpad(newcall,4, '0').
delete variables call newcall.
rename variables newcall2 = call.
I appreciate any help.
Thanks,
Keval

=====================
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
Reply | Threaded
Open this post in threaded view
|

Re: lpad function

Richard Ristow
In reply to this post by Keval Khichadia
At 08:04 PM 6/23/2008, Keval Khichadia wrote:

>I have a numeric variable call (F11.0) which I need to be a string
>variable of length 4. If the call number is not 4 digits it needs to
>be padded with [leading] 0's.

ViAnn Beadle's suggestion, to use N4 rather than F4 to convert from
numeric to string, is much the simplest.

But for the code you posted, though I'm not testing, I think that if
you replace

>compute newcall2 = lpad(newcall,4, '0').

compute newcall2 = lpad((LTRIM(newcall),4, '0').

it will work.

=====================
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