You are on page 1of 10

12/29/2016

StataProgrammingEssentials

StataProgrammingEssentials
Everneededtodothesamethingtotendifferentvariablesandwishedthatyoudidn'thavetowriteit
outtentimes?Ifso,thenthisarticleisforyou.Ifnot,somedayyouwillsoyoumightaswellkeep
readinganyway.
Statahasallthetoolsrequiredtowriteverysophisticatedprograms,butknowingjustafewofthem
allowsyoutomakeeverydaydofilesshorterandmoreefficient.Thisarticlewillfocusonthose
programmingtoolsthat,inourexperience,anyonewhousesStataheavilywilleventuallywanttolearn.
Tobenefitfromthisarticleyou'llneedasolidunderstandingofbasicStatasyntax,suchasyoucanget
fromourStataforResearchersseries.TheprimaryintendedaudienceisStatauserswithnoother
programmingexperience.Ifyou'vedonealotofStataprogrammingalreadyandarelookingtoexpand
your"bagoftricks"checkoutStataProgrammingTools.
ThisarticleisbestreadatthecomputerwithStatarunning.Typingthecommandsintheexamples
yourselfwillhelpyounoticeandretainallthedetails,andprepareyoutowriteyourowncode.

Macros
AStatamacroisaboxyouputtextin.Youthenusewhat'sintheboxinsubsequentcommands.(The
realtrickisgettingasinglecommandtorunmultipletimeswithadifferentbitoftextintheboxeach
timewe'llgetthere).
Themacroswe'lluseare"local"macros.Ifyou'refamiliarwithglobalandlocalvariablesfromother
languages,Stata'slocalmacrosarelocalinthesameway.Ifnot,justtrustusthatlocalmacrosarethe
rightonestouse.
Thecommandtodefinealocalmacrois:

localnamevalue
Forexample:

