I have a DOB variable (date type) as yyyy/mm/dd from which I need to create a
new variable that is simply the year of the first one in numeric form. So for example 2007/06/23 would become 2007 (numeric) in the new variable. I have been attempting to figure out a simple way to write some code for this but am struggling to be efficient. Any assistance would be most appreciated. Maybe I'm missing something elementary, but the recode function seems to be only for when both variables are numeric type. JD -- Sent from: http://spssx-discussion.1045642.n5.nabble.com/ ===================== 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 |
If this is already an SPSS date variable, just do this. COMPUTE year=XDATE.YEAR(thedate). if it is actually a string, you would need to get the year with char.substr and then convert to numeric. On Sat, Mar 23, 2019 at 10:05 AM J.D. Haltigan <[hidden email]> wrote: I have a DOB variable (date type) as yyyy/mm/dd from which I need to create a |
Thanks, Jon.
If you might oblige holding my hand through the following: My variable is named DOB (it is already a date type variable). So I would write: COMPUTE BIRTH_YEAR=DOB.YEAR. I wasn't sure what your parens with (the date) referred to. -- Sent from: http://spssx-discussion.1045642.n5.nabble.com/ ===================== 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 |
It would be COMPUTE BIRTH_YEAR = XDATE.YEAR(DOB). XDATE.YEAR is a function that takes a date variable as its argument and extracts the year portion. There are XDATE.xxx function for extracting all parts of date or time variables. The Transform > Compute dialog box function list is a convenient way to see all the transformation functions grouped into categories. XDATE.YEAR appears in the Date Extraction category. On Sat, Mar 23, 2019 at 11:21 AM J.D. Haltigan <[hidden email]> wrote: Thanks, Jon. |
Thanks for bearing with me Jon and for this pro-tip, esp. the
transform>compute dialog box function to see the grouped functions for use. -- Sent from: http://spssx-discussion.1045642.n5.nabble.com/ ===================== 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 |
I have a follow-up to my earlier query on this issue.
As with before I am working with DOB variable that is a date type in the format YYYY/MM/DD. In this instance, I am trying to transform this to a purely numeric variable such that 2007/11/28 (for example) becomes 20071128. I thought after reviewing the XDATE function set that XDATE.DATE(DOB) would do the trick, but when I run this function, I get a long numeric value which may be days or hours or some such. As one example, for 1997/11/20 I get 13099363200. What am I missing here? The description of XDATE.DATE in the function description seems like what I need but clearly not... Thanks in advance for insights. -- Sent from: http://spssx-discussion.1045642.n5.nabble.com/ ===================== 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 |
J.D.,
This is a little busy, but it will work. It's been tested. Perhaps someone else on the listserv can do it in one or two fell swoops!
If numdate is your date variable, then this will convert it to a string (altdate), concat the characters without the '/', and convert to a numeric of length 8.
string altdate (A10).
compute altdate = String(numdate, Adate10).
Exe.
string newdate(a10) .
compute newdate=concat(char.substr(altdate,1,2),char.substr(altdate,4,2),char.substr(altdate,7,4)) .
alter type newdate(f8) .
Brian Dates From: SPSSX(r) Discussion <[hidden email]> on behalf of J.D. Haltigan <[hidden email]>
Sent: Monday, March 25, 2019 8:03:53 PM To: [hidden email] Subject: Re: Date Type Variable to Numeric Type I have a follow-up to my earlier query on this issue.
As with before I am working with DOB variable that is a date type in the format YYYY/MM/DD. In this instance, I am trying to transform this to a purely numeric variable such that 2007/11/28 (for example) becomes 20071128. I thought after reviewing the XDATE function set that XDATE.DATE(DOB) would do the trick, but when I run this function, I get a long numeric value which may be days or hours or some such. As one example, for 1997/11/20 I get 13099363200. What am I missing here? The description of XDATE.DATE in the function description seems like what I need but clearly not... Thanks in advance for insights. -- Sent from: http://spssx-discussion.1045642.n5.nabble.com/ ===================== 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 |
In reply to this post by J.D. Haltigan
Your variable DOB is an SPSS date/time value. XDATE.DATE just extracts the date portion. In your case, since there is no time portion to DOB (I assume), it just gives you back the original value. To see the result with a date format, you just need to assign the date format you want via the FORMAT command or using the Data Editor Variable View. However, you asked for a numeric value, but I think you really want a string with year, month, and day components but no slash separators. You can build this as a string using the date extraction functions and converting each component to a string and the concatenating them like this. string thedate(a8). compute thedate = concat(string(xdate.year(DOB), n4), string(xdate.month(DOB), n2), string(xdate.mday(DOB), n2)). I used n format in converting the numer values from the xdate functions so that leading zero would be provided when the value is not wide enough to fill the field. On Mon, Mar 25, 2019 at 6:03 PM J.D. Haltigan <[hidden email]> wrote: I have a follow-up to my earlier query on this issue. |
In reply to this post by bdates
Brian:
This works perfectly save for one minor issue. In the resultant concatenated numeric instantiation I need the year to come first. The syntax below gives me the year last. Is there a simple way to concatenate the variable in a different order? As: compute newdate=concat(char.substr(altdate,7,4),char.substr(altdate,4,2),char.substr(altdate,1,2)) . Instead of the original: compute newdate=concat(char.substr(altdate,1,2),char.substr(altdate,4,2),char.substr(altdate,7,4)) . -- Sent from: http://spssx-discussion.1045642.n5.nabble.com/ ===================== 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 |
In reply to this post by J.D. Haltigan
Concatenate them in the order you want them to arrive. ;-O
Here is another method for the record. DATA LIST / mydate (ADATE). BEGIN DATA 03/25/2019 END DATA. COMPUTE MyNewDate=SUM(XDATE.YEAR(mydate) * 10000,XDATE.MONTH(mydate) * 100,XDATE.MDAY(mydate)). FORMATS MyNewDate (F8.0). /* If you need it as a string then use ALTER TYPE */. COMPUTE CopyOfMyDate=MyNewDate. ALTER TYPE CopyOfMyDate(A8). LIST. mydate MyNewDate CopyOfMyDate 03/25/2019 20190325 20190325 ===================== 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 |
Free forum by Nabble | Edit this page |