date format and calculation

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

date format and calculation

Rongjin Guan
Good morning, everyone.

I have two date variables, first one in yyyymm format (like 201512), and the other
variable in mm/dd/yyyy format (like 12/14/2015). I want to calculate the difference
between two dates and use year as unit, say, if 10 years 7 months, it will round to 11;
if 10 years 5 months, it will round to 10 years.

Can anyone kindly give me some hints?

Thanks

Rongjin Guan
School of Social Work
Rutgers University
=====================
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: date format and calculation

Jon Peck
These variables need to be converted to true SPSS date values if they are not already, i.e., if they are currently strings.
Then the datediff function can do arithmetic on them, and you can choose the units of the result.

I suggest that you start with the Date and Time Wizard on the Transform menu to work this out.

On Mon, Dec 14, 2015 at 8:51 AM, Rongjin Guan <[hidden email]> wrote:
Good morning, everyone.

I have two date variables, first one in yyyymm format (like 201512), and the other
variable in mm/dd/yyyy format (like 12/14/2015). I want to calculate the difference
between two dates and use year as unit, say, if 10 years 7 months, it will round to 11;
if 10 years 5 months, it will round to 10 years.

Can anyone kindly give me some hints?

Thanks

Rongjin Guan
School of Social Work
Rutgers University
=====================
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



--
Jon K Peck
[hidden email]

===================== 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: date format and calculation

Rongjin Guan
Thank you, Jon, for your reply.
I figured it out using MOD and TRUNC functions.

Now I have a new problem, that I have yyyy-mm as string, and I want to convert to mm/dd/yyyy
(I can add day as 15 to all of them).

I tried to convert it to yyyymm format since I figured out how to convert it already,
but neither of the two following work"
compute  newdate=replace(birth_date,'-','') and
compute  newdate=concat(substr(birth_date,6,2),'',substr(birth_date,1,4))

Can anyone give some hints how to convert the yyyy-mm format to mm/dd/yyyy?
Or how to remove the '-' since I figured out how to convert yyyymm format already.

Thank you

Rongjin


________________________________________
From: Jon Peck [[hidden email]]
Sent: Monday, December 14, 2015 11:01 AM
To: Rongjin Guan
Cc: [hidden email]
Subject: Re: [SPSSX-L] date format and calculation

These variables need to be converted to true SPSS date values if they are not already, i.e., if they are currently strings.
Then the datediff function can do arithmetic on them, and you can choose the units of the result.

I suggest that you start with the Date and Time Wizard on the Transform menu to work this out.

On Mon, Dec 14, 2015 at 8:51 AM, Rongjin Guan <[hidden email]<mailto:[hidden email]>> wrote:
Good morning, everyone.

I have two date variables, first one in yyyymm format (like 201512), and the other
variable in mm/dd/yyyy format (like 12/14/2015). I want to calculate the difference
between two dates and use year as unit, say, if 10 years 7 months, it will round to 11;
if 10 years 5 months, it will round to 10 years.

Can anyone kindly give me some hints?

Thanks

