Search code examples
dateminpspp

how to find the minimum of several dates in PSPP


I'm trying to extract the minimal date out of several date columns in PSPP. Here is the syntax file I'm using:

***************************************
* [1] READ FORM CSV FILE :: input.csv *
***************************************
GET DATA
/TYPE=TXT
/FILE='input.csv'
/FIRSTCASE=2
/VARIABLES=
startDate1 ADATE8
endDate1   ADATE8
startDate2 ADATE8
endDate2   ADATE8
startDate3 ADATE8
endDate3   ADATE8
startDate4 ADATE8
endDate4   ADATE8
.

***********************************
* [2] DEFINE VARIABLE :: earliest *
***********************************
Variable earliest:
COMPUTE earliest = MIN(startDate1,startDate2,startDate3,startDate4).
EXECUTE.

**************************************
* [3] SAVE TO CSV FILE :: output.csv *
**************************************
SAVE TRANSLATE
/OUTFILE='output.csv'
/REPLACE
/TYPE=CSV

When I examine output.csv I see that a new column was indeed created, but instead of an mm/dd/yyyy pattern it's just some 11 digits numbers. Any help is very much appreciated, thanks!


Solution

  • Well I finally found it. Not so straight forward, but still working. The catch is that you have to convert a date to an integer and only then take the minimum value.

    ***************************************
    GET DATA
    /TYPE=TXT
    /FILE='input.csv'
    /FIRSTCASE=2
    /VARIABLES=
    startDate1 ADATE8
    endDate1   ADATE8
    startDate2 ADATE8
    endDate2   ADATE8
    startDate3 ADATE8
    endDate3   ADATE8
    startDate4 ADATE8
    endDate4   ADATE8
    .
    
    *******************************************************************
    Variable myDate1:
    COMPUTE myDate1=XDATE.YEAR(startDate1)*1000+XDATE.JDAY(startDate1).
    EXECUTE.
    *******************************************************************
    Variable myDate2:
    COMPUTE myDate2=XDATE.YEAR(startDate2)*1000+XDATE.JDAY(startDate2).
    EXECUTE.
    *******************************************************************
    Variable myDate3:
    COMPUTE myDate3=XDATE.YEAR(startDate3)*1000+XDATE.JDAY(startDate3).
    EXECUTE.
    *******************************************************************
    Variable myDate4:
    COMPUTE myDate4=XDATE.YEAR(startDate4)*1000+XDATE.JDAY(startDate4).
    EXECUTE.
    *******************************************************************
    Variable earliest:
    COMPUTE earliest = MIN(myDate1,myDate2,myDate3,myDate4).
    EXECUTE.
    
    **************************************
    SAVE TRANSLATE
    /OUTFILE='output.csv'
    /REPLACE
    /TYPE=CSV