Filtering for First X Cases of a Customer

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

XFactorer
Posts: 3
Joined: Fri Sep 14, 2012 9:16 pm

Filtering for First X Cases of a Customer

Postby XFactorer » Fri Sep 14, 2012 9:36 pm

Here's what I have:

Customer... Date... Purchase
A... 1/1/2010... $12
A... 1/2/2010... $12
A... 1/3/2010... $14
A... 1/4/2010... $167
A... 1/5/2010... $19
A... 1/6/2010... $80
B... 1/7/2010... $55
B... 1/8/2010... $78
B... 1/9/2010... $8
B... 2/2/2010... $12
...

I want to select JUST the FIRST 3 purchases for each customer and do analysis with that. Is this a simple SELECT IF command (if so, I don't see what I would be SELECTING unless I number each customer's case)?

Thanks in advance!
Penguin_Knight
Posts: 473
Joined: Thu Apr 05, 2012 5:58 pm

Re: Filtering for First X Cases of a Customer

Postby Penguin_Knight » Sat Sep 15, 2012 4:16 am

Use this syntax:

Code: Select all

SORT CASES BY customer(A) date(A).

IF $casenum = 1 seq = 1 .
EXECUTE .

IF customer NE LAG(customer) seq = 1 .
EXECUTE .

IF SYSMIS(seq) & customer EQ LAG(customer) seq = LAG(seq) + 1 .
EXECUTE .
And then you can use select cases function to select "seq less than or equal 3" for your analysis.
XFactorer
Posts: 3
Joined: Fri Sep 14, 2012 9:16 pm

Re: Filtering for First X Cases of a Customer

Postby XFactorer » Sat Sep 15, 2012 8:06 pm

Thanks! Wish it weren't that difficult. I'll try it tomorrow and report my findings.

I was thinking I'd have to just number the cases w/ LAG.
XFactorer
Posts: 3
Joined: Fri Sep 14, 2012 9:16 pm

Re: Filtering for First X Cases of a Customer

Postby XFactorer » Mon Sep 17, 2012 5:23 pm

On a related note, by is there the dollar sign in front of casenum? It looks to not have saved like 'seq' does. Is it a an SPSS marker for "This is a temporary variable"? I know its use in Excel.
Penguin_Knight
Posts: 473
Joined: Thu Apr 05, 2012 5:58 pm

Re: Filtering for First X Cases of a Customer

Postby Penguin_Knight » Mon Sep 17, 2012 8:31 pm

XFactorer wrote:On a related note, by is there the dollar sign in front of casenum? It looks to not have saved like 'seq' does. Is it a an SPSS marker for "This is a temporary variable"? I know its use in Excel.
Yes, it's a special reserved variable in SPSS. When you put $ in front of the variable, the variable will be calculated and used in the processed but will not stay in the data sheet. This is called scratch variable.

Among the scratch variables, there are a few reserved keywords that are associated with a particular function. $casenum is one of them, it's pretty much the number of case. It's pretty useful if you just want to make a sequence in a data set. Another way of using it is:

Code: Select all

COMPUTE myUniqueID = $casenum .
EXECUTE .
And you'll instantly get a column of unique IDs.

Who is online

Users browsing this forum: No registered users and 2 guests

cron