How to create a single date variable from separate month and year variables in SPSS - spss

I have a data set in which the date of an event is represented by two variables: month of the event and the year of the event. I would like to turn these two variables into a single variable, preferably formatted as a date/time variable. How would the syntax for doing this in SPSS look?
NB. I don't have a variable specifying which day (1-31) of the month a date is (and the day isn't important either). I was thinking of either just using a date format that only include the month and year, or specifying that all events happened on the first of each month.
Image illustrating how the variables look:
Image illustrating how the new date variable should look

From the menu:
Transform/Date and Time Wizard/Create a Date/time variables from...
Then select your Month variable to the Month field, Year variable to Year field
Press Next
Put a name into the 'Result Variable` field (e.g.: "MonthYear")
From the "Output format" choose whichever format you want (e.g.: "mmm yyyy")
I would suggest to "Paste" the syntax, in case you need it later ;)
Press Finish and you will get a syntax looking like this (based on the above inputs):
COMPUTE MonthYear=DATE.DMY(1, Month, Year).
VARIABLE LABELS MonthYear "".
VARIABLE LEVEL MonthYear (SCALE).
FORMATS MonthYear (MOYR8).
VARIABLE WIDTH MonthYear(8).
EXECUTE.

Related

I need to define a custom work week in MS access

I am trying to create a custom function on a form to define a week Number.
I have created a table that defines the week number.
Example WeekNo, StartDay, End Day
example: WeekNo 1 StartDay = 3/29/2020, End Day 4/4/2020
I have a Date box on my form if I enter a date of 3/29/2020
I would like 1 to be populated in my week number box.
On my form in the row source I have designed a Dlookup query
=DLookup("[WeekNumber]", "tblWeekNumber", "[Startdate] >= " & frmSearchNew.dt_Date & "") & [EndDate] <= frmSearchNew.dtDate
When I change to from view I get the error the record source specified on this form does not exist.
The table tblWeekNumber has the fields ID, WeekNo, StartDay and EndDay.
Where am I going wrong? any help is appreciated.
There are quite a few issues with the DLookup that you have put together.
Firstly, the field that you are looking for and the fields that you are using as criteria do not appear to match those in the table - WeekNumber/WeekNo, StartDate/StartDay, EndDate/EndDay;
Next, the logic for the lookup is wrong. You are trying to find a the week number that has a start date that is greater than the entered date, and an end date that is less than the entered date. What you should be looking for is a start date before the entered date, and an end date after the entered date.
Finally, dates are a bit funny in Access. You need to wrap them in '#' so that Access knows they are dates, and you should also take care to disambiguate them - 03/04/2020 could be either 3rd April or 4th March depending on you nationality.
Putting it all together, the final control source should look like:
=DLookUp("WeekNo","tblWeekNumber","StartDay<=#" & Format([dt_Date],"dd-mmm-yyyy") & "# AND EndDay>=#" & Format([dt_Date],"dd-mmm-yy") & "#")
Regards,

SPSS - pass values of three dates fields into file name

