Statalist


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

st: WG: RE: Identify min and max observations


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: WG: RE: Identify min and max observations
Date   Wed, 30 Sep 2009 23:54:06 +0200

<> 



*************
clear*

inp byte id str8(admdate diagnosis)
1     	12may06       A409
1				20jun06				A409
1				30aug06    		A409	
1				13dec06				C341
2				14dec06				A409
2				17dec06				A409
3				18dec06				A409
3				19dec06				A409
3				20dec06				A409
3				21dec06				A409
4				27dec06				A409
4				28dec06				C341
5				30dec07				D402
end

compress
levelsof diagnosis

qui foreach di in `r(levels)'{
	/*check whether any diagnosis
	of this kind anywhere within id */ 
	bys id: egen byte anydiag`di'= /* 
	*/ max(diagnosis=="`di'")
	/*keep it only for last 
	entry per id */ 
	by id: replace anydiag`di'= /* 
	*/ anydiag`di'*(_n==_N)
	/*count now accurate as only
	once per id */ 
	cou if anydiag`di'==1
	/*keep the result as 
	a variable for future 
	reference */
	gen countdiag`di'=r(N)
	la var countdiag`di' /* 
	*/ "Count of Patients with Condition"
	/*display the result */ 
	noi di _n as txt /* 
	*/ "For diagnosis "  /* 
	*/ in red "`di'" as txt " there "  /* 
	*/ "are " in red  `r(N)'  /* 
	*/ as txt " cases" _n
}

qui drop anydiag*

list, noobs sepby(id)
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: Thomas Cars [mailto:thomas.cars@pubcare.uu.se] 
Gesendet: Mittwoch, 30. September 2009 23:14
An: 'Martin Weiss'
Betreff: SV: RE: Identify min and max observations

Thank you for you answer. Worked perfect!

Another question:

In my dataset I also have data of diagnosis (ICD-10-codes)

Example:

Id 	Adm.date   	Diagnosis
1     	12may06       	A409
1	20jun06		A409
1	30aug06    	A409	
1	...		C341
2	...		A409
2			A409
3			A409
3			A409
3			A409
3			A409
4			A409
4			C341

And so on....

I now want to calculate how many unique subjects (id:s) that have the
diagnosis A409. The result should be 4 (Id 1,2,3,4)
And I also want to calculate how many unique patients that have the
diagnosis C341. The result should be 2 (Id 1 and 4)

How can I calculate this in Stata?

Thanks for your help!
Thomas


-----Ursprungligt meddelande-----
Från: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] För Martin Weiss
Skickat: den 27 september 2009 15:42
Till: statalist@hsphsun2.harvard.edu
Ämne: st: RE: Identify min and max observations


<>

You want your admdate to be numeric for sure. You could then -sort- within
id and take the first and last value. A more general solution is:

**************
clear*

inp id str10  admdate      
1  12may06       
1  20jun06
1  30aug06       
2  10may06       
2  10sep06
2  20dec06
2  10jan07       
3  10aug06       
3  20sep06
3  10dec06
3  15jan07
3  10feb07  
end

compress

gen date=date(admdate, "DM20Y")
format date %tdMonth_DD,_CCYY

list, noobs  

tempvar min max
bys id: egen `min'=min(date)
bys id: egen `max'=max(date)

bys id: gen byte mindate=date==`min'
bys id: gen byte maxdate=date==`max'

list, noobs sepby(id)
**************


HTH
Martin


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Thomas Cars
Sent: Sonntag, 27. September 2009 14:18
To: statalist@hsphsun2.harvard.edu
Subject: st: Identify min and max observations

Hi,
I have a dataset including subjects (id) and date for administration  
of a specific drug (adm.date)

I now want to construct a variable telling me which one of the  
observations (adm.date) in each subject (id) that has the lowest date  
(min) and the highest date(max).

The number of adm.date can vary in each subject from 1 to 10.


Example (min=1 och max=2)



Id                   adm.date        min/max

1                    12may06         1
1                    20jun06
1                    30aug06         2
2                    10may06         1
2                    10sep06
2                    20dec06
2                    10jan07         2
3                    10aug06         1
3                    20sep06
3                    10dec06
3                    15jan07
3                    10feb07         2

And so on?

I hope anyone can help me!

Thomas


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index