Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: st: egen with user-defined function
From
Joe Canner <[email protected]>
To
"[email protected]" <[email protected]>
Subject
RE: st: egen with user-defined function
Date
Tue, 20 Aug 2013 01:32:09 +0000
A couple of minor comments:
1. Based on the context and the fact that you named your indicator variable "bool", I suspect you are only interested in knowing whether codes in the range A410-A419 appear at all, not how many there are. In that case, Nick's code can be simplified using the -strpos()- function to look for the first occurrence of "A41x". If this (as I suspect) is a well-behaved list of ICD-10 codes, you could even just look for "A41" and probably be OK.
2. While Nick's code is, as usual, quite clever and quite correct, I am wondering why you don't parse the 4-digit (ICD-10?) codes into separate variables. This, I believe, is probably how they were meant to be processed and so doing reduces the need for clever coding tricks.
Just my $0.02.
Joe Canner
________________________________________
From: [email protected] [[email protected]] on behalf of Nick Cox [[email protected]]
Sent: Monday, August 19, 2013 8:40 PM
To: [email protected]
Subject: Re: st: egen with user-defined function
I don't see quite what -egen- is objecting to here but you have more
fundamental problems.
Wiring in particular names to a program is not illegal but it usually
implies that you are pitching your problem at the wrong level.
(*) I guess that you have some variable -RECORD_AXIS_DATA- and you are
looking for occurrences of the strings "A410" ... "A419".
Your code would at most look in the first observation as you are using
the -if- command
instead of an -if- qualifier.
It would be probably be best if you described the concrete problem, as
my strong guess is that no original program is needed here.
But I'll guess that (*) is the nub of the problem.
clonevar copy = RECORD_AXIS_DATA
qui forval i = 0/9 {
replace copy = subinstr(copy, "A41`i'", "", .)
}
gen count = (length(RECORD_AXIS_DATA) - length(copy)) / 4
So in a copy of the variable we replace each occurrence of "A410"
"A411" ... "A419" with an empty string. Each such deletion reduces the
number of characters by 4.
This was written up in http://www.stata-journal.com/article.html?article=dm0056
Nick
[email protected]
On 20 August 2013 01:03, Jan Barendregt <[email protected]> wrote:
> I'm trying to let egen to use a user-defined function. Here is what I'm doing:
>
> do "C:\Ddrive\MattCooper\Data\test.do"
>
> . capture program drop _grecordparse
>
> . program define _grecordparse
> 1. local bool=0
> 2. local li=1
> 3. local i=0
> 4. while `i' < RECORD_AXIS_COUNT {
> 5. if (substr(RECORD_AXIS_DATA,`li',4)>="A410") & (substr(RECORD_AXIS_DATA,`li',4)<="A419") local bool=
>> `bool'+1
> 6. local li=`li'+4
> 7. local i=`i'+1
> 8. }
> 9. gen `typlist' `varlist' = `bool'
> 10. end
>
> .
> end of do-file
>
> . egen sepsis=recordparse
> unknown egen function recordparse()
> r(133);
>
>
> So I can't get egen to recognise my recordparse function. Any ideas?
>
> I'm using Stata 11.2 on Windows
>
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/