My dataset has three date fields (adate10): Period, Version, and Created. The values for each field are identical for all cases. Let's say the values are:
Period = 10/01/17
Version = 11/15/17
Created = 11/25/17
I would like to pass all three dates into a SAVE OUTFILE='C:\Users\Inventory ? ? ?... command so the file name is:
C:\MyData\Inventory p20171001 v20171115 c20171125.sav
How I can pass the values of these three date fields into the save command? I assume Python is the way to go, but cannot figure out the syntax. I see several solutions at the link below, but they all involve only one date field; I have three. (And even when I tried with one date I couldn't get any of them to work).
http://spssx-discussion.1045642.n5.nabble.com/include-value-from-variable-in-filename-when-saving-td4326879.html
Here you go with a little Python:
BEGIN PROGRAM.
import spss, spssdata
# names of date variables (case sensitive)
varlist = ['period', 'version', 'created']
# fetch dates from dataset and format them to YYYYMMDD
data = spssdata.Spssdata(indexes=(varlist), cvtDates=(varlist))
dates = [date.strftime("%Y%m%d") for date in data.fetchone()]
data.close()
# Create 'save command' command and submit it to spss
spss.Submit(r"""
SAVE OUTFILE = 'C:\MyData\Inventory p{0} v{1} c{2}.sav'.
""".format(*dates))
END PROGRAM.
You can get the results you need with syntax only:
TEMPORARY.
select if $casenum=1.
format Period Version Created (f11).
write out="C:\MyData\save with dates in name.sps"
/"save out='C:\MyData\Inventory p", Period, " v", Version, " c", Created, ".sav' ".
exe.
insert file="C:\MyData\save with dates in name.sps".

Parse week strings for comparison using Java 8

I want to compare string representations of weeks, e.g. week "01/17" is before "02/17" and after "52/16".
The following code throws an exception, I guess because my string doesn't hint at the exact day of each week. However, I don't care - it could all be Mondays or Thursdays or whatever:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ww/YY", Locale.GERMANY);
LocalDate date1 = formatter.parse(str1, LocalDate::from);
Do I need to modify the parser? Or parse to some other format? Unfortunatley there is no object like YearMonth for weeks...
One solution would be to always default to the same day, say the Monday. You could build a custom formatter for that:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.appendPattern("ww/YY")
.parseDefaulting(ChronoField.DAY_OF_WEEK, 1)
.toFormatter(Locale.GERMANY);
You can now build LocalDates representing the Monday of the given week:
LocalDate d1 = LocalDate.parse("01/17", fmt);
LocalDate d2 = LocalDate.parse("52/16", fmt);
System.out.println(d1.isAfter(d2));
which prints true because 01/17 is after 52/16.
I wasn't able to find a way for this to work with the DateTimeFormatter class, but I would like to suggest a different approach.
The Threeten Extra library contains a number of classes that were deemed too specific to include in the java.time library. One of them is the YearWeek class you mention.
Your problem can be solved by parsing the week-number and year manually from the input-string and then invoking the YearWeek creator-method like this:
YearWeek yw = YearWeek.of(year, monthOfYear);
tl;dr
YearWeek.parse( "2017-W01" )
ISO 8601
Or parse to some other format?
Yes, use another format.
Use the standard ISO 8601 formats when serializing date-time values to text. The standard includes support for week dates.
For a year-week that would be four year digits, a hyphen, a W, and two digits for the week of the year.
2017-W01
Get clear on your definition of a “week”. The ISO 8601 definition is that:
The Week # 1 contains the first Thursday of the year, and
Runs Monday-Sunday.
So years run either 52 or 53 weeks long. And note that under this definition, the first few days of the year may be in the prior year when week-numbering. Likewise, the last few days of the year may be in the following year when week-numbering.
If you want to indicate a particular day within that week, append a hyphen and a single digit running 1-7 for Monday-Sunday.
Tip: To see ISO 8601 week numbers by default on your computer, you may need to adjust your OS setting. For example, on macOS set System Preferences > Language & Region > Calendar > ISO 8601 to make apps such as Calendar.app to display week numbers with this standard definition.
2017-W01-7
By the way, a couple of similar representations:
An ordinal date meaning the year and the day-of-year-number running from 1-366 is year, a hyphen, and a three-digit number: 2017-123
Month-Day without year is two hyphens, month number, hyphen, and day-of-month number: --01-07
Note that the use of Locale as seen in the Question is irrelevant here with the standard ISO 8601 formats.
YearWeek
Unfortunatley there is no object like YearMonth for weeks...
Ahhh, but there is such a class.
For a class to directly represent the idea of a week-year, see the correct Answer by Henrik. That Answer shows the ThreeTen-Extra library’s class YearWeek.
The YearWeek class can directly parse and generate strings in standard format.
YearWeek yw = YearWeek.parse( "2017-W01" );
You can compare the YearWeek objects with methods: compareTo, equals, isBefore, isAfter.
yw.isBefore( thatYw )
The ThreeTen-Extra project offers other classes such as YearQuarter that you may find useful.

TABLEAU: calc field to get the last value available

I'm using Tableau Desktop, my data are like this:
KPI,date,monthValue
coffee break,01/06/2015,10.50
coffee break,01/07/2015,8.30
and I want to build a table like this
KPI, year(date), last value
coffee time, 2015, 8.30
How can I set a calculated field in order to show me the last value available in that year? I tried to do:
LOOKUP([MonthValue], LAST())
But it didn't work and tells me 'cannot mix aggregate and non-aggregate', so I did:
LOOKUP(sum([MonthValue]), LAST())
But it didn't work too. How should I proceed?
If you are using Tableau 9 then you can do this with an LOD calc that looks for the max value in your date field and then checks if the current date value is the same as the max date value.
[Date] == {fixed: max([Date])}
As you can see in the example below when you use the calc as a filter you will only get the last row from your example above.
UPDATE: to get the values per year you can do something like:
Here I am using a table calculation to find the max date per year and then ranking those dates and filtering down to the latest date in each year (which will be the one that has a rank equal to 1).
!max date is WINDOW_MAX(ATTR(Date))
!rank is RANK(Date)
You need to make sure that the table calculations are computer in the correct way (in this case across the values of each year).

I'm getting "Invalid month in date" trying to run this?

I'm trying to run the following db command against Informix:
delete from table1
where u_id in (select u_id
from table2
where c_id in (select c_id
from ptable
where name = 'Smith'
and dob = '29-08-1946'));
I pass this in as a string to the db.ExecuteNonQuery method in the MS Data Application block and I get the above error?
To get the date format '29-08-1946' to work, you need your DBDATE environment variable set to a value such as "DMY4-" (or "DMY4/"). These are standard variations for the UK (I used them for years; I now use "Y4MD-" exclusively, which matches both ISO 8601:2004 (Date formats) and ISO 9075 (SQL), except when debugging someone else's environment). There are other environment variables that can affect date formatting - quite a lot of them, in fact - but DBDATE takes priority over the others, so it is the big sledgehammer that fixes the problem.
One of the problems is that your notation using a plain string is not portable between US and UK (and ISO) settings of DBDATE. If you have a choice, the neutral constructor for dates is the MDY() function:
WHERE dob = MDY(8,29,1946)
This works regardless of the setting of DBDATE. You can probably use TO_DATE() too:
SELECT TO_DATE('29-08-1946', '%d-%m-%Y') FROM dual;
This generated '1946-08-29 00:00:00.00000' for me - the function generates a DATETIME YEAR TO FRACTION(5) value, but those convert reliably to DATE values in Informix.
You can also use the DATE() function or an explicit cast to DATE (either CAST('29-08-1946' AS DATE) or '29-08-1946'::DATE), but both of those are subject to the whims of the locale of the users.
Your date field is improperly formatted. Since there is no 29th month in the year 1946 that is what is causing the error.
I'd try just swapping the month and day. 08-29-1946.
The way the day and month parts of a date string are read in can depend on your computer's culture settings.
It is always safer to pass date strings to a database in the form 'dd-MMM-yyyy' (i.e. '29-aug-1946')
It's even safer to pass them as YYYY-MM-DD, the dd-MMM-yyyy in that example will fail on a server with a (for example) French locale.

Resources