Removing last 3 numbers from variable

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

Removing last 3 numbers from variable

Jennifer Steffes
Hi Listserv-

I have a string variable named "SCID" that currently has 11 numbers.  I
want to remove the last 3 from all of the entries so that it goes from:

06005001036
to
06005001

Is there a syntax out there that will do this easily for me?

Thanks,
Jenny

=====================
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: Removing last 3 numbers from variable

vlad simion
Hi Jenny,

if your variable length is constant (11), try this:

string scid_new (a20).
compute scid_new=substr(scid,1,8).
exe.

Hth,
Vlad

On Nov 27, 2007 6:29 PM, Jennifer Steffes <[hidden email]> wrote:

> Hi Listserv-
>
> I have a string variable named "SCID" that currently has 11 numbers.  I
> want to remove the last 3 from all of the entries so that it goes from:
>
> 06005001036
> to
> 06005001
>
> Is there a syntax out there that will do this easily for me?
>
> Thanks,
> Jenny
>
> =====================
> 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
>



--
Vlad Simion
Data Analyst
Tel:      +40 0751945296

=====================
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: Removing last 3 numbers from variable

David Wasserman
Or, even more simply:

string scid_new (a8).
compute scid_new=scid.

The new string will automatically take the first 8 characters of the
original.

David Wasserman
Custom Data Analysis and SPSS Programming

----- Original Message -----
From: "vlad simion" <[hidden email]>
To: <[hidden email]>
Sent: Tuesday, November 27, 2007 9:42 AM
Subject: Re: Removing last 3 numbers from variable


> Hi Jenny,
>
> if your variable length is constant (11), try this:
>
> > exe.
>
> Hth,
> Vlad
>
> On Nov 27, 2007 6:29 PM, Jennifer Steffes <[hidden email]> wrote:
>
>> Hi Listserv-
>>
>> I have a string variable named "SCID" that currently has 11 numbers.  I
>> want to remove the last 3 from all of the entries so that it goes from:
>>
>> 06005001036
>> to
>> 06005001
>>
>> Is there a syntax out there that will do this easily for me?
>>
>> Thanks,
>> Jenny
>>
>> =====================
>> 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
>>
>
>
>
> --
> Vlad Simion
> Data Analyst
> Tel:      +40 0751945296
>
> =====================
> 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: Removing last 3 numbers from variable

ViAnn Beadle
In reply to this post by Jennifer Steffes
This question is similar to a most recent post of yours except you're
trimming at the end of a string.

Use the SUBSTRING function to get the first 8 characters:

STRING newstring (A8).
COMPUTE newstring=SUBSTRING(SCID,1,8).

The arguments to SUBSTRING are string, startposition, numberofcharacters.
The third argument is required here.

This is the "approved" way to do it.

However, a much simpler way it to just declare your newstring with the
STRING command and then just compute the newstring = SCID. This works
because you have set the length of your newstring to 8 so it will only hold
the first 8 characters of the string from which it is computed.

Since you've got to do these kinds of string manipulations, go to Help and
look at the string functions (COMPUTE command) topic. This will walk you
through the syntax used to manipulate string. You can get there by the index
or follow the path from Command Syntax Reference>Compute>Examples>String
Functions (COMPUTE command).




-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Jennifer Steffes
Sent: Tuesday, November 27, 2007 9:29 AM
To: [hidden email]
Subject: Removing last 3 numbers from variable

Hi Listserv-

I have a string variable named "SCID" that currently has 11 numbers.  I
want to remove the last 3 from all of the entries so that it goes from:

06005001036
to
06005001

Is there a syntax out there that will do this easily for me?

Thanks,
Jenny

=====================
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: Removing last 3 numbers from variable

ViAnn Beadle
In reply to this post by vlad simion
Why are you declaring the scid_new variable to be 20 characters long?

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
vlad simion
Sent: Tuesday, November 27, 2007 9:42 AM
To: [hidden email]
Subject: Re: Removing last 3 numbers from variable

Hi Jenny,

if your variable length is constant (11), try this:

string scid_new (a20).
compute scid_new=substr(scid,1,8).
exe.

Hth,
Vlad