Rongjin Guan
School of Social Work
Rutgers University
=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email]<mailto:[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



--
Jon K Peck
[hidden email]<mailto:[hidden email]>

=====================
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: date format and calculation

Jon Peck
Here's an example
data list list/date(a10).
begin data
"2015-12"
end data.
compute spssdate = date.mdy(
    numeric(
        char.substr(date, 6, 2),
        F2.0
    ), 
    15, 
    numeric(
        char.substr(date, 1,4), 
        F4.0
    )
).
format spssdate(adate8).
list.

On Mon, Dec 14, 2015 at 1:43 PM, Rongjin Guan <[hidden email]> wrote:
Thank you, Jon, for your reply.
I figured it out using MOD and TRUNC functions.

Now I have a new problem, that I have yyyy-mm as string, and I want to convert to mm/dd/yyyy
(I can add day as 15 to all of them).

I tried to convert it to yyyymm format since I figured out how to convert it already,
but neither of the two following work"
compute  newdate=replace(birth_date,'-','') and
compute  newdate=concat(substr(birth_date,6,2),'',substr(birth_date,1,4))

Can anyone give some hints how to convert the yyyy-mm format to mm/dd/yyyy?
Or how to remove the '-' since I figured out how to convert yyyymm format already.

Thank you

Rongjin


________________________________________
From: Jon Peck [[hidden email]]
Sent: Monday, December 14, 2015 11:01 AM
To: Rongjin Guan
Cc: [hidden email]
Subject: Re: [SPSSX-L] date format and calculation

These variables need to be converted to true SPSS date values if they are not already, i.e., if they are currently strings.
Then the datediff function can do arithmetic on them, and you can choose the units of the result.

I suggest that you start with the Date and Time Wizard on the Transform menu to work this out.

On Mon, Dec 14, 2015 at 8:51 AM, Rongjin Guan <[hidden email]<mailto:[hidden email]>> wrote:
Good morning, everyone.

I have two date variables, first one in yyyymm format (like 201512), and the other
variable in mm/dd/yyyy format (like 12/14/2015). I want to calculate the difference
between two dates and use year as unit, say, if 10 years 7 months, it will round to 11;
if 10 years 5 months, it will round to 10 years.

Can anyone kindly give me some hints?

Thanks

Rongjin Guan
School of Social Work
Rutgers University
=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email]<mailto:[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



--
Jon K Peck
[hidden email]<mailto:[hidden email]>

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



--
Jon K Peck
[hidden email]

===================== 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: date format and calculation

David Marso
Administrator
This is a bit less verbose:

data list list/date(a10).
begin data
"2015-12"
end data.
COMPUTE SPSSdate=NUMBER(CONCAT(CHAR.SUBSTR(date,6),"/15/",CHAR.SUBSTR(date,1,4)),ADATE10).
FORMATS SPSSdate (ADATE).
LIST.

Jon Peck wrote
Here's an example
data list list/date(a10).
begin data
"2015-12"
end data.
compute spssdate = date.mdy(
    numeric(
        char.substr(date, 6, 2),
        F2.0
    ),
    15,
    numeric(
        char.substr(date, 1,4),
        F4.0
    )
).
format spssdate(adate8).
list.

On Mon, Dec 14, 2015 at 1:43 PM, Rongjin Guan <[hidden email]> wrote:

> Thank you, Jon, for your reply.
> I figured it out using MOD and TRUNC functions.
>
> Now I have a new problem, that I have yyyy-mm as string, and I want to
> convert to mm/dd/yyyy
> (I can add day as 15 to all of them).
>
> I tried to convert it to yyyymm format since I figured out how to convert
> it already,
> but neither of the two following work"
> compute  newdate=replace(birth_date,'-','') and
> compute  newdate=concat(substr(birth_date,6,2),'',substr(birth_date,1,4))
>
> Can anyone give some hints how to convert the yyyy-mm format to mm/dd/yyyy?
> Or how to remove the '-' since I figured out how to convert yyyymm format
> already.
>
> Thank you
>
> Rongjin
>
>
> ________________________________________
> From: Jon Peck [[hidden email]]
> Sent: Monday, December 14, 2015 11:01 AM
> To: Rongjin Guan
> Cc: [hidden email]
> Subject: Re: [SPSSX-L] date format and calculation
>
> These variables need to be converted to true SPSS date values if they are
> not already, i.e., if they are currently strings.
> Then the datediff function can do arithmetic on them, and you can choose
> the units of the result.
>
> I suggest that you start with the Date and Time Wizard on the Transform
> menu to work this out.
>
> On Mon, Dec 14, 2015 at 8:51 AM, Rongjin Guan <[hidden email]
> <mailto:[hidden email]>> wrote:
> Good morning, everyone.
>
> I have two date variables, first one in yyyymm format (like 201512), and
> the other
> variable in mm/dd/yyyy format (like 12/14/2015). I want to calculate the
> difference
> between two dates and use year as unit, say, if 10 years 7 months, it will
> round to 11;
> if 10 years 5 months, it will round to 10 years.
>
> Can anyone kindly give me some hints?
>
> Thanks
>
> Rongjin Guan
> School of Social Work
> Rutgers University
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email]<mailto:[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
>
>
>
> --
> Jon K Peck
> [hidden email]<mailto:[hidden email]>
>
> =====================
> 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
>



--
Jon K Peck
[hidden email]

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