* (Q) Data file contains a variable named varx which contains date and time values We want to create a new variable (named newvar) whose value for each case will equal the earliest value of varx in the 7 days preceding the value of varx for the given case. * (A) posted to newsgroup by rlevesque@videotron.ca on 2001/08/13. * visit my SPSS site http://pages.infinit.net/rlevesqu/. * generate 100 random dates and time between 1/1/2001 and 31/3/2001. NEW FILE. INPUT PROGRAM. LOOP #i = 1 TO 100. COMPUTE datetime = RV.UNIFORM(DATE.DMY(1,1,2001),DATE.DMY(31,3,2001)). END CASE. END LOOP. END FILE. END INPUT PROGRAM. EXECUTE. COMPUTE date1=XDATE.DATE(datetime). COMPUTE time1=XDATE.TIME(datetime). COMPUTE varx=date1 + time1. FORMATS varx datetime (DATETIMEW19) date1(ADATE10) time1(TIME5). VARIABLE WIDTH varx datetime(17) time1(6) date1(11). ** The purpose of the above is to create a sample data file The program assumes that the variable varx includes both the date and time If this is not the case, add them up as I did above. SORT CASES BY varx. RANK varx /N into n. * N contains the number of cases in the file. * make a submacro to be invoked from the main macro. DO IF $CASENUM=1. WRITE OUTFILE 'c:\\temp\\temp.sps' /"DEFINE !n()"n"!ENDDEFINE.". END IF. EXE. INCLUDE FILE="c:\\temp\\temp.sps". /* The number of cases in the file is now accessible using !n */. * Next line is just to allow you to see what's going on. Replace yes by no if not needed. SET MPRINT=yes. *///////////////////////////////. DEFINE !findmin(myvar=!TOKENS(1)) STRING vnames(A8). COMPUTE vnames=CONCAT('v',LTRIM(STRING($CASENUM,F7.0))). FLIP VARIABLES=varx /NEWNAME=vnames . VECTOR r(!n F8.0) /v=v1 TO !CONCAT('v',!n). * The next variable contains the number of seconds in 7 days. COMPUTE #seven = 7*24*60*60. * Next loop calculates the answers. LOOP #1=1 TO !EVAL(!n). + LOOP #2=#1 TO 1 BY (-1) IF v(#1)- #seven <= v(#2). + COMPUTE r(#1)=v(#2). + END LOOP. END LOOP. SAVE OUTFILE='c:\\temp\\all vars.sav'. * Purpose of remaining syntax is simply to place varx and newvar side by side. MATCH FILES FILE=* /KEEP=v1 TO !CONCAT('v',!n). FLIP. RENAME VARIABLES (var001 = varx). SAVE OUTFILE='c:\\temp\\varx.sav'. GET FILE='c:\\temp\\all vars.sav'. MATCH FILES FILE=* /KEEP=r1 TO !CONCAT('r',!n). FLIP. RENAME VARIABLES (var001 = newvar). MATCH FILES /FILE=* /FILE='C:\\Temp\\varx.sav' /RENAME (case_lbl = d0) /DROP= d0. MATCH FILES FILE=* /KEEP=varx newvar. FORMATS varx newvar (DATETIMEW19). VARIABLE WIDTH varx newvar(17). EXECUTE. !ENDDEFINE. ** Call the macro to do the job. !findmin myvar=varx.