Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Generating unique values from unique and duplicate cases

From   "Alvarez,Sergio" <>
To   <>
Subject   Re: st: Generating unique values from unique and duplicate cases
Date   Mon, 17 Oct 2011 11:35:23 -0400


I don't know if this is what you need, but I know this will give you unique values for each value of 'dup':

sort dup id
by dup: gen new_id = _n

then if you sort by id I think you'll get the observation in the order you want.

hope it works,


On Mon, 17 Oct 2011 15:49:42 +0100, Tim Morris wrote:

I have created syntax to check for and label potential duplicate
cases across 3 variables (two text and one numeric). the syntax is as

sort var1 var2 var3
quietly by  var1 var2 var3:  gen dup = cond(_N==1,0,_n) if var1!=. |
var2!="" | var3!=""

this results in a new variable (dup) which may read as follows
through the cases: 0, 0, 0, 1, 2, 0, 0 (1 and 2 being duplicate cases
the rest being unique). What i want to do is create a new variable
(id) that assigns a unique id to each unique case and groups together
the corresponding duplicates into the same id, so based upon the
example above the results will be along the lines of:

dup	id
0 	1
0 	2
0 	3
1 	4
2 	4
0 	5
0 	6

I have played around with various code, searched online and spoken to
other STATA users for help but cannot find a way to make STATA assign
unique values for each 'group' of duplicates. Thanks in advance for
any help.

tim morris

Tim Morris, ALSPAC
0117 331 0022

*   For searches and help try:

Sergio Alvarez
Food and Resource Economics
University of Florida
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index