Selecting characters from variable length fields

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

Selecting characters from variable length fields

Jones, Carol
I have the following data:
k_m_ai  
g12_f_ai  
alt_m_as  
g1_f_as  
g4_m_aa  

I would like to create a new field that contains only the last two
characters.  How can I select only the last two characters in order to
create a new variable?  
 
Thanks
 
Carol G. Jones
School Data Specialist
Center for Educational Performance and Information
517.335.6445 (office)
517.335.0488 (fax)
 

====================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: Selecting characters from variable length fields

Richard Ristow
At 01:31 PM 3/13/2008, Jones, Carol wrote:

>I have the following data:
|-----------------------------|---------------------------|
|Output Created               |13-MAR-2008 13:58:31       |
|-----------------------------|---------------------------|
OLDVAR

k_m_ai
g12_f_ai
alt_m_as
g1_f_as
g4_m_aa

Number of cases read:  5    Number of cases listed:  5


>*  "I would like to create a new field that contains only the last .
>*  two characters."                                                .

STRING  NEWVAR  (A2).

*  The following is useful if your fields can have leading blanks.   .
*  If not, just apply the logic to OLDVAR instead of to #L.Just.     .

STRING  #L.Just (A12).
COMPUTE #L.Just = LTRIM(OLDVAR).

COMPUTE #Length = LENGTH(RTRIM(#L.Just)).
COMPUTE NEWVAR  = SUBSTR(#L.Just,#Length-1).

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |13-MAR-2008 13:58:31       |
|-----------------------------|---------------------------|
OLDVAR       NEWVAR

k_m_ai       ai
g12_f_ai     ai
alt_m_as     as
g1_f_as      as
g4_m_aa      aa

Number of cases read:  5    Number of cases listed:  5
===================
APPENDIX: Test data
===================
DATA LIST FIXED
   /OLDVAR 01-12 (A).
BEGIN DATA
k_m_ai
g12_f_ai
alt_m_as
g1_f_as
g4_m_aa
END DATA.
LIST.

=====================
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: Selecting characters from variable length fields

Melissa Ives
In reply to this post by Jones, Carol
Carol,

You will need to combine substr and length.
Replace the 'pos' in substr with "Length(var)-2".

Eg. (untested).
Compute v2=substr(var,Length(var)-2).

LENGTH. LENGTH(strexpr). Numeric. Returns the length of strexpr, which must be a string expression. This is the defined
length, including trailing blanks. To get the length without trailing blanks, use LENGTH(RTRIM(strexpr)).

SUBSTR. SUBSTR(strexpr,pos). String. Returns the substring beginning at position pos of strexpr and running to the end of strexpr.



Melissa

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Jones, Carol
Sent: Thursday, March 13, 2008 12:32 PM
To: [hidden email]
Subject: [SPSSX-L] Selecting characters from variable length fields

I have the following data:
k_m_ai
g12_f_ai
alt_m_as
g1_f_as
g4_m_aa

I would like to create a new field that contains only the last two characters.  How can I select only the last two characters in order to create a new variable?

Thanks

Carol G. Jones
School Data Specialist
Center for Educational Performance and Information
517.335.6445 (office)
517.335.0488 (fax)


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


PRIVILEGED AND CONFIDENTIAL INFORMATION
This transmittal and any attachments may contain PRIVILEGED AND
CONFIDENTIAL information and is intended only for the use of the
addressee. If you are not the designated recipient, or an employee
or agent authorized to deliver such transmittals to the designated
recipient, you are hereby notified that any dissemination,
copying or publication of this transmittal is strictly prohibited. If
you have received this transmittal in error, please notify us
immediately by replying to the sender and delete this copy from your
system. You may also call us at (309) 827-6026 for assistance.

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