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 标签变量的循环_Loops_Label_Stata - Fatal编程技术网

Loops 标签变量的循环

Loops 标签变量的循环,loops,label,stata,Loops,Label,Stata,我试图创建一个用于标记变量的循环。为此,我尝试使用以下宏: local diaglbl "=1 if high blood pressure diag" "=1 if mult diag high blood press" "=1 if coronary hrt disease diag" /// "=1 if angina diagnosis" "=1 if heart attack diag" "=1 if other heart disease diag" "=1 if stroke di

我试图创建一个用于标记变量的循环。为此,我尝试使用以下宏:

local diaglbl "=1 if high blood pressure diag" "=1 if mult diag high blood press" "=1 if coronary hrt disease diag" ///
"=1 if angina diagnosis" "=1 if heart attack diag" "=1 if other heart disease diag" "=1 if stroke diagnosis" ///
"=1 if emphysema diagnosis" "=1 if chronc bronchits last 12 mths" "=1 if high cholesterol diagnosis" ///
"=1 if cancer diagnosis" "=1 if diabetes diagnosis" "=1 if joint pain last 12 months" ///
"=1 if arthritis diagnosis" "=1 if asthma diagnosis"
问题是,当我使用
macro dir
检查宏时,外部引号(只有第一个和最后一个)消失了。我试着在开始时使用`,在结束时使用`,但仍然不起作用。有没有办法解决这个问题,或者有没有更聪明的办法自动标记多个变量,但使用不同的标签?

您需要使用“,”和“”来分隔本地宏

local part1 `""=1 if high blood pressure diag" "=1 if mult diag high blood press" "=1 if coronary hrt disease diag""'
local part2 `""=1 if angina diagnosis" "=1 if heart attack diag" "=1 if other heart disease diag" "=1 if stroke diagnosis""'
local part3 `""=1 if emphysema diagnosis" "=1 if chronc bronchits last 12 mths" "=1 if high cholesterol diagnosis""'
local part4 `""=1 if cancer diagnosis" "=1 if diabetes diagnosis" "=1 if joint pain last 12 months""'
local part5 `""=1 if arthritis diagnosis" "=1 if asthma diagnosis""'

local diaglbl = `"`part1' `part2' `part3' `part4' `part5'"'          

macro dir
在这种情况下,您必须在一行上定义整个宏,或者按照上面的说明分部分进行定义,然后合并这些部分


Statalist的消息来源:,

另一个答案是,以这种方式工作几乎没有明显的好处。您已经准备好几个文本字符串作为变量标签。除非变量名的结构非常简单,否则将它们放在一个包中(这里是宏)是没有意义的。你什么都没告诉我们,所以我们无法帮助你循环这些名字。你只要再把标签从袋子里拿出来就行了

一个简单但实用的方法就是使用一系列命令

label var hbp "=1 if high blood pressure diag" 
label var mhbp "=1 if mult diag high blood press" 
进一步假设这些是指示符(虚拟)变量,更简单的标签如下

label var hbp "high blood pressure diag" 
label var mhbp "mult high blood press" 
会有帮助的,让你解释一下,1表示诊断,0表示不诊断。这就为使用完整的短语留出了更多的空间,这样在表格和图表中看起来会更好

一般原则:

  • 相同的文本无助于区分

  • 循环只有在节省工作和时间的情况下才是好的


  • 当然,您必须重复键入
    标签var
    ,但您最喜欢的文本编辑器应该可以让这变得简单

    下面的代码可能会为您指明一个有用的方向。然而,在为我自己写的文章中,我发现用循环编码比用多个单行命令编码没有任何优势。事实上,知道什么标签与什么变量相匹配是很尴尬的,这为犯错误打开了大门。在我关心的工作中,我会对每个命令使用一行。在我看来,循环并不简单

    关于需要在一行上定义整个宏的句子与
    newlabels
    local宏有关,该宏本身包含带引号的字符串。下面我的代码通过使用
    #diffline
    命令允许Stata代码的一行跨越多行文本,并用复合引号将其括起来,从而解决了这个问题。我要补充的是,虽然我的代码在单独的一行上显示每个标签,但您可以在每一行上放置多个标签,就像您在派生此标签的示例中所做的那样

    clear
    set obs 1
    generate actlim = 1
    generate age = 1
    #delimit ;
    local newvars 
        actlim
        age
        ;
    local newlabels `"
        "actlim label"
        "age label"
        "'
        ;
    #delimit cr
    local nv : word count `newvars'
    forvalues i = 1/`nv' {
        local v : word `i' of `newvars'
        local l : word `i' of `newlabels'
        label variable `v' "`l'"
    }
    describe
    

    问题可能是它们不在同一行上,如果您在开头使用“`”,在结尾使用“`”,并且整个命令都在同一行上,那么它应该工作。您应该显示不工作的代码。没有它我们就猜不到问题。谢谢你,这就是我需要的!好。如果它对您有效,“接受”我的答案将提高您和我在Stack Exchange上的声誉。您应该在每个答案的左上角、投票向上/向下箭头下方看到一个复选标记。