Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

st: Re: Challenging foreach problem

From   daniel klein <>
Subject   st: Re: Challenging foreach problem
Date   Sun, 13 Nov 2011 14:41:30 +0100


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.



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:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index