lpad on concatenated compute

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

lpad on concatenated compute

wsu_wright
I need to insert a leading space on a concatenated string, listed below
is my current method but I would like to get this as one line of syntax
rather than two.

string newvar (a50).
compute newvar=concat(vara to varz).
compute newvar=lpad(newvar,51).


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

Re: lpad on concatenated compute

David Marso
Administrator
David,
At first I thought that CONCAT(vara TO varz) would go south...
Tried it and surprise!  It worked.
Now, you CAN'T reallocate string length using LPAD.
Maybe ALTER TYPE? But best to preallocate ahead of time.
One liner:
string XX (A51).
COMPUTE XX=CONCAT(" ",a1 TO a50).
------
data list / a1 to a50 (50A1).
begin data
gdsafdhasfghafasfdghafhafdhgasfgdfagdfasgdfasdgasf
dgafdhgafdhgafdafhgafsgafdgafghdafgdfagdfagdfagsfd
afdgafdgafdgafgdfagdffgadfhafdhgafdhgafgdfaghdfagh
dfgasdfgasfdafdagdfaghdfasghdfhadfsgfgshgfhsgfgshj
fgshjfghsgfsgfhjsgjfgsfgsgfhsgdgfsgfjhsghfgshdfghj
sgfhjsfghjsggdhaghdgashgfhjghgfsgfjsgfjgjgjhfgsdgf
end data.
string XX (A51).
COMPUTE XX=CONCAT(" ",a1 TO a50).
LIST.
HTH, David
--

David Wright-6 wrote
I need to insert a leading space on a concatenated string, listed below
is my current method but I would like to get this as one line of syntax
rather than two.

string newvar (a50).
compute newvar=concat(vara to varz).
compute newvar=lpad(newvar,51).


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
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: lpad on concatenated compute

Jon K Peck
In reply to this post by wsu_wright
You can just write this

string newvar (a50).
compute newvar = lpad(concat(vara to varz), 50).

Additionally, since the new variable has length 50, using 51 above would trim off the rightmost character.

Jon Peck (no "h")
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        David Wright <[hidden email]>
To:        [hidden email]
Date:        10/28/2011 05:58 AM
Subject:        [SPSSX-L] lpad on concatenated compute
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




I need to insert a leading space on a concatenated string, listed below
is my current method but I would like to get this as one line of syntax
rather than two.

string newvar (a50).
compute newvar=concat(vara to varz).
compute newvar=lpad(newvar,51).


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


Reply | Threaded
Open this post in threaded view
|

Re: lpad on concatenated compute

Jon K Peck
In reply to this post by David Marso
There is a subtle difference between using LPAD and just concatenating a blank on the front with CONCAT.  The latter will always add the blank and then truncate (or pad) the string on the right to fit the target length, while the former will add just enough padding to meet the specified length, which might result in no leading blank, and right pad as needed.

Note: the old functions such as LPAD are strictly byte oriented and can give different results in Unicode and code page mode if there are any extended characters in the expression (e.g., accented roman, Asian, Russian, Hebrew, Greek, ...).

The CHAR.* functions such as CHAR.LPAD were introduced back in SPSS 16.  These are character oriented.  That is almost always what you want, and they give the same result in Unicode and code page modes.  The CHAR.* functions are also friendlier with respect to blank padding on the right in expressions.  We strongly recommend that users switch and start using these already not so new functions.

Jon Peck (no "h")
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        David Marso <[hidden email]>
To:        [hidden email]
Date:        10/28/2011 06:34 AM
Subject:        Re: [SPSSX-L] lpad on concatenated compute
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




David,
At first I thought that CONCAT(vara TO varz) would go south...
Tried it and surprise!  It worked.
Now, you CAN'T reallocate string length using LPAD.
Maybe ALTER TYPE? But best to preallocate ahead of time.
One liner:
string XX (A51).
COMPUTE XX=CONCAT(" ",a1 TO a50).
------
data list / a1 to a50 (50A1).
begin data
gdsafdhasfghafasfdghafhafdhgasfgdfagdfasgdfasdgasf
dgafdhgafdhgafdafhgafsgafdgafghdafgdfagdfagdfagsfd
afdgafdgafdgafgdfagdffgadfhafdhgafdhgafgdfaghdfagh
dfgasdfgasfdafdagdfaghdfasghdfhadfsgfgshgfhsgfgshj
fgshjfghsgfsgfhjsgjfgsfgsgfhsgdgfsgfjhsghfgshdfghj
sgfhjsfghjsggdhaghdgashgfhjghgfsgfjsgfjgjgjhfgsdgf
end data.
string XX (A51).
COMPUTE XX=CONCAT(" ",a1 TO a50).
LIST.
HTH, David
--


David Wright-6 wrote:
>
> I need to insert a leading space on a concatenated string, listed below
> is my current method but I would like to get this as one line of syntax
> rather than two.
>
> string newvar (a50).
> compute newvar=concat(vara to varz).
> compute newvar=lpad(newvar,51).
>
>
> Thanks in advance.
>
> David,
>
> =====================
> 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/lpad-on-concatenated-compute-tp4945745p4945788.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