We run our database queries with macros. Since we want to run queries every day (for the past day), we would need a dynamic timestamp within the macro. Therefore we tried to combine macro with python by using the command spss.SetMacroValue. But this does not work. What is wrong - or is there any other solution instead of python/macro?
(solutions in SQL slow down the query)
Code:
begin program.
import datetime, string, spss
when = datetime.datetime.today()
first = datetime.date(day=1, month=when.month, year=when.year)
firstThisMonth=first.strftime('%Y-%m-%d %H:%M:%S')
prev_month_end = first - datetime.timedelta(days=1)
prev_month_start = datetime.date(day=1, month= prev_month_end.month, year=
prev_month_end.year)
firstDay=prev_month_start.strftime('%Y-%m-%d %H:%M:%S')
spss.SetMacroValue("!timestamp1", "'" + firstDay+ "'")
spss.SetMacroValue("!timestamp2", "'" +firstThisMonth+ "'")
end program.
DEFINE macdef1 (arg_macdef1 = !TOKENS(1)
/ arg_macdef2 = !TOKENS(1)
/ arg_macdef3 = !TOKENS(1)).
GET DATA
/TYPE=ODBC
/CONNECT= !arg_macdef1
/SQL=
(...)
*deliveryDate >= !timestamp1 and deliveryDate < !timestamp2;'*
(...)
/ASSUMEDSTRWIDTH=255.
CACHE.
EXECUTE.
DATASET NAME !arg_macdef2 WINDOW=FRONT.
SAVE OUTFILE=!arg_macdef3.
!ENDDEFINE.
macdef1 arg_macdef1 = 'DSN=www1;' arg_macdef2 = www1 arg_macdef3 =
'E:\www1.sav'.