我可能會推薦三個陣列,但你可能做一個。這很大程度上取決於變量的順序,這在我的書中不是一個好的假設。另外,你會如何自動命名結果?
data want;
set have;
array vars(*) stuff2016--fire2008;
array diffs(*) diffs1-diffs20; *something big enough to hold difference;
do i=1 to dim(vars)-1;
diffs(i) = vars(i)-vars(i+1);
end;
run;
相反,我會強烈建議你使用字典表來查詢您的變量名和動態生成變量列表隨後傳遞到三個不同的陣列,一個2016年,一個2008,一個用於區別。請牢記這一點,libname和memname以大寫形式存儲在Dictionary表中。
data have;
input Stuff2016 Stuff2008 Earth2016 Earth2008 Fire2016 Fire2008;
cards;
123456 5646743 45 456 456 890101
541351 543534534 45 489 489 74456
352352 564889 98 489489 1231 189
464646 542235423 13 15615 1561 78
987654 4561889 44 1212 12121 111
;
run;
proc sql;
select name into :var2016 separated by " "
from sashelp.vcolumn
where libname='WORK'
and memname='HAVE'
and name like '%2016'
order by name;
select name into :var2008 separated by " "
from sashelp.vcolumn
where libname='WORK'
and memname='HAVE'
and name like '%2008'
order by name;
select catx("_", compress(name, ,'d'), "diff") into :vardiff separated by " "
from sashelp.vcolumn
where libname='WORK'
and memname='HAVE'
and name like '%2016'
order by name;
quit;
%put &var2016.;
%put &var2008.;
%put &vardiff.;
data want;
set have;
array v2016(*) &var2016;
array v2008(*) &var2008;
array diffs(*) &vardiff;
do i=1 to dim(v2016);
diffs(i)=v2016(i)-v2008(i);
end;
run;
**美麗!!!!! **哦,非常感謝你!我喜歡你的第二個解決方案,因爲考慮到年前的變量名稱可能會有所不同_動態生成變量名_正是我想要做的。並回答你的問題「另外,你會如何自動命名結果?」正如你正確預測的那樣,我會將結果存儲爲帶有後綴diff的列或類似的東西。 – user650738