On Nov 27, 2007 6:29 PM, Jennifer Steffes <[hidden email]> wrote:

> Hi Listserv-
>
> I have a string variable named "SCID" that currently has 11 numbers.  I
> want to remove the last 3 from all of the entries so that it goes from:
>
> 06005001036
> to
> 06005001
>
> Is there a syntax out there that will do this easily for me?
>
> Thanks,
> Jenny
>
> =====================
> 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
>



--
Vlad Simion
Data Analyst
Tel:      +40 0751945296

=====================
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: Removing last 3 numbers from variable

Art Kendall-2
In reply to this post by Jennifer Steffes
something like this untested syntax should do it.

string newSCID (a8).
compute newSCID = substr(SCID, 1,8).

Art Kendall
Social Research Consultants

Jennifer Steffes wrote:

> Hi Listserv-
>
> I have a string variable named "SCID" that currently has 11 numbers.  I
> want to remove the last 3 from all of the entries so that it goes from:
>
> 06005001036
> to
> 06005001
>
> Is there a syntax out there that will do this easily for me?
>
> Thanks,
> Jenny
>
> =====================
> 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: Removing last 3 numbers from variable

vlad simion
In reply to this post by ViAnn Beadle
For no reaason, I just declared a string variable of 20 length :)

On Nov 27, 2007 6:52 PM, ViAnn Beadle <[hidden email]> wrote:

> Why are you declaring the scid_new variable to be 20 characters long?
>
> -----Original Message-----
> From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
> vlad simion
> Sent: Tuesday, November 27, 2007 9:42 AM
> To: [hidden email]
> Subject: Re: Removing last 3 numbers from variable
>
> Hi Jenny,
>
> if your variable length is constant (11), try this:
>
> string scid_new (a20).
> compute scid_new=substr(scid,1,8).
> exe.
>
> Hth,
> Vlad
>
> On Nov 27, 2007 6:29 PM, Jennifer Steffes <[hidden email]> wrote:
>
> > Hi Listserv-
> >
> > I have a string variable named "SCID" that currently has 11 numbers.  I
> > want to remove the last 3 from all of the entries so that it goes from:
> >
> > 06005001036
> > to
> > 06005001
> >
> > Is there a syntax out there that will do this easily for me?
> >
> > Thanks,
> > Jenny
> >
> > =====================
> > 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
> >
>
>
>
> --
> Vlad Simion
> Data Analyst
> Tel:      +40 0751945296
>
> =====================
> 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
>
>


--
Vlad Simion
Data Analyst
Tel:      +40 0751945296

=====================
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: Removing last 3 numbers from variable

Norton, John
In reply to this post by vlad simion
Hi Jenny,

I'd take Vlad's suggestion one step further and use the NUMBER function to create a numeric variable from the original, thus:

COMPUTE scid_num=NUMBER(SUBSTR(scid,1,8),F8).
EXE.
FORMATS scid_num (N8).

It seems to me that if the contents of your variable are numbers, it makes sense that the variable be defined as a number.  The FORMATS statement will also retain leading zeros.

Again, as Vlad points out, this assumes that your source variable consistently has 11 characters.

ViAnn's solution - to simply compute an 8 character variable from the 11 character source variable - is also efficient, but retains the string definition of the variable.  If you want your target variable to be defined as a number, then the above approach should suffice.

HTH,

John Norton

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of vlad simion
Sent: Tuesday, November 27, 2007 10:42 AM
To: [hidden email]
Subject: Re: Removing last 3 numbers from variable

Hi Jenny,

if your variable length is constant (11), try this:

string scid_new (a20).
compute scid_new=substr(scid,1,8).
exe.

Hth,
Vlad

On Nov 27, 2007 6:29 PM, Jennifer Steffes <[hidden email]> wrote:

> Hi Listserv-
>
> I have a string variable named "SCID" that currently has 11 numbers.  I
> want to remove the last 3 from all of the entries so that it goes from:
>
> 06005001036
> to
> 06005001
>
> Is there a syntax out there that will do this easily for me?
>
> Thanks,
> Jenny
>
> =====================
> 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
>



--
Vlad Simion
Data Analyst
Tel:      +40 0751945296

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