Introduction to Stata 8 - (GRIPS
Introduction to Stata 8 - (GRIPS
Introduction to Stata 8 - (GRIPS
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
10.4. Sorting data<br />
sort<br />
To sort your data according <strong>to</strong> mpg (primary key) and weight (secondary key):<br />
sort mpg weight<br />
[R] sort, [R] gsort<br />
sort only sorts in ascending order; gsort is more flexible, but slower. To sort by mpg<br />
(ascending) and weight (descending) the command is:<br />
gsort mpg –weight<br />
10.5. Numbering observations [U] 16.7<br />
The variable age in the third observation can be referred <strong>to</strong> as age[3]. The principle is:<br />
First observation<br />
age[1]<br />
Last observation<br />
age[_N]<br />
Current observation age[_n]<br />
Previous (lag) observation age[_n-1]<br />
Next (lead) observation age[_n+1]<br />
Observation 27<br />
age[27]<br />
From a patient register you have information about hospital admissions, one or more per<br />
person, identified by cpr and admdate (admission date). You want <strong>to</strong> construct the<br />
following variables: obsno (observation number), persno (internal person ID), admno<br />
(admission number), adm<strong>to</strong>t (patient's <strong>to</strong>tal number of admissions).<br />
. use c:\dokumenter\proj1\alfa1.dta<br />
. sort cpr admdate<br />
. gen obsno=_n // _n is the observation number<br />
. by cpr: gen admno=_n // _n is obs. number within each cpr<br />
. by cpr: gen adm<strong>to</strong>t=_N // _N is <strong>to</strong>tal obs. within each cpr<br />
. sort admno cpr // all admno==1 first<br />
. gen persno=_n if admno==1 // give each person number if admno==1<br />
. sort obsno // original sort order<br />
. replace persno=persno[_n-1] if persno >=. // replace missing persno<br />
. save c:\dokumenter\proj1\alfa2.dta<br />
. list cpr admdate obsno persno admno adm<strong>to</strong>t in 1/7<br />
cpr admdate obsno persno admno adm<strong>to</strong>t<br />
1. 0605401234 01.05.1970 1 1 1 3<br />
2. 0605401234 06.05.1970 2 1 2 3<br />
3. 0605401234 06.05.1971 3 1 3 3<br />
4. 0705401234 01.01.1970 4 2 1 1<br />
5. 0705401235 01.01.1970 5 3 1 1<br />
6. 0805402345 01.01.1970 6 4 1 2<br />
7. 0805402345 10.01.1970 7 4 2 2<br />
. summarize persno // number of persons (max persno)<br />
. anycommand if admno==1 // first admissions<br />
. anycommand if admno==adm<strong>to</strong>t // last admissions<br />
. tab1 adm<strong>to</strong>t if admno==1 // distribution of n of admissions<br />
You may also create a keyfile linking cpr and persno, and remove cpr from your<br />
analysis file. See example 16b in Take good care of your data.<br />
24