substring?

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

substring?

Leo Bakker
Hello all,

I’m still a bit of a newbie so I’ll hope  someone will help me with a
small but annoying problem.
I have a list of cases numbered by year followed by an id number like this:
  200300500
  200400258
  200400279
  200501378
  200501457
  200300079
  200301081
  200300387

I want to create a new var getting only the last four digits, so 200300500
becomes 0500 and  200501378 becomes 1378.
I’ve tried it with this:

COMPUTE nummer  = NUMBER(STRING(adm_id, 5, 9), F5.0).
EXE.

But that doesn’t work, I think because it isn’t a string, how do I get
what I want?

Thanks,
Leo

=====================
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: substring?

Bob Walker-2
Reply | Threaded
Open this post in threaded view
|

Re: substring?

Justin Black
In reply to this post by Leo Bakker
Hi Leo,

Does the syntax below get you what you need?

*** Creates sample data .

DATA LIST FREE /adm_id (A50).
BEGIN DATA
'200300500'
'200400258'
'200400279'
'200501378'
'200501457'
'200300079'
'200301081'
'200300387'
END DATA.

*** Creates numeric variable, the values of which .
*** are the last four digits of adm_id .

COMPUTE nummer = NUMBER(SUBSTR(adm_id,6),F5.0) .
FORMAT nummer (N4) .
LIST .

On Fri, Mar 21, 2008 at 9:01 AM, Leo Bakker <[hidden email]> wrote:

> Hello all,
>
> I'm still a bit of a newbie so I'll hope  someone will help me with a
> small but annoying problem.
> I have a list of cases numbered by year followed by an id number like this:
>  200300500
>  200400258
>  200400279
>  200501378
>  200501457
>  200300079
>  200301081
>  200300387
>
> I want to create a new var getting only the last four digits, so 200300500
> becomes 0500 and  200501378 becomes 1378.
> I've tried it with this:
>
> COMPUTE nummer  = NUMBER(STRING(adm_id, 5, 9), F5.0).
> EXE.
>
> But that doesn't work, I think because it isn't a string, how do I get
> what I want?
>
> Thanks,
> Leo
>
> =====================
> 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: substring?

Melissa Ives
In reply to this post by Bob Walker-2
Of course if the problem really is that the ID is numeric, SUBSTR won't even work.

If that is the case, I suggest that MOD( ) would be the place to start.

MOD. MOD(numexpr,modulus). Numeric. Returns the remainder when numexpr is divided by modulus. Both arguments must be numeric, and modulus must not be 0.

***untested.
String nummer (A4).
Compute nummer=lpad(string(mod(adm_id,10000),A4.0),4,'0').

If you don't really care about the leading zeros,
Compute nummer=mod(adm_id,10000).
Will give you the following for your data below.
500
258
279
1378
1457
79
1081
387

Melissa

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bob Walker
Sent: Friday, March 21, 2008 9:22 AM
To: [hidden email]
Subject: Re: [SPSSX-L] substring?

Leo,

You used "STRING" instead of "SUBSTR", which would have parsed the string as intended. You wrote:

COMPUTE number=NUMBER(STRING(adm_id,5,9), F5.0).

The format is SUBSTR(string variable, starting position, length). Your arguments say that you want 9 characters starting at position 5, but you really wanted 5 characters starting at position 5, or:

COMPUTE number=NUMBER(SUBSTR(adm_id,5,5),F5).

Here are two options, depending upon whether you want numbers or strings as the final result...

DATA LIST FREE /VAR1 (A9).
BEGIN DATA
200300500
200400258
200400279
200501378
200501457
200300079
200301081
200300387
END DATA.
DATASET NAME DS1 WINDOW=FRONT.

* ===== FOR NUMERIC VALUES.
COMPUTE YEAR1=NUMBER(SUBSTR(VAR1,1,4),F4).
COMPUTE ADMIN1=NUMBER(SUBSTR(VAR1,5,5),F5).

* ===== FOR STRING VALUES.
STRING YEAR2 (A4) ADMIN2 (A5).
COMPUTE YEAR2=SUBSTR(VAR1,1,4).
COMPUTE ADMIN2=SUBSTR(VAR1,5,5).
EXECUTE.

