f



Create groups based on date range between variables

Hi all!

I was wondering whether it was possible to create a group variable based on month, year and day between two variables in SPSS. 

For example, I have a dataset with a "START" and "END" variable. 

START                   END
01-Sep-2015 00:00:00	31-Mar-2016 00:00:00
5-Jul-2016 00:00:00	31-Oct-2016 00:00:00 
16-Jun-2016 00:00:00	6-Sept-2016 00:00:00 


I would like to create a "GROUP" variable and assign a value of 1 to dates that fall within the 01-Sep-2016 to 31-Mar-2016 range, and 2 to the others.

For example, I'm looking for a statement like: 

if (START=DATE.DMY(1,9,2016)) and (END=DATE.DMY(31-03-2016)) group=1.
execute. 

The above statement will likely error out, but if there's a function that can perform what I'm looking to do, I would greatly appreciate any suggestions.

Thanks!

J. 
0
jace
12/8/2016 5:28:57 PM
comp.soft-sys.stat.spss 5679 articles. 0 followers. Post Follow

2 Replies
399 Views

Similar Articles

[PageSpeed] 45

On Thursday, December 8, 2016 at 12:28:59 PM UTC-5, jace....@gmail.com wrote:
> Hi all!
> 
> I was wondering whether it was possible to create a group variable based on month, year and day between two variables in SPSS. 
> 
> For example, I have a dataset with a "START" and "END" variable. 
> 
> START                   END
> 01-Sep-2015 00:00:00	31-Mar-2016 00:00:00
> 5-Jul-2016 00:00:00	31-Oct-2016 00:00:00 
> 16-Jun-2016 00:00:00	6-Sept-2016 00:00:00 
> 
> 
> I would like to create a "GROUP" variable and assign a value of 1 to dates that fall within the 01-Sep-2016 to 31-Mar-2016 range, and 2 to the others.
> 
> For example, I'm looking for a statement like: 
> 
> if (START=DATE.DMY(1,9,2016)) and (END=DATE.DMY(31-03-2016)) group=1.
> execute. 
> 
> The above statement will likely error out, but if there's a function that can perform what I'm looking to do, I would greatly appreciate any suggestions.
> 
> Thanks!
> 
> J.

Does this do what you want?

* Read in some sample data.
DATA LIST LIST / START END (2DATE11).
BEGIN DATA
01-Sep-2016        31-Mar-2016
01-Oct-2016        28-Feb-2016
01-Sep-2015        31-Mar-2016
5-Jul-2016         31-Oct-2016
16-Jun-2016        6-Sept-2016
END DATA.

COMPUTE Group = START GE DATE.DMY(1,9,2016) AND END LE DATE.DMY(31,3,2016).
RECODE Group(0=2).
FORMATS Group(F1).
LIST.

OUTPUT:

      START         END Group

01-SEP-2016 31-MAR-2016   1
01-OCT-2016 28-FEB-2016   1
01-SEP-2015 31-MAR-2016   2
05-JUL-2016 31-OCT-2016   2
16-JUN-2016 06-SEP-2016   2

Number of cases read:  5    Number of cases listed:  5
0
Bruce
12/8/2016 6:20:51 PM
Works like a charm. Thanks so much Bruce!

On Thursday, December 8, 2016 at 10:20:53 AM UTC-8, Bruce Weaver wrote:
> On Thursday, December 8, 2016 at 12:28:59 PM UTC-5, jace....@gmail.com wrote:
> > Hi all!
> > 
> > I was wondering whether it was possible to create a group variable based on month, year and day between two variables in SPSS. 
> > 
> > For example, I have a dataset with a "START" and "END" variable. 
> > 
> > START                   END
> > 01-Sep-2015 00:00:00	31-Mar-2016 00:00:00
> > 5-Jul-2016 00:00:00	31-Oct-2016 00:00:00 
> > 16-Jun-2016 00:00:00	6-Sept-2016 00:00:00 
> > 
> > 
> > I would like to create a "GROUP" variable and assign a value of 1 to dates that fall within the 01-Sep-2016 to 31-Mar-2016 range, and 2 to the others.
> > 
> > For example, I'm looking for a statement like: 
> > 
> > if (START=DATE.DMY(1,9,2016)) and (END=DATE.DMY(31-03-2016)) group=1.
> > execute. 
> > 
> > The above statement will likely error out, but if there's a function that can perform what I'm looking to do, I would greatly appreciate any suggestions.
> > 
> > Thanks!
> > 
> > J.
> 
> Does this do what you want?
> 
> * Read in some sample data.
> DATA LIST LIST / START END (2DATE11).
> BEGIN DATA
> 01-Sep-2016        31-Mar-2016
> 01-Oct-2016        28-Feb-2016
> 01-Sep-2015        31-Mar-2016
> 5-Jul-2016         31-Oct-2016
> 16-Jun-2016        6-Sept-2016
> END DATA.
> 
> COMPUTE Group = START GE DATE.DMY(1,9,2016) AND END LE DATE.DMY(31,3,2016).
> RECODE Group(0=2).
> FORMATS Group(F1).
> LIST.
> 
> OUTPUT:
> 
>       START         END Group
> 
> 01-SEP-2016 31-MAR-2016   1
> 01-OCT-2016 28-FEB-2016   1
> 01-SEP-2015 31-MAR-2016   2
> 05-JUL-2016 31-OCT-2016   2
> 16-JUN-2016 06-SEP-2016   2
> 
> Number of cases read:  5    Number of cases listed:  5

0
jace
12/8/2016 7:55:51 PM
Reply: