Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | daniel klein <klein.daniel.81@googlemail.com> |
To | statalist@hsphsun2.harvard.edu |
Subject | st: Re: Challenging foreach problem |
Date | Sun, 13 Nov 2011 14:41:30 +0100 |
Anwar, I am not entirely sure, what exactly you are trying to do, partly because this is not my field. From your code I understand, that you want to create a number of indicator variables. Probabaly you want something like: loc A `""350.2", "352.12", "368.58""' loc B `""354.15" "387.89" "369.56""' loc i 0 foreach x in A B { foreach v of varlist pdx-sdx8 { g byte `v'disease_`x' = inlist(`v', ``x'') } } Note the underscore "_" in "`v'disease_`x'" replacing the embedded space in your code, which is not allowed in variable names. Best Daniel -- Dear Statlist reader , I am working on a patient dataset with patients identification code observation( each row), and 15 variables containing ICD9 diagnosis codes ( string varaibles) .Part of the dataset if posted below For diseases of my interest 1)disease A with icd9 codes "350.2" "352.12" "368.58" 2)disease B("354.15" "387.89" "369.56") ] I am using the foreach command foreach x in pdx sdx1 sdx2 sdx3 sdx4 sdx5 sdx6 sdx7 sdx8{ icd9 gen `x'disease A=`x', range"350.2" "352.12" "368.58") } foreach x in pdx sdx1 sdx2 sdx3 sdx4 sdx5 sdx6 sdx7 sdx8{ icd9 gen `x'disease B=`x', range"354.15" "387.89" "369.56") } [...] * * 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/