HowtoProgramALoopwithStata

上传人:人*** 文档编号:471063499 上传时间:2023-06-18 格式:DOC 页数:4 大小:98.50KB
返回 下载 相关 举报
HowtoProgramALoopwithStata_第1页
第1页 / 共4页
HowtoProgramALoopwithStata_第2页
第2页 / 共4页
HowtoProgramALoopwithStata_第3页
第3页 / 共4页
HowtoProgramALoopwithStata_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《HowtoProgramALoopwithStata》由会员分享,可在线阅读,更多相关《HowtoProgramALoopwithStata(4页珍藏版)》请在金锄头文库上搜索。

1、Biostat 212Created by Yvette Wild, 9/1/10How to Program A “ Loop” with StataWhe n you have a repetitive task, using a loop helps cut dow n on your program ming work and helps you avoid programming errors. You II get an introduction to loops in Lecture 6. This handout goes into a bit more detail, pro

2、vid ing examples of using loops for creat ing new variables, clea ning variables and an alyz ing variables.Basic con ceptWhe n you program a loop, you have to tell Stata 3 basic thin gs:1) What you want to do repetitively2) How many times you want to do it3) What changes each timeHere s an example o

3、f a basic loop (you can paste this into a do file and run it):forvalues x = 1/5 display x + 1 =display x + 1display Regard ing the basic tasks above:1) All the comma nds betwee n and are the comma nds done repetitively2) The forvalues comma nd tells Stata you want to do the loop 5 times, one for eac

4、h value betwee n1 and 5, in clusive3) The forvalues comma nd also desig nates asthe placeholder for the thing that will cha nge, and the n any where thafx appears in the comma nds in the brackets, Stata substitutes the actual value (1,2, 3, 4 or 5) corresp onding with that run of the loop.Other impo

5、rta nt quirks and no tes about the syn tax:- The ope n brace must appear on the same line Ooreach- Noth ing may follow the ope n brace except comme nts; the first comma nd to be executed must appear on a new line- The “ ” symbol is the quote right below the on your keyboard and the ” symbol is the q

6、uote below the “ on your keyboard.- The close brace must appear on a line by itselfFor more advice about usin gforvalues , and alsoforeach(ano ther way of sett ing up a loop whe n youhave a list of thi ngs you want to loop through in stead of a set of nu mbers), look u|forvalues and foreach in Stata

7、 s help menu. A coup lquiEk notes on forvalues:- You can specify min / max to indicate a sequenee of numbers. For example/3 yields 1,2 and 3- You can specify first ( step ) last to indicate a sequenee that skips numbers. For example 15(5)50 yields 15,20,25,30,35,40,45 and 50.Below are some examples

8、of how to use loops.Example 1: Creat ing New VariablesSay you have a dataset in which weight (in lbs) and height (in in ches) have bee n measured mon thly.However, you need to recalculate the weight (change from lbs to kg) and the height (change from inches to meters), so that you can an alyze mon t

9、hly BMI.Con sider the dataset list ing mon thly weights (in lbs) for 3 in dividuals:idwt1wt2wt3wt4wt5wt6wt7wt8wt9wt10wt11wt121160154153155160r 163163165166 116816916821201221201211251271261261251221231243143144144145147149150151152152153154Manu ally , we can convert weight in lbs to kg by the follow

10、 ing gen erate comma nds: gen erate kgwt1 = wt1/2.2gen erate kgwt2 = wt2/2.2gen erate kgwt3 = wt3/2.2gen erate kgwt4 = wt4/2.2gen erate kgwt5 = wt5/2.2gen erate kgwt6 = wt6/2.2gen erate kgwt7 = wt7/2.2gen erate kgwt8 = wt8/2.2gen erate kgwt9 = wt9/2.2gen erate kgwt10 = wt10/2.2gen erate kgwt11 = wt1

11、1/2.2gen erate kgwt12 = wt12/2.2or, we could use aforeach loop to do this task, like so: foreach var of varlist wt1-wt12 gen erate kgvar = var/2.2Note that we ve usedoteach comma nd here, and can specify a list of variables (aarlist ) that are n amed similarly with nu mbers as no ted above (seeelp v

12、arlist for more details). Stata will cycle through wt1, wt2, wt12, substitut ing the n ames of that variable whererer appears in the bracketed comma nds.We could write a loop to do the same for con vert ing height to meters and the n for calculati ng BMI, or we could put all those comma nds into a s

13、in gle loop, as below: forvalues x = 1/12 gen erate kgwtx = wtx/2.2gen erate mhtx = htx * 2.54/100 gen erate bmix = kgwtx/(mhtxA2) For this one, n ote that we veousaldese comma nd. For ANOTHER in terest ing way to solve thisproblem, con sider reshap ing the data from wide to long and the n doing the

14、 an alysis (see the Reshape Han dout). Many ways to skin the catExample 2: Data Clea ning and Dummy VariablesLoops can help with clearing variables or creating“ dummy SayablBsare given a variable ofage ,but want to want to specifically identify those people ages 25, 26, 27, 28, and 29 years old. The

15、 following loop comma nd may be used: forvalues x=25/29 gen agex=0 replace agex=1 if age=x This loop uses them in / max format to gen erate 5 new dummy variablesage25, age26, age27, age28 and age29 -all variables are set to = 0. Theeplace comma nd on the n ext line the n replaces the 5 n ewly gen erated variables with the value “ 1 ” if that in dividual is equal to the age in that specific label.You can also create dummy variables to represe nt age groups by five-year in tervals using the first ( st

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 医学/心理学 > 基础医学

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号