You are on page 1of 2

HowdoIcreateindividualidentifiersnumberedfrom1upwards?

Title

Creatinggroupidentifiers

Author

NicholasJ.Cox,DurhamUniversity,UK
WilliamGould,StataCorp

Date

December1999minorrevisionsMarch2001

Case1.Iwanttocreatevariableidcontaining1,2,3,...
Type
.genid=_n
_nistheStatawayofreferringtotheobservationnumber.
Ina10observationdataset,_ntakesonthevalues1,2,...,10.

Case2.Ialreadyhaveanidvariable,andIhavemultipleobservationsper
id,butIwantanewidvariablecontaining1forthefirstid,2forthe
second,andsoon.
Suchquestionsoftenarisewithpaneldataandinothercircumstances.Perhapstheidentifiervariableisa
stringid"numbers"1A038,2B217,...andyouneednumericidentifiers1,2,...becausesome
Statacommandsrequirethem.Perhapstheoriginalidisnumericoftheform102938,149384,150394,
...butyouwanttodrawagraphusingtheidentifierasoneoftheaxesandwantthedatapointsequally
spaced.
Answer1.
Tocreateanewvariablenewidfromtheexistingvariableoldid,whetheroldidisstringornumeric,type
.egennewid=group(oldid)
>Thenewvariablenewidwillcontain1forthefirstvalueofoldid,2forthesecondvalue,andsoon.
Answer2.
Tocreateanewvariablenewidfromtheexistingvariableoldid,whetheroldidisstringornumeric,type
.sortoldid
.byoldid:gennewid=1if_n==1
.replacenewid=sum(newid)
.replacenewid=.ifmissing(oldid)
Bothanswersyieldthesameresults:thefourlinesofanswer2amounttowhategendoes.Itis,however,
worthunderstandinganswer2.
WestartwithexistingidentifierID,whichmaybeeitheranumericvariableorastringvariable.
.sortoldid
Thiscommandputstheobservationsintheorderofoldid.
.byoldid:gennewid=1if_n==1

Thiscommandcreatesanewvariablenewidthatis1forthefirstobservationforeachindividualand
missingotherwise._nistheStatawayofreferringtotheobservationnumberina10observation
dataset,_ntakesonthevalues1,2,...,10.When_niscombinedwithby,however,_nistheobservation
numberwithinbygroup,inthiscase,withinoldid.Iftherewerethreeoldid==1observationsfollowedby
twooldid==2observationsinthedataset,_nwouldtakeonthevalues1,2,3,1,2.Thus,by...:...if
_n==1isawaytorefertothefirstobservationineachbygroup.Seethesectionsof[U]indexed
underbyvarlist:prefix.
byoldid:gennewid=1if_n==1setsnewidto1inthefirstobservationofeacholdid.
.replacenewid=sum(newid)
Thiscommandreplacesnewidbyitscumulativeorrunningsum.
.replacenewid=.ifmissing(oldid)
Thiscommandputsmissingvalueintonewid,whereoldidcontainedmissingvalue.Thisstepisprobably
unnecessarybecauseifoldidreallyisanIDvariable,itshouldnevercontainmissinganyway.
Letusseehowthatworksforasimpledataset.Missingvalues(.)makenodifferencetoacumulativesum.
Inthatcontext,theyaretreatedasnumericallyequalto0.
oldidnewid(ascreated)newid(asreplaced)
111
1.1
1.1
1.1
2212
22.2
22.2
3313
33.3

You might also like