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

st: AW: irecode problem

From   "Martin Weiss" <>
To   <>
Subject   st: AW: irecode problem
Date   Thu, 5 Mar 2009 09:44:58 +0100


With regard to the length issue, have you had a look at yet?


-----Ursprüngliche Nachricht-----
[] Im Auftrag von Kieran McCaul
Gesendet: Donnerstag, 5. März 2009 00:31
Betreff: st: irecode problem

My problem is concerned with using the -irecode- function in a program
when the number of cutpoints (the xi used in the function) varies.

I'm accumulating probabilities across a number of groups and then
picking someone from these groups at random based on these
probabilities.  So I've approached this using the -irecode- function.  I
sum the probabilities in each group and then create a running cumulative
probability which I then append to the -irecode- function that I've
store in a local as a string.  When someone is picked, I recalculate the
cumulative probabilities excluding those who have already been picked.
I need to generate the -irecode- function this way because, while I know
how many groups there are to begin with (38), as I progressively pick
individuals, the number of groups will diminish.

The code is below.  There is nothing wrong with it as far as I know and
it will work with a small number of groups, but with a large number of
groups, the string length exceeds the 244 character limit.  So the comm
macro ends up with only the first 244 characters of the command.

* maxg is the total number of groups   
* prob is each person's probability

local p = uniform()   /* generate a probability from U(0,1)  */

local p0 = 0

local comm = "irecode(`p', 0"

/* The total number of groups is maxg
   This loop calculates the total probability in each group,
   adds it to the cumulative probability, 
   and appends it to the string stored in the comm local */

forvalues i = 1/`maxg'  {
	qui summ prob if picked==0 & group==`i'      /* sum of
probabilities in a group  */
	local pg`i' = r(sum)                         /* sum(prob) in
group i  */
	local k = `i'-1                              /* number of
previous group */
	local p`i' = `p`k'' + `pg`i''                /* add this sum to
previous sum  */
	local comm = "`comm'" + ", `p`i''"

local comm = "`comm'" + ")"

Now I can get around this, I suppose, by getting rid of the comm macro
altogether and instead using a series of -if- statements.

if `maxg' == 38  {
local group = irecode(`p',0,`p1',`p2',`p3', ...,`p38')
else if `maxg' == 37 {
local group = irecode(`p',0,`p1',`p2',`p3', ...,`p37')
else if `maxg' == 36 {
local group = irecode(`p',0,`p1',`p2',`p3', ...,`p36')
else if `maxg' == 35 {
local group = irecode(`p',0,`p1',`p2',`p3', ...,`p35')
etc, etc

But this is a tad inelegant.  Can anyone think of anything more


Kieran McCaul MPH PhD
WA Centre for Health & Ageing (M573)
University of Western Australia
Level 6, Ainslie House
48 Murray St
Perth 6000
Phone: (08) 9224-2140
Fax: (08) 9224 8009
The fact that no one understands you doesn't make you an artist.

*   For searches and help try:

*   For searches and help try:

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