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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.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 跨列添加第一个未丢失的观察值_Loops_Foreach_Stata - Fatal编程技术网

Loops 跨列添加第一个未丢失的观察值

Loops 跨列添加第一个未丢失的观察值,loops,foreach,stata,Loops,Foreach,Stata,我目前有一些模型的偏差,如以下数据所示: year model_2015 model_2016 model_2017 2016 15 . . 2017 20 10 . 2018 30 20 30 变量2015在2015中执行,与2016、2017、2018存在偏差;变量2016车型用于2017以及2018,依此类推 我想创建一个变

我目前有一些模型的偏差,如以下数据所示:

year   model_2015   model_2016   model_2017
2016      15            .           . 
2017      20           10           .
2018      30           20           30
变量
2015
2015
中执行,与
2016
2017
2018
存在偏差;变量
2016车型
用于
2017
以及
2018
,依此类推

我想创建一个变量,对每个变量的第一个观测值求和

所以对于这个例子:

first = 15 + 10 + 30 = 55
我假设我必须做一个循环,但我不知道如何去做


编辑:


理想情况下,我还希望有一个解决方案,添加第二个、第三个等非缺失观测值

以下内容对我有用:

generate first = model_2015[1] + model_2016[2] + model_2017[3]
然而,这里有一个更一般的方法:

clear

input year   model_2015   model_2016   model_2017
2016      15            .           . 
2017      20           10           .
2018      30           20           30
end

generate id = 1
tempfile myfile
save `myfile'

collapse (firstnm) model*, by(id)
egen first = rowtotal(model*)
keep id first
merge 1:m id using `myfile'

drop id _merge
order year model* first

list, abbreviate(15)

     +-----------------------------------------------------+
     | year   model_2015   model_2016   model_2017   first |
     |-----------------------------------------------------|
  1. | 2016           15            .            .      55 |
  2. | 2017           20           10            .      55 |
  3. | 2018           30           20           30      55 |
     +-----------------------------------------------------+

编辑:

下面是一个更通用的解决方案:

clear

input year   model_2015   model_2016   model_2017
2016      15            .           . 
2017      20           10           .
2018      30           20           30
2019      40           10           10
end

local i = 0
foreach v of varlist model* {
    local ++i
    local vals
    forvalues j = 1 / `=_N' {
        if !missing(`v'[`j']) local vals `vals' `=`v'[`j']'
    }
    local ind_`i' `: word 1 of `vals'' // CHANGE THIS NUMBER
    local ind_all `ind_all' `ind_`i''
}
generate first = `= subinstr("`ind_all'", " ", "+", `= wordcount("`ind_all'") - 1')'
结果:

list, abbreviate(15)

     +-----------------------------------------------------+
     | year   model_2015   model_2016   model_2017   first |
     |-----------------------------------------------------|
  1. | 2016           15            .            .      55 |
  2. | 2017           20           10            .      55 |
  3. | 2018           30           20           30      55 |
  4. | 2019           40           10           10      55 |
     +-----------------------------------------------------+

     +-----------------------------------------------------+
     | year   model_2015   model_2016   model_2017  second |
     |-----------------------------------------------------|
  1. | 2016           15            .            .      50 |
  2. | 2017           20           10            .      50 |
  3. | 2018           30           20           30      50 |
  4. | 2019           40           10           10      50 |
     +-----------------------------------------------------+

     +-----------------------------------------------------+
     | year   model_2015   model_2016   model_2017   third |
     |-----------------------------------------------------|
  1. | 2016           15            .            .      40 |
  2. | 2017           20           10            .      40 |
  3. | 2018           30           20           30      40 |
  4. | 2019           40           10           10      40 |
     +-----------------------------------------------------+

请注意,在本例中,为了更好地进行说明,我使用了一个稍加修改的示例。

以下内容适用于我:

generate first = model_2015[1] + model_2016[2] + model_2017[3]
然而,这里有一个更一般的方法:

clear

input year   model_2015   model_2016   model_2017
2016      15            .           . 
2017      20           10           .
2018      30           20           30
end

generate id = 1
tempfile myfile
save `myfile'

collapse (firstnm) model*, by(id)
egen first = rowtotal(model*)
keep id first
merge 1:m id using `myfile'

drop id _merge
order year model* first

list, abbreviate(15)

     +-----------------------------------------------------+
     | year   model_2015   model_2016   model_2017   first |
     |-----------------------------------------------------|
  1. | 2016           15            .            .      55 |
  2. | 2017           20           10            .      55 |
  3. | 2018           30           20           30      55 |
     +-----------------------------------------------------+

编辑:

下面是一个更通用的解决方案:

clear

input year   model_2015   model_2016   model_2017
2016      15            .           . 
2017      20           10           .
2018      30           20           30
2019      40           10           10
end

local i = 0
foreach v of varlist model* {
    local ++i
    local vals
    forvalues j = 1 / `=_N' {
        if !missing(`v'[`j']) local vals `vals' `=`v'[`j']'
    }
    local ind_`i' `: word 1 of `vals'' // CHANGE THIS NUMBER
    local ind_all `ind_all' `ind_`i''
}
generate first = `= subinstr("`ind_all'", " ", "+", `= wordcount("`ind_all'") - 1')'
结果:

list, abbreviate(15)

     +-----------------------------------------------------+
     | year   model_2015   model_2016   model_2017   first |
     |-----------------------------------------------------|
  1. | 2016           15            .            .      55 |
  2. | 2017           20           10            .      55 |
  3. | 2018           30           20           30      55 |
  4. | 2019           40           10           10      55 |
     +-----------------------------------------------------+

     +-----------------------------------------------------+
     | year   model_2015   model_2016   model_2017  second |
     |-----------------------------------------------------|
  1. | 2016           15            .            .      50 |
  2. | 2017           20           10            .      50 |
  3. | 2018           30           20           30      50 |
  4. | 2019           40           10           10      50 |
     +-----------------------------------------------------+

     +-----------------------------------------------------+
     | year   model_2015   model_2016   model_2017   third |
     |-----------------------------------------------------|
  1. | 2016           15            .            .      40 |
  2. | 2017           20           10            .      40 |
  3. | 2018           30           20           30      40 |
  4. | 2019           40           10           10      40 |
     +-----------------------------------------------------+

请注意,在本例中,为了更好地进行说明,我使用了一个稍加修改的示例。

下面的代码可能就是您正在寻找的循环:

forvalues i = 1 / `=_N' {
    generate S_`i' = 0
    forvalues j = `i' / `=_N' {
        capture replace S_`i' = S_`i' + model_`=2015+`j'-`i''[`j']
    }
}

下面的代码可能是您正在寻找的循环:

forvalues i = 1 / `=_N' {
    generate S_`i' = 0
    forvalues j = `i' / `=_N' {
        capture replace S_`i' = S_`i' + model_`=2015+`j'-`i''[`j']
    }
}