Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 如何在stata中为宏创建循环?_Loops_Macros_Stata - Fatal编程技术网

Loops 如何在stata中为宏创建循环?

Loops 如何在stata中为宏创建循环?,loops,macros,stata,Loops,Macros,Stata,我有一个非常大的数据集,但简而言之,我用以下示例演示了数据: * Example generated by -dataex-. To install: ssc install dataex clear input float(patid death dateofdeath) 1 0 . 2 0 . 3 0 . 4 0 . 5 1 15007 6 0 . 7 0 . 8 1 15526 9 0 . 10 0 . en

我有一个非常大的数据集,但简而言之,我用以下示例演示了数据:

* Example generated by -dataex-. To install: ssc install dataex
clear
input float(patid death dateofdeath)
 1 0     .
 2 0     .
 3 0     .
 4 0     .
 5 1 15007
 6 0     .
 7 0     .
 8 1 15526
 9 0     .
10 0     .
end
format %d dateofdeath
我试图根据死亡日期取样进行病例对照研究。在这个阶段,我需要首先创建一个变量,所有参与者的每个死亡日期都是重复的(因此我们最终得到了一个包含20名参与者的数据集)和一个与相应病例的患者id
patid
相当的
pairid

我为一个案例创建了一个宏(有效),但我发现很难在循环中对所有案例(其中
death==1
)重复该宏

成功的宏如下所示:

local i "5" //patient id who died 
     
gen pairid= `i'
gen matchedindexdate = dateofdeath
replace matchedindexdate=0 if pairid != patid
gsort matchedindexdate
replace matchedindexdate= matchedindexdate[_N]
format matchedindexdate %d
save temp`i'
我尝试的循环是:

* (min and max patid id)     
forval j = 1/10  {
    count if patid == `j' & death==1
    if r(N)=1 {
        gen pairid= `j'
        gen matchedindexdate = dateofdeath
        replace matchedindexdate=0 if pairid != patid
        gsort matchedindexdate
        replace matchedindexdate= matchedindexdate[_N]
        save temp/matched`j'
    }
}

use temp/matched1, clear
forval i=2/10 {
    capture append using temp/matched`i'
    save matched, replace
}
但我得到:

invalid syntax

如何进行循环?

我终于解决了问题,请检查:


如果r(N)=1是直接的错误,则在
交叉发布。应该是
==
。很好,你解决了这个问题。现在的关键问题是,这条线索将来是否会让其他人受益。由于我对此表示怀疑,我建议删除它。