Rank within variable based on order

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

GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

Rank within variable based on order

Postby GerineL » Thu Oct 04, 2012 1:01 pm

I have data like this:

V1..........V2
1............3
1............2
1............4
1............4
2............1
2............3
2............3

What I want is this:

V1..........V2..............V3
1............3................2
1............2................1
1............4................3
1............4................4
2............1................1
2............3................2
2............3................3

Which is:
- a Rank of V2 within V1
- if there are 2 or more identical values of V2 within one case (within one V1), the first occurance of that rank gets the lower rank.

Can anyone help me with that?
apeape
Posts: 56
Joined: Mon May 02, 2011 6:07 pm

Re: Rank within variable based on order

Postby apeape » Fri Oct 05, 2012 6:18 am

This should do what you want:

Code: Select all

data list list / v1 v2.
begin data
1   3
1   2
1   4
1   4
2   1
2   3
2   3
end data.

compute tempid = $casenum.

sort cases by v1 (a) v2 (a).

rank variables = v2(a) by v1
/rank
/ties = low.

if (lag(v2) = v2) and (lag(v1) = v1) rv2 = lag(rv2)+1.

formats rv2 (f2).

sort cases by tempid(a).
delete variables tempid.

exe.


Who is online

Users browsing this forum: No registered users and 1 guest

cron