localx1
Thiscreatesalocalmacrocalledxandputsthecharacter'1'init(notthevalue1asin"oneunittothe
rightofzeroonthenumberline").Touseamacro,youputitsnameinacommand,surroundedbya
particularsetofquotationmarks:

display`x'
Thequotebeforethexistheleftsinglequote.Itisfoundintheupperleftcornerofthekeyboard,under
thetilde(~).Thequoteafterthexistherightsinglequote.Itisfoundunderthedoublequotationmark
(")ontherightsideofthekeyboard.
MacrosarehandledbyamacroprocessorthatexaminescommandsbeforepassingthemtoStataproper.
Whenitseesamacro(denotedbythatparticularsetofquotationmarks)itreplacesthemacrowithits
table.ThuswhatStatapropersawwas:

display1
Nowtryaslightlymorecomplicatedmacro:
http://www.ssc.wisc.edu/sscc/pubs/stata_prog1.htm

1/10

12/29/2016

StataProgrammingEssentials

localx2+2
display`x'
Theresultis4,butthat'sbecausethedisplaycommandactslikeacalculator.ThecommandStatasaw
was:

display2+2
soitevaluated2+2andgaveyoutheanswer.Ifyouwantdisplaytoputsomethingonthescreen
withoutevaluatingit,putitinquotes.Thendisplaywilltreatitlikeastring.

display"`x'"
givestheresult2+2.Butconsiderwhathappenedbeforeyouputitinquotes:yourmacrocontaineda
workingbitofStatacodewhichStatahappilyexecutedwhenyouusedit.InfactStataproperdidn'tknow
orcarethat2+2camefromamacro.Thisfeatureallowsyoutousemacrosabsolutelyanywhere,even
inmacrodefinitions.

StoringResultsinMacros
Ifyouwanttoputtheresultofacalculationinamacro,putanequalssignafterthemacroname:

localx=2+2
display"`x'"
Ifthelocalcommandcontainsanequalssign,Statawillevaluatewhatfollowsbeforeputtingitinthe
macro.Nowxreallydoescontain4andnot2+2nomatterhowyoudisplayit.

MacroExpressions
Stata'smacroprocessorcanevaluateStataexpressionsi.e.anyformulayoucouldputaftertheequals
signinagenerateorreplacecommand(butnotegen).Thesyntaxis:

`=expression'
whereexpressionistheexpressiontobeevaluated.Try:

display"`=2+2'"
Theresultis4,butdisplaydidn'tcalculateit(thequotespreventthat).Instead,theequalssign
before2+2toldthemacroprocessortoevaluatethatexpressionandputtheresultinthecode,sowhat
Statapropersawwasdisplay"4".Anothercommonuseis`=_N',whichwillbethenumberof
observationsinthecurrentdataset(andcanbeusedinplaceswhere_Nbyitselfcan't).
Macroexpressionsandmacrosingeneralcancontainothermacros.Try:

display"`=`x'1'"
Thistellsthemacroprocessortosubtractonefromthevalueofthemacroxandthenplacetheresultin
thecode.Thiscanbeextremelyuseful:forexample,ifyouhadamacro`year'containingthecurrent
year,`=`year'1'wouldbetheyearbeforethecurrentyear.

UndefinedMacros
Unfortunately,usingamacroyouhaven'tdefineddoesn'tgenerateanerrormessage.Stata'smacro
processorjustreplacesitwithnothing:

display`y'
Givesthesameresultas:

display
http://www.ssc.wisc.edu/sscc/pubs/stata_prog1.htm

2/10

12/29/2016

StataProgrammingEssentials

Thiscancauseheadaches:ifyoumistypeamacro'snameyou'llprobablygetagenericsyntaxerrorwith
noindicationthatamacroisthecauseoftheproblem.Evenworse,insomecircumstancesthecommand
willstillworkbutgiveincorrectresults.Beverycarefultotypethenamesofmacrosproperly.

SomeUsesforMacrosOutsideofLoops
Themainreasonforlearningaboutmacrosissoyoucanusetheminloops.Buttherearetimeswhen
usingthemallbythemselvescanmakecomplexcodeeasiertoread.
Supposeyouneedtorunalargenumberofregressionsofvarioustypes,buttheyallincludeafixedsetof
controlvariables.Considerputtingthelistofcontrolvariablesinamacro:

localcontrolVarsagesexoccupationlocationmaritalStatus
hasChildren
Thiswillmaketheregressioncommandsshorter:

regincomeeducation`controlVars'
logitemployededucation`controlVars'
Nowsupposeyoufrequentlyworkwithsubsamplesofyourdataset.Youcandefinemacrosforthemas
well:

localblackWomanrace==1&female
localhispManrace==2&!female
regincomeeducation`controlVars'if`blackWoman'
logitemployededucation`controlVars'if`hispMan'
Thepointhereisnottosavekeystrokes,buttomakethecodemoreclear.Usingmacroshidesthedetails
ofwhatthecontrolvariablesareorhowablackwomancanbeidentifiedinthisdatasetandhelpsyou
focusonwhatyou'retryingtodo.Nothavingtotypeoutthosedetailseverytimealsoremovesan
opportunityforerror.Youcanmakechangesmorequicklytoo:ifyouneedtoaddacontrolvariableyou
onlyhavetoaddittothedefinitionofthecontrolVarsmacroratherthanaddingittoeach
regressioncommand.
Savingkeystrokesisanicesideeffect,butresistthetemptationtomakeyourcodelessclearinthename
ofmakingitshorter.Takingafewminutestotypeoutclearcodeisfarmoreefficientthanspending
hoursdebuggingcodethat'sshortbuthardtounderstand.

ForLoops
Aforeachlooptakesalistandthenexecutesacommandorsetofcommandsforeachelementofthe
list.Theelementcurrentlybeingworkedonisstoredinamacrosoyoucanrefertoitinthecommands.
Thelisttobeloopedovercanbeagenericlistcontainingtext,orthereareseveralkindsofstructured
lists(we'llonlydiscussvarlists).
Thesyntaxforaforeachloopwithagenericlistis:

foreachmacroinlist{
command(s)
}
Asaverysimpleexample:

foreachcolorinredbluegreen{
display"`color'"
}

http://www.ssc.wisc.edu/sscc/pubs/stata_prog1.htm

3/10

12/29/2016

StataProgrammingEssentials

Here,coloristhenameofthemacrothatwillcontainthelistelements.redbluegreenisthe
listitself.Statabreaksthelistintoelementswhereveritseesspaces,sothislistcontainsthreeelements:

red,blue,andgreen.Theleftcurlybracket({)marksthebeginningoftheloopandmustbeat
theendoftheforeachcommand.Therightcurlybracket(})markstheendoftheloopandmustgo
onitsownline.IfyoutypethisininteractiveStatatheResultswindowaddslinenumbersforthe
commandsinsidetheloop,butyoudonotneedtotypethem.Notehownothingisactuallyexecuted
untilyoutypetherightcurlybracket,andthenStatarunsthewholething.Whenitdoesyou'llgetthe
followingoutput:
red
blue
green
Statabeginsbyanalyzingyourlistandidentifyingtheelementsitcontains.Itthenputsthefirstelement
(red)intheloop'smacro(color)andexecutesthecommandintheloop.Giventhetablesofcolor,
thecommandbecomesdisplay"red"andredisprintedonthescreen.Statathenputsthe

secondelementinthemacroandrunsthecommandagain,printingblueonthescreen.Itthenrepeats
theprocessforgreen,andwhenthat'sdoneStatarealizesthelistisoutofelementsandtheforeach
loopiscomplete.
Throughoutthisarticleyou'llseethatcommandswhichareinsidealoopareindented.Thismakesthe
loop'sstructurevisuallyobviousandwehighlyrecommendyoudothesamewhenwritingdofiles.Allyou
needtodoispressTabbeforeyoubeginthefirstlineoftheloop.Stata'sdofileeditorandanyothertext
editorsuitableforprogrammingwillindentsubsequentlinesautomatically.(There'snoneedtoworry
aboutindentingwhenworkinginteractively,butinrealworkit'sveryraretouseloopsinteractively.)
Youcanuseagenericlisttoloopovermanydifferentkindsofthings:variables,values,files,subsamples,
subscripts,anythingyoucandescribeusingtext.Ifanelementneedstocontainspaces,putitinquotes.

LoopingoverVariables
Themostcommonthingtoloopoverisvariables.Forexample,supposeyouwantedtoregressseveral
differentdependantvariablesonthesameindependentvariables.Thefollowingcodedoesso,usingthe
automobileexampledatasetthatcomeswithStata:

sysuseauto
foreachyvarinmpgpricedisplacement{
reg`yvar'foreignweight
}
LoopingoverPartsofVariableNames
Considerthefollowingdataset:

usehttp://www.ssc.wisc.edu/sscc/pubs/files/stata_prog/months.dta
Itcontainsafictitious(andnotterriblyplausible)datasetofpeopleandtheirincomesovertwelve
months.Thisispaneldatainthewideform,sotherearetwelveincomevariables:incJan,incFeb,
incMar,etc.Supposeyouwanttocreateacorrespondingsetofindicatorvariablesforwhetherthe
personhadanyincomeinthatmonth.Creatingoneofthemisstraightforward:

genhadIncJan=(incJan>0)ifincJan<.
butcreatingalltwelveinthesamewaywouldbetedious.
(Ifyouchecked,you'dfindthatthisdatasetdoesnothaveanymissingvaluessoexcludingthemwith
ifincJan<.isnotstrictlynecessary.Consideritaremindertoalwaysthinkaboutmissingvalues
whencreatingsuchindicatorvariables.)

http://www.ssc.wisc.edu/sscc/pubs/stata_prog1.htm

4/10

12/29/2016

StataProgrammingEssentials

Youcancreatealltwelveindicatorvariablesquicklyandeasilywithaforeachloop:

foreachmonthinJanFebMarAprMayJunJulAugSepOctNovDec{
genhadInc`month'=(inc`month'>0)ifinc`month'<.
}
Thissetsupagenericlistcontainingthemonths,andthenusesthosemonthsaspartsofvariable
names.
Notetheprocessweusedtocreatethisloop:firstwefiguredoutthecommandwe'duseforasingle
elementofthelistandthenchangedittousemacros.Thisisagoodhabitwheneveryouneedtowrite
nontrivialcodeinvolvingmacros.

LoopingoverVarlists
Whilegenericlistscancontainvariablenames,youhavetotypeoutallthenamesindividually.Ifyoutell
StatathatthelistyouwanttoloopoverisanofficialStatavarlistyoucanusestandardvarlistshortcuts,
likex*forallvariablesthatbeginwithxandxzforallthevariablesfromxtoz.Toreviewvarlist
syntax,seetheappropriatesectioninStataforResearchers.
Thesyntaxforaforeachloopoveravarlistisasfollows:

foreachmacroofvarlistvars{
Notethatwhiletheforeachsyntaxforagenericlistcontainsin,thesyntaxforastructuredlisthas

of.Statausestheinoroftodeterminewhetherthenextwordisthefirstelementofthelistora
typeoflist.

Researchersoccasionallyreceivedatasetscreatedinotherprogramswherethevariablenamesarein
uppercaseletters.SinceStataactuallycaresaboutcase,uppercasevariablenamescanbetiresometo
workwith.Statarecentlygavetherenamecommandtheabilitytoconvertnamestolowercase:

rename*,lower
Butthissuchagreatexamplethatlet'sdoitwithaforeachloopoveravarlistanyway:

foreacholdnameofvarlist*{
localnewname=lower("`oldname'")
rename`oldname'`newname'
}
Theasterisk(*)allbyitselfmatchesallvariables,sothelistforeachistoloopovercontainsallthe
variablesinthecurrentdataset.Thelower()functiontakesastring,inthiscasethetablesofthe
macrooldname,andconvertsittolowercase.Notetheuseoftheequalssigninthelocal
commandthatdefinesnewname,sothatlower("`oldname'")isevaluatedandtheresultis
stored.

LoopingoverNumbers
Aforvaluesloop(frequentlyabbreviatedforval)loopsovernumbers.Ratherthandefiningalist,
youdefinearangeofnumbers.
Byfarthemostcommonrangeconsistsofastartingnumberandanendingnumber,andStataassumes
itshouldcountbyonesbetweenthem.Thesyntaxissimply:

forvaluesmacro=start/end{
Forexample:

forvaluesi=1/5{
display`i'
http://www.ssc.wisc.edu/sscc/pubs/stata_prog1.htm

5/10

12/29/2016

StataProgrammingEssentials

}
givestheoutput:
1
2
3
4
5
Ifyouneedtocountinadifferentway,typehelpforvaluestoseemoreoptions.
Considerthefollowingdataset:

usehttp://www.ssc.wisc.edu/sscc/pubs/files/stata_prog/years.dta
Thisdatasetisverysimilartothedatasetofmonthlyincomesweexaminedearlier,butitcontains
yearlyincomesfrom1990to2010.Yourtaskisagaintocreateanindicatorforwhetherapersonhad
anyincomeinagivenyear.Usingforvaluesthisisveryeasytodo:

forvaluesyear=1990/2010{
genhadInc`year'=(inc`year'>0)ifinc`year'<.
}
Thiswouldbemoredifficultiftheyearsdidnotincludethecentury(i.e.90insteadof1990)because

Statathinks100shouldcomeafter99andnot00.Ifyourdataincludesuchyears,consideraddingthe
centurybeforedoinganyseriousworkwithit.

LoopingoverValuesandlevelsof
Sometimesyouneedtoloopoverthevaluesaparticularvariabletakeson.Considerthefollowingdata
set:

usehttp://www.ssc.wisc.edu/sscc/pubs/files/stata_prog/vals.dta
Thiscontainsdataontherace,income,ageandeducationcategoryofasetoffictionalpeople.Suppose
youwanttoregressincomeonageandeducation,butbelievethattheeffectsofageand
educationmaybedifferentforpeopleofdifferentraces.Oneapproach(probablynotthebestone)would
betorunaseparateregressionforthepeopleofeachrace.Normallyyoucoulddothatwith:

byrace:regressincomeagei.education
(Theconstructioni.educationtellsStatathateducationisafactororcategoricalvariableand
shouldbeconvertedintoasetofindicators.SeethesectiononfactorvariablesinStataforResearchersif
you'dliketoreviewfactorvariablesyntax.)
However,thisisfictionalsurveydataandyouneedtocorrectforthesurveydesigninrunning
regressions.Ifyou'renotfamiliarwithStata'ssurveycommands,thatmeansthefollowing:
1.Thesurveydesignisdescribedusingthesvyset(surveyset)command.Thisdatasethas
primarysamplingunitsgivenbythevariablepsuandprobabilityweightsgivenbythevariable
weight.Thecorrespondingcommandsvysetcommand(whichhasalreadybeenrunsoyou
don'tneedto)is:

svysetpsu[pweight=weight]

2.TohaveStatacorrectforthoseweightsinestimationcommands,addthesvy:prefix,for
example:

svy:regressincomeagei.education

3.Youcan'tusethestandardifsyntaxwithsurveydataortheweightsmaynotbeappliedcorrectly.
Instead,usethesubpop()optionofsvy:,forexample:

svy,subpop(ifrace==1):regressincomeagei.education

by:can'tbeusedwithsvy:
http://www.ssc.wisc.edu/sscc/pubs/stata_prog1.htm

6/10

12/29/2016

StataProgrammingEssentials

4.by:can'tbeusedwithsvy:
Point#4meansyoucan'trunyourregressionforallracesusingby:,butyoucandoitwithaloop.All
by:doesisidentifythevaluesofraceandthenloopoverthem,andatthispointyouknowhowtodo
thatyourself(thoughby:isfasterwhenyoucanuseit).Theracevariabletakesonthevaluesone,
twoandthree,soanappropriateloopis:

forvaluesrace=1/3{
svy,subpop(ifrace==`race'):regincomeagei.education
}
Whatifyouhadafourthrace,anditsnumberwerenine("Other")ratherthanfour?Youcouldsimply
recodeitandmakeitfour.Butifthat'snotagoodideaforyourproject,you'llhavetoswitchtotheless
structuredforeachloop:

foreachracein1239{
svy,subpop(ifrace==`race'):regincomeagei.education
}
Ontheotherhand,it'snotunusualtohavetoloopoverdozensorevenhundredsofvalues,ornotto
knowaheadoftimewhatvaluesavariabletakeson.Inthatcaseyoucanletthelevelsofcommand
identifythemforyouandputtheminamacro.Thesyntaxis:

levelsofvariable,local(macro)
Forexample,

levelsofrace,local(races)
willlistallthevaluesofthevariableraceandstoretheminamacrocalledraces.Youcanthenloop
overallofthemwith:

foreachracein`races'{
svy,subpop(ifrace==`race'):regincomeagei.education
}
However,thissituationiscommonenoughthatStatawrotespecialcodeforparsingmacrosintolistsfor
looping.Thesyntaxis:

foreachraceoflocalraces{
svy,subpop(ifrace==`race'):regincomeagei.education
}
Notethatracesisnotintheusualmacroquotes:thewholepointofthisconstructionistobypassthe
regularmacroprocessorinfavorofcodethat'sfasterinthecontextofloops.Itmakesaverysmall
difference,butifyoudoenoughloopingitwilladdup.
Onefeatureyou'llmissfromby:isthetextintheoutputtellingyouwhichbygroupiscurrentlybeing
workedon,butyoucanaddityourself.Thefollowingversionoftheloopaddsadisplaycommand
thatinsertstwoblanklinesandthenprintsthecurrentvalueoftheracemacrobeforerunningthe
regression:

foreachraceoflocalraces{
display_newline(2)"Race=`race'"
svy,subpop(ifrace==`race'):regincomeagei.education
}
Usingdisplaytoprintoutthevalueofamacroatagivenpointinyourprogramisalsoaveryuseful
toolfordebugging.
http://www.ssc.wisc.edu/sscc/pubs/stata_prog1.htm

7/10

12/29/2016

StataProgrammingEssentials

Keepinmindthatthiswasjustanexample.Abetterwaytoexaminetheeffectofracewouldprobably
betointeractracewiththeothervariables.Thenewsyntaxforfactorvariablesandinteractionsmakes
thisveryeasy:

svy:regressincomei.race##(c.agei.education)
Thismodelcontainsallthepreviousmodelsifyou'renewtoregressionsthatincludeinteractions,
figuringoutwhythatismightbeagoodexercise.

NestedLoops
Thecommandscontainedinaloopcanincludeotherloops:

forvali=1/3{
forvalj=1/3{
display"`i',`j'"
}
}
Thiscodecreatesthefollowingoutput:
1,1
1,2
1,3
2,1
2,2
2,3
3,1
3,2
3,3
Theinnerloop(theonethatusesj)isexecutedthreetimes,onceforeachvalueofi.Thusthe
displaycommandrunsatotalofninetimes.Notehowthedisplaycommandisindentedtwice:
oncebecauseitispartoftheiloopandoncebecauseitispartofthejloop.Whenyoustartworking
withnestedloopsit'sevenmoreimportantthatyoucaneasilytellwhateachloopcontains.
Consideronefinaldataset:

usehttp://www.ssc.wisc.edu/sscc/pubs/files/stata_prog/monthyear.dta
Thiscontainsmonthlyincomedata,butfortheperiod19902010.Thevariablenamesareintheform

incJan1990,incFeb1990,etc.Togenerateasetofcorrespondingindicatorsyouneedtoloop
overboththemonthsandtheyears:

forvalyear=1990/2010{
foreachmonthinJanFebMarAprMayJunJulAugSepOctNovDec{
genhadInc`month'`year'=(inc`month'`year'>0)if
inc`month'`year'<.
}
}
Thisiscertainlyworkable,butsomewhatcumbersome.Itwouldbeespeciallyawkwardifyouwere
interestedinlags,leads,orchangesovertime:you'dneedcodetotellStatathatthemonthbefore
January1991isDecember1990.Formostpurposesit'seasieriftimeperiodsaresimplynumbered
sequentially.InthiscaseJanuary1990wouldbeperiod1,December1990wouldbeperiod12and
January1991period13.Fortunatelyit'sfairlyeasytoswitch:

http://www.ssc.wisc.edu/sscc/pubs/stata_prog1.htm

8/10

12/29/2016

StataProgrammingEssentials

localperiod1
forvalyear=1990/2010{
foreachmonthinJanFebMarAprMayJunJulAugSepOctNovDec{
renameinc`month'`year'inc`period'
renamehadInc`month'`year'hadInc`period'
localperiod=`period'+1
}
}
Themacroperiodisusedasacounter.Itstartsoutsetto1,andthusasthenestedloopsbegin

incJan1990isrenamedinc1(andsimilarlyhadIncJan1990tohadInc1).Thecommand
localperiod=`period'+1increasesperiodbyone:oncethemacroprocessorisdonewith
itStataproperseeslocalperiod=1+1.Thatcompletestheinnerloop,somonthischangedto
Feb,andincFeb1990isrenamedtoinc2.Theperiodmacroisincreasedagain(Stataproper
nowseeslocalperiod=2+1),monthissettoMar,incMar1990isrenamedtoinc3,and
soforthuntilall252monthsareconverted.(Notethat1990to2010inclusiveis21years.)
Inmakingthisconversionyoulosetheabilitytolookatavariableandknowimmediatelywhatcalendar
monthitdescribes.Butit'smucheasiertoloopover.Thenestedloopscanbereplacedwith:

forvaluesperiod=1/252{

TheImportanceofNamingConventions
ThevariablenameincJan1990containsthreecomponents:thethingbeingobserved(income)and
themonthandyearinwhichitisobserved.Theloopswewrotedependonthevariablenamesdescribing
allthreeinaconsistentway:theywouldfailifthedatasetcontainedincJan1990alongwith

incomeJan1991,incjan1992,incJanuary1993orincJan94.Intherealworldsuch

thingsarenotunusual.Datasetsfromsurveysareaparticularchallengebecausetheirvariablenames
oftencomefromtheformofthequestionnaireratherthantheinformationtheycontain.Takingthetime
torenameyourvariablesinawaythatmakessensetoyouisagoodideaatthebeginningofanyproject,
butifyou'llbeusingloopsit'svitalthatyoucreateandapplyaconsistentnamingconventionfor
variables.

TakeAdvantageofStata'sAutomaticLoops
Nowthatyou'velearnedhowtouseloops,itcanbetemptingtousethemforeverything.Keepinmind
thatmostStatacommandsarealreadyloops(dosomethingtoobservationone,thendoittoobservation
two,etc.)andthoseloopsaremuchfasterthananyforeachorforvaluesloop.Forexample,the
followingusesforvaluestoloopoveralltheobservationsinthedatasetandsetthevalueofyfor
eachobservationtothevalueofxforthatobservation:

geny=.
forvaluesi=1/`=_N'{
replacey=x[`i']if_n==`i'
}
butyou'llgettheexactsameresultfarmorequicklyandeasilywith:

geny=x
Occasionallysomeonefindsataskthatreallydoesrequiresexplicitloopingoverobservations,butit'srare.
Cleverprogrammingcansometimesturnotherloopsintothestandardloopoverobservations,making

foreachorforvaluesunnecessary.Forexample,reshapingwideformpaneldataintolongform
willeliminatetheneedformanyloops.
Gobacktotheoriginal12monthsofincomedata:
http://www.ssc.wisc.edu/sscc/pubs/stata_prog1.htm

9/10

12/29/2016

StataProgrammingEssentials

usehttp://www.ssc.wisc.edu/sscc/pubs/files/stata_prog/months.dta
RecallthatwecreatedhadIncindicatorvariableswiththefollowingloop:

foreachmonthinJanFebMarAprMayJunJulAugSepOctNovDec{
genhadInc`month'=(inc`month'>0)ifinc`month'<.
}
However,you'llgetthesameresultswiththefollowing:

reshapelonginc,i(id)j(month)string
genhadInc=(inc>0)ifinc<.
reshapewideinchadInc,i(id)j(month)string
(Takeamomenttoexaminethedataaftereachstep.)
Reshapingalargedatasetistimeconsuming,sodon'tswitchbetweenwideformandlongformlightly.
Butifyoucanidentifyablockofthingsyouneedtodothatwouldbeeasiertodoinlongform,itmaybe
worthreshapingatthebeginningandendofthatblock.
LastRevised:2/27/2012
20092015UWBoardofRegents,UniversityofWisconsinMadison

http://www.ssc.wisc.edu/sscc/pubs/stata_prog1.htm

10/10

You might also like