我正在參加SAS課程,並有一個項目可以完成。當然,我並不是在尋找確切的答案(儘管那樣會很好),但是我們真的很感激正確的方向。使用SAS Macro創建間隔
問題是編寫一個宏,該宏創建年齡從一個人年齡的年齡組。給出的代碼是:
data a ;
infile in1 ;
input name $ age ;
if 30 le age < 33 then agegrp = 30 ;
else if 33 le age < 36 then agegrp = 33 ;
else if 36 le age < 39 then agegrp = 36 ;
else if 39 le age < 42 then agegrp = 39 ;
else if 42 le age < 45 then agegrp = 42 ;
else if 45 le age < 48 then agegrp = 45 ;
else if 48 le age < 51 then agegrp = 48 ;
else if 51 le age < 54 then agegrp = 51 ;
else if 54 le age < 57 then agegrp = 54 ;
else if 57 le age then agegrp = 57 ;
我的工作是編寫一個生成這些if-then/else語句的SAS宏。這是我到目前爲止。我意識到這不會運行,但我想要弄清楚一些事情,告訴你們這些任務有多遠。
options ls=78 formdlim=' ' ;
%MACRO CODEIT(start= , stop= , count= , old= , new=);
%let x=&start;
%let y=%eval(&start+&count);
if &start
%do x=&start+&count %to &stop %by &count ;
<= &old < &x then &new=&start ;
%let start=&x ;
else if &start
%end ;
<= &old then &new=&start ;
%MEND
data a ;
input age ;
datalines;
30 31 32 33 34 37
38 39 39 41 42 45
46 46 47 49 50 50
52 53 54 55 56 57
%CODEIT(start=30, stop=57, count=3, old=0, new=0);
我非常感謝你提前給你提供的所有幫助。
是否需要成爲宏?這通常不是實現這一目標的最佳實踐。 – Joe
它需要是一個宏。該宏應該能夠獲取人們年齡的數據集,並根據數據中的年齡範圍生成不同範圍的if-then/else語句。非常感謝您的快速回復。 – William
我希望這位教授至少在「這是可怕的編程習慣,但是我爲了學習的目的而要求你這麼做」之前寫過? :) – Joe