我正在尝试编写一个小程序,它只需要一个变量列表,并返回一个包含数据集中实际变量的子列表。
我在一个文件中多次执行此do
操作(不是按顺序执行的,因此没有循环),因此使用快速程序而不是每次都有效地复制此代码更容易。
该程序的简化版本如下。主要问题出现在第 6 行。程序的第一个参数应该是本地宏的名称,其中包含要与数据集中的变量名称进行比较的变量名称。因此,例如,如果本地宏是list1
,则程序的第一个参数是list1
,并且我想存储一个新的本地宏vlist
,它包含 中的所有变量list1
。
但是当我尝试这样做时:
``1''
生成的本地宏vlist
最终只是空的,而本地allvars
很好。
我的程序代码如下:
clear
cap program drop lm
program define lm
* Create a new local, vlist, that is all the variables in the local macro identified in argument 1
local vlist ``1''
* Create a new local, allvars, that is all variables in teh dataset
qui ds, a
local allvars `r(varlist)'
* Display both macros, to illustrate that vlist is empty, while allvars contains all variables
di as text "vlist: " as result "`vlist'"
di as text "allvars: " as result "`allvars'" _newline
* Create a new local macro that is the intersection of the two lists (if it worked)
local `1'_inter : list vlist & allvars
* Display different messages depending on the outcome (e.g. if a list was created, or empty)
if missing("``1'_inter'") di as error "There are no common variables between `1' and the dataset."
else di as result "`1' intersection with varlist _all now stored as local: `1'_inter"
end
clear
input float(var1 var2 var3 var4 var5) // Input irrelevant data
. . . . .
end
/* Next, create a macro with a list of variables that are in the dataset
(e.g. var1 and var2), and even some that are not in teh data (var7). */
local list1 var1 var2 var7
* Execute the list-match program
lm list1
如您所见,本地vlist
最终为空,因此列表之间没有交集。
知道我在这里做错了什么吗?
我确定这是双本地宏,但我不确定如何修复它。