[英]SAS Macro Code Variable Creation
我想在數據集中創建多個標志(以及許多派生的數字特征),但是我正努力使代碼正常工作。
我正在使用帶條件的迭代循環。 對決議的任何幫助或建議,將不勝感激。
這是我嘗試的代碼,它創建一個標志,但這不是我的新數據集中的變量。
%macro gb1(howmany);
%do i=1 %to &howmany;
%if status&i = 1 %then %let &gb1_&i = 1;
%else %if status&i = 2 %then %let &gb1_&i = 1;
%else %if dpd&i >= 2 %then %let &gb1_&i = 1;
%else %if ((dpd&i < 2) and (dpd&i >= 1)) %then %let &gb1_&i = 2;
%else %let &gb1_&i = 1;
%end;
%mend gb1;
data test;
set y2014.perf_data_derive (where=((dpd15 <= 2) and (prod = 1)));;
%gb1(36);
run;
感謝您能提供的任何幫助。
%if
, %then
和%else
等是宏語句,用於評估帶有宏變量的表達式,以便有條件地提交SAS代碼。 似乎您沒有嘗試執行任何操作; 您只想重復涉及數據集中變量(而不是宏變量)的數據步驟語句。 為此,請像在數據步驟中一樣使用語句:
%macro gb1(howmany);
%do i=1 %to &howmany;
if status&i = 1 then gb1_&i = 1;
else if status&i = 2 then gb1_&i = 1;
else if dpd&i >= 2 then gb1_&i = 1;
else if ((dpd&i < 2) and (dpd&i >= 1)) then gb1_&i = 2;
else let gb1_&i = 1;
%end;
%mend gb1;
我認為您對宏語言感到困惑。 它(通常)用於生成SAS代碼。 基本上,它使您無需輸入。 它對DATA步驟變量不做任何事情。 DATA步驟代碼可讓您操作數據步驟變量,宏語言處理宏變量。
您的輸入數據集perf_data_derive是否具有status1 status2 ... status36和dpd1 dpd2 ... dpd36之類的變量? 您正在嘗試創建新的變量gb1_1 gb1_2 ... gb1_36嗎? 如果是這樣,您應該研究如何使用數組。 ARRAY語句是DATA步驟語句,可用於引用數據步驟變量。 您可以將其編碼為(未試用):
data test;
set y2014.perf_data_derive (where=((dpd15 <= 2) and (prod = 1)));
array status{36} ; *array elements status1-status36;
array dpd{36} ;
array gb1_{36} ; *array statement can create new variables! ;
do i=1 to 36;
if status{i}=1 then gb1_{i} = 1;
else if status{i}=2 then gb1_{i} = 1;
else if dpd{i} >=2 then gb1_{i} = 1;
*etc;
end;
drop i;
run;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.