help with extracting month as a numeric from a string variable

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

help with extracting month as a numeric from a string variable

thara vardhan-2
Dear Members

Can anyone help me solve this simple problem please?

Currently I have a string variable "month" with values as

Month
2007-01
2007-06
2007-12

I need to extract only the month values and compute a new variable as a
numeric
       newmonth
        01
        06
        12

I have tried the following syntax  and it works wonderfully but
unfortunately the resultant variable is a string variable and I need it as
a numeric.

string newmonth(a8).
compute newmonth=substr(month,6,8).
exe.

I also tried using the date/time wizard but it gives me the resultant
value as
                                                              jan
                                                              Jun
                                                              Dec

thanks
regards
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

This message and any attachment is confidential and may
be privileged or otherwise protected from disclosure. If you
have received it by mistake, please let us know by reply
and then delete it from your system; you should not copy
the message or disclose its contents to anyone.

=====================
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: help with extracting month as a numeric from a string variable

Peck, Jon
I think what you are getting from the date/time wizard is actually what you want.  The wizard create a month-format variable with values 1-12, but the format labels it with the three-letter month name.  You can use this as a regular numeric variable.
If you try
compute latermonth = newmonth+1, it should work (assuming newmonth is what the wizard produced).

HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Thara Vardhan
Sent: Tuesday, March 18, 2008 7:29 PM
To: [hidden email]
Subject: [SPSSX-L] help with extracting month as a numeric from a string variable

Dear Members

Can anyone help me solve this simple problem please?

Currently I have a string variable "month" with values as

Month
2007-01
2007-06
2007-12

I need to extract only the month values and compute a new variable as a
numeric
       newmonth
        01
        06
        12

I have tried the following syntax  and it works wonderfully but
unfortunately the resultant variable is a string variable and I need it as
a numeric.

string newmonth(a8).
compute newmonth=substr(month,6,8).
exe.

I also tried using the date/time wizard but it gives me the resultant
value as
                                                              jan
                                                              Jun
                                                              Dec

thanks
regards
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

This message and any attachment is confidential and may
be privileged or otherwise protected from disclosure. If you
have received it by mistake, please let us know by reply
and then delete it from your system; you should not copy
the message or disclose its contents to anyone.

=====================
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: reply to Jon Peck- help with extracting month as a numeric from a string variable

thara vardhan-2
Hi Jon

Thanks for your help. Unfortunately while I was experimenting with several
options I lost track of how I arrived at Jan, Jun and Dec values.

However using the time/date wizard menu I was able to solve it as 2 step
process.

First option under the wizard use the

create a time/date variable from a string variable

* Date and Time Wizard: month1.
COMPUTE month1 = number(Month, JDATE7).
VARIABLE LABEL month1.
VARIABLE LEVEL month1 (SCALE).
FORMATS month1 (JDATE7).
VARIABLE WIDTH month1(7).
EXECUTE.

Second stage is under time/date wizard

use extract part of a date or time variabe

* Date and Time Wizard: finalmonth.
COMPUTE finalmonth = XDATE.MDAY(month1).
VARIABLE LABEL finalmonth "finalmonth".
VARIABLE LEVEL finalmonth (SCALE).
FORMATS finalmonth (F8.0).
VARIABLE WIDTH finalmonth(8).
EXECUTE.

The resultant variable is finalmonth as a numeric value. Sample output is
as below

Month      Month1          finalmonth
2007-01    2007001            1

Thanks a lot.
regards
Thara Vardhan
Senior Statistician
Planning & Results
Organisation Review and Support
NSW Police
Tel: (02) 8835-8526




"Peck, Jon" <[hidden email]>
Sent by: "SPSSX(r) Discussion" <[hidden email]>
19/03/2008 12:56
Please respond to
"Peck, Jon" <[hidden email]>


To
[hidden email]
cc

Subject
Re: help with extracting month as a numeric from a string variable






I think what you are getting from the date/time wizard is actually what
you want.  The wizard create a month-format variable with values 1-12, but
the format labels it with the three-letter month name.  You can use this
as a regular numeric variable.
If you try
compute latermonth = newmonth+1, it should work (assuming newmonth is what
the wizard produced).

HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Thara Vardhan
Sent: Tuesday, March 18, 2008 7:29 PM
To: [hidden email]
Subject: [SPSSX-L] help with extracting month as a numeric from a string
variable

Dear Members

Can anyone help me solve this simple problem please?

Currently I have a string variable "month" with values as

Month
2007-01
2007-06
2007-12

I need to extract only the month values and compute a new variable as a
numeric
       newmonth
        01
        06
        12

I have tried the following syntax  and it works wonderfully but
unfortunately the resultant variable is a string variable and I need it as
a numeric.

string newmonth(a8).
compute newmonth=substr(month,6,8).
exe.

I also tried using the date/time wizard but it gives me the resultant
value as
                                                              jan
                                                              Jun
                                                              Dec

thanks
regards
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_

This message and any attachment is confidential and may
be privileged or otherwise protected from disclosure. If you
have received it by mistake, please let us know by reply
and then delete it from your system; you should not copy
the message or disclose its contents to anyone.

=====================
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
All mail is subject to content scanning for possible violation of New
South Wales Police Force
Electronic Messaging Policy. All NSWPF employees are required to
familiarise themselves with the
content of the policy, found under Policies on the NSW Police Force
Intranet.





_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

This message and any attachment is confidential and may
be privileged or otherwise protected from disclosure. If you
have received it by mistake, please let us know by reply
and then delete it from your system; you should not copy
the message or disclose its contents to anyone.

=====================
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: help with extracting month as a numeric from a string variable

Richard Ristow
In reply to this post by thara vardhan-2
At 09:28 PM 3/18/2008, Thara Vardhan wrote:

>Currently I have a string variable "month" with values as
>
>Month
>2007-01
>2007-06
>2007-12
>
>I need to extract only the month values and compute a new variable as a
>numeric
>        newmonth
>         01
>         06
>         12
>
>The following syntax works wonderfully but unfortunately the
>resultant variable is a string variable and I need it as a numeric.
>
>string newmonth(a8).
>compute newmonth=substr(month,6,8).

The code from the date-time wizard from your follow-up posting
Date:    Wed, 19 Mar 2008 15:02:18 +1100
From:    Thara Vardhan <[hidden email]>
Subject: Re: reply to Jon Peck- help with extracting month as a numeric
          from a string  variable
To:      [hidden email]
works just fine.

However, you could use the simple logic you started with, with the
small addition of converting the string-format month value into a number:


NUMERIC newmonth(N2).
compute newmonth=NUMBER(substr(month,6,8),F2).

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |19-MAR-2008 01:44:20       |
|-----------------------------|---------------------------|
Month        newmonth

2007-01         01
2007-06         06
2007-12         12

Number of cases read:  3    Number of cases listed:  3
===================
APPENDIX: Test data
===================
*  Currently I have a string variable "month" with values as          .
DATA LIST FIXED/
    Month 01-12 (A).

Data List will read 1 records from the command file

Variable          Rec   Start     End  Format

Month               1       1      12  A12

BEGIN DATA
2007-01
2007-06
2007-12
END DATA.

LIST.

List
|-----------------------------|----------------------------|
|Output Created               |19-MAR-2008 |01:41:10       |
|-----------------------------|----------------------------|
Month

2007-01
2007-06
2007-12

Number of cases read:  3    Number of cases listed:  3

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