The numeric option ignores leading zeros -- I don't know if that's important to you. Regards,

Bob Walker
Surveys & Forecasts, LLC

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Leo Bakker
Sent: Friday, March 21, 2008 9:02 AM
To: [hidden email]
Subject: substring?

Hello all,

I’m still a bit of a newbie so I’ll hope  someone will help me with a small but annoying problem.
I have a list of cases numbered by year followed by an id number like this:
  200300500
  200400258
  200400279
  200501378
  200501457
  200300079
  200301081
  200300387

I want to create a new var getting only the last four digits, so 200300500 becomes 0500 and  200501378 becomes 1378.
I’ve tried it with this:

COMPUTE nummer  = NUMBER(STRING(adm_id, 5, 9), F5.0).
EXE.

But that doesn’t work, I think because it isn’t a string, how do I get what I want?

Thanks,
Leo

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




E-mail message checked by Spyware Doctor (5.5.0.212) Database version: 5.09460 http://www.pctools.com/en/spyware-doctor/





E-mail message checked by Spyware Doctor (5.5.0.212) Database version: 5.09460 http://www.pctools.com/en/spyware-doctor/

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

Re: substring?

ViAnn Beadle
How about just wrapping the number in a string function first, like


data list free / adm_id (f9).
begin data
200300500
200400258
200400279
200501378
200501457
200300079
200301081
200300387
end data.
numeric x (f4.0).
Compute x= number(substr(String(adm_id,f9),6,9), f4.0).

Is there some reason that the results have to also be a number? If not, then strip off the outermost number function and define a to be a string with

String x (a4)
Compute x= substr(String(adm_id,f9),6,9).


list.-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Melissa Ives
Sent: Friday, March 21, 2008 8:57 AM
To: [hidden email]
Subject: Re: substring?

Of course if the problem really is that the ID is numeric, SUBSTR won't even work.

If that is the case, I suggest that MOD( ) would be the place to start.

MOD. MOD(numexpr,modulus). Numeric. Returns the remainder when numexpr is divided by modulus. Both arguments must be numeric, and modulus must not be 0.

***untested.
String nummer (A4).
Compute nummer=lpad(string(mod(adm_id,10000),A4.0),4,'0').

If you don't really care about the leading zeros,
Compute nummer=mod(adm_id,10000).
Will give you the following for your data below.
500
258
279
1378
1457
79
1081
387

Melissa

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bob Walker
Sent: Friday, March 21, 2008 9:22 AM
To: [hidden email]
Subject: Re: [SPSSX-L] substring?

Leo,

You used "STRING" instead of "SUBSTR", which would have parsed the string as intended. You wrote:

COMPUTE number=NUMBER(STRING(adm_id,5,9), F5.0).

The format is SUBSTR(string variable, starting position, length). Your arguments say that you want 9 characters starting at position 5, but you really wanted 5 characters starting at position 5, or:

COMPUTE number=NUMBER(SUBSTR(adm_id,5,5),F5).

Here are two options, depending upon whether you want numbers or strings as the final result...

DATA LIST FREE /VAR1 (A9).
BEGIN DATA
200300500
200400258
200400279
200501378
200501457
200300079
200301081
200300387
END DATA.
DATASET NAME DS1 WINDOW=FRONT.

* ===== FOR NUMERIC VALUES.
COMPUTE YEAR1=NUMBER(SUBSTR(VAR1,1,4),F4).
COMPUTE ADMIN1=NUMBER(SUBSTR(VAR1,5,5),F5).

* ===== FOR STRING VALUES.
STRING YEAR2 (A4) ADMIN2 (A5).
COMPUTE YEAR2=SUBSTR(VAR1,1,4).
COMPUTE ADMIN2=SUBSTR(VAR1,5,5).
EXECUTE.

The numeric option ignores leading zeros -- I don't know if that's important to you. Regards,

Bob Walker
Surveys & Forecasts, LLC

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Leo Bakker
Sent: Friday, March 21, 2008 9:02 AM
To: [hidden email]
Subject: substring?

Hello all,

I’m still a bit of a newbie so I’ll hope  someone will help me with a small but annoying problem.
I have a list of cases numbered by year followed by an id number like this:
  200300500
  200400258
  200400279
  200501378
  200501457
  200300079
  200301081
  200300387

