Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9094711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:24:50+00:00 2026-06-16T23:24:50+00:00

I have some data in Stata which look like the first two columns of:

  • 0

I have some data in Stata which look like the first two columns of:

group_id   var_to_rank  desired_rank
____________________________________

1           10          1
1           20          2
1           30          3
1           40          4
2           10          1
2           20          2
2           20          2
2           30          3

I’d like to create a rank of each observation within group (group_id) according to one variable (var_to_rank). Usually, for this purpose I used:

gen id = _n

However some of my observations (group_id = 2 in my small example) have the same values of ranking variable and this approach doesn’t work.

I have also tried using:

egen rank

command with different options, but cannot make my rank variables make to look like desired_rank.

Could you point me to a solution to this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T23:24:52+00:00Added an answer on June 16, 2026 at 11:24 pm

    I’d say this question is posed the wrong way round for best understanding. The aim is to group observations, those with the lowest value all being assigned a grade 1, the next lowest being all assigned 2 and so forth. This isn’t ranking in most senses that I have seen discussed, but Stata’s egen, rank() does get you part of the way.

    But the direct way, which was mentioned in the Statalist thread cited elewhere in this thread (start here) is simpler in spirit than any solution quoted:

    bysort group_id (var_to_rank): gen desired_rank = sum(var_to_rank != var_to_rank[_n-1]) 
    

    Once data are sorted on var_to_rank then when values differ from previous values at the start of each block of distinct values a value of 1 is the result of var_to_rank != var_to_rank[_n-1]; otherwise 0 is the result. Summing those 1s and 0s cumulatively gives the desired variable. The prefix command bysort does the sorting required and ensures that this is all done separately within the groups defined by group_id. No need for egen at all (a command that many people who only use Stata occasionally often find bizarre).

    Declaration of interest: The Statalist thread cited shows that when asked a similar question I too did not think of this solution in one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data from log files and would like to group entries by
I have some data which (quite reasonably) uses null and false for different meanings.
I have some data and need to create a json file with this structure
I have some data which i need to make some statistics. I need to
I have some data that I would like to visualize. Each byte of the
I have an html/javascript page that creates some data which is similar to a
I have developed REST web service which is able to accept some data, load
I have the need to create a database for some data I have, and
I have a fairly basic question regarding python lists/dictionaries which I would like some
I am using the sqlite v3, i have the table which has some columns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.