単純に変数のリストを取得し、実際にデータセットにある変数を含むサブリストを返すミニプログラムを作成しようとしています。
私はこれを 1 つの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
に空になるため、リスト間に交差はありません。
私がここで間違っていることは何か分かりますか?
ダブル ローカル マクロであることは確かですが、修正方法がわかりません。