I want to create a new var getting only the last four digits, so 200300500 becomes 0500 and  200501378 becomes 1378.
I’ve tried it with this:

COMPUTE nummer  = NUMBER(STRING(adm_id, 5, 9), F5.0).
EXE.

But that doesn’t work, I think because it isn’t a string, how do I get what I want?

Thanks,
Leo

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




E-mail message checked by Spyware Doctor (5.5.0.212) Database version: 5.09460 http://www.pctools.com/en/spyware-doctor/





E-mail message checked by Spyware Doctor (5.5.0.212) Database version: 5.09460 http://www.pctools.com/en/spyware-doctor/

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

=====================
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: substring?

Leo Bakker-2
In reply to this post by Melissa Ives
Thanks Melissa, it works just fine.
The MOD function was what I was searching for.

Greetz,
Leo


-----Oorspronkelijk bericht-----
Van: SPSSX(r) Discussion [mailto:[hidden email]] Namens Melissa Ives
Verzonden: vrijdag 21 maart 2008 15:57
Aan: [hidden email]
Onderwerp: Re: substring?

Of course if the problem really is that the ID is numeric, SUBSTR won't even work.

If that is the case, I suggest that MOD( ) would be the place to start.

MOD. MOD(numexpr,modulus). Numeric. Returns the remainder when numexpr is divided by modulus. Both arguments must be numeric, and modulus must not be 0.

***untested.
String nummer (A4).
Compute nummer=lpad(string(mod(adm_id,10000),A4.0),4,'0').

If you don't really care about the leading zeros,
Compute nummer=mod(adm_id,10000).
Will give you the following for your data below.
500
258
279
1378
1457
79
1081
387

Melissa

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bob Walker
Sent: Friday, March 21, 2008 9:22 AM
To: [hidden email]
Subject: Re: [SPSSX-L] substring?

Leo,

You used "STRING" instead of "SUBSTR", which would have parsed the string as intended. You wrote:

COMPUTE number=NUMBER(STRING(adm_id,5,9), F5.0).

The format is SUBSTR(string variable, starting position, length). Your arguments say that you want 9 characters starting at position 5, but you really wanted 5 characters starting at position 5, or:

COMPUTE number=NUMBER(SUBSTR(adm_id,5,5),F5).

Here are two options, depending upon whether you want numbers or strings as the final result...

DATA LIST FREE /VAR1 (A9).
BEGIN DATA
200300500
200400258
200400279
200501378
200501457
200300079
200301081
200300387
END DATA.
DATASET NAME DS1 WINDOW=FRONT.

* ===== FOR NUMERIC VALUES.
COMPUTE YEAR1=NUMBER(SUBSTR(VAR1,1,4),F4).
COMPUTE ADMIN1=NUMBER(SUBSTR(VAR1,5,5),F5).

* ===== FOR STRING VALUES.
STRING YEAR2 (A4) ADMIN2 (A5).
COMPUTE YEAR2=SUBSTR(VAR1,1,4).
COMPUTE ADMIN2=SUBSTR(VAR1,5,5).
EXECUTE.

The numeric option ignores leading zeros -- I don't know if that's important to you. Regards,

Bob Walker
Surveys & Forecasts, LLC

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Leo Bakker
Sent: Friday, March 21, 2008 9:02 AM
To: [hidden email]
Subject: substring?

Hello all,

I’m still a bit of a newbie so I’ll hope  someone will help me with a small but annoying problem.
I have a list of cases numbered by year followed by an id number like this:
  200300500
  200400258
  200400279
  200501378
  200501457
  200300079
  200301081
  200300387

I want to create a new var getting only the last four digits, so 200300500 becomes 0500 and  200501378 becomes 1378.
I’ve tried it with this:

COMPUTE nummer  = NUMBER(STRING(adm_id, 5, 9), F5.0).
EXE.

But that doesn’t work, I think because it isn’t a string, how do I get what I want?

Thanks,
Leo

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




E-mail message checked by Spyware Doctor (5.5.0.212) Database version: 5.09460 http://www.pctools.com/en/spyware-doctor/





E-mail message checked by Spyware Doctor (5.5.0.212) Database version: 5.09460 http://www.pctools.com/en/spyware-doctor/

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

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