2015-08-14 37 views
0

設置宏之內我可以用下面的方式來創建多個數據集:創建數據在SAS

data D3RGDPX (drop=BasePeriod BaseYear Forecast10Year) 
D3GDPX (drop=BasePeriod BaseYear Forecast10Year) 
D3BFIX (drop=BasePeriod BaseYear Forecast10Year) 
D3CPAT (drop=BasePeriod BaseYear Forecast10Year) 
D3IP  (drop=BasePeriod BaseYear Forecast10Year) 
D3TPHS (drop=BasePeriod BaseYear Forecast10Year) 
D3PPI (drop=BasePeriod BaseYear Forecast10Year) 
D3CPI (drop=BasePeriod BaseYear Forecast10Year) 
D3UNPR (drop=BasePeriod BaseYear Forecast10Year) 
D3WMFG (drop=BasePeriod BaseYear Forecast10Year) 
D3RTTR (drop=BasePeriod BaseYear Forecast10Year) 
D3AUTODF (drop=BasePeriod BaseYear Forecast10Year) 
D3SPIF (drop=BasePeriod BaseYear Forecast10Year); 
set my.data; 
    if  Type='RGDPX' then output D3RGDPX; 
    else if Type='GDPX' then output D3GDPX; 
    else if Type='BFIX' then output D3BFIX; 
    else if Type='CPAT' then output D3CPAT; 
    else if Type='IP'  then output D3IP; 
    else if Type='TPHS' then output D3TPHS; 
    else if Type='PPI' then output D3PPI; 
    else if Type='CPI' then output D3CPI; 
    else if Type='UNPR' then output D3UNPR; 
    else if Type='WMFG' then output D3WMFG; 
    else if Type='RTTR' then output D3RTTR; 
    else if Type='AUTODF' then output D3AUTODF; 
    else if Type='SPIF' then output D3SPIF; 
run; 

,我打算給宏內做到這一點,但每次宏將覆蓋其他使得只有最後的數據集將保留,並且不會創建多個數據集。我如何在宏中完成這項任務?

新增自評:

%macro new (data=, set=, Type=); 
data &data (drop=BasePeriod BaseYear Forecast10Year); 
set my.data; 
if Type = "&Type" then output &data; 
run; 
%mend new; 
%new (data =D3RGDPX, Type = RGDPX); 
%new (data=D3GDPX, Type=GDPX); 

等。

+0

請向我們展示您迄今嘗試使用的代碼。 – DomPazz

+0

%macro new(data =,set =,Type =); data&data(drop = BasePeriod BaseYear Forecast10Year); set my.data; if Type =「&Type」then then &data; run; %修補新; %new(data = D3RGDPX,Type = RGDPX); %new(data = D3GDPX,Type = GDPX); 依此類推。 – user2916331

+0

我把你的代碼放到問題中 - 在那裏讀起來更容易。 – DomPazz

回答

0

我不明白爲什麼你的宏創建你描述的問題。如果你想要一個宏來運行這個,你可以試試

%macro outds(types=, input=); 
%local i n type; 
%let n = %sysfunc(countw(&types)); 
data 
%do i=1 %to &n; 
    %let type=%scan(&types,&i); 
    D3&type (drop=BasePeriod BaseYear Forecast10Year) 
%end; 
; 
set &input; 

%do i=1 %to &n; 
    %let type=%scan(&types,&i); 
    if type="&type" then output D3&type; 
%end; 
run; 
%mend; 

options mprint; 
%outds(types=RGDPX GDPX BFIX CPAT IP TPHS PPI CPI UNPR WMFG RTTR AUTODF SPIF, 
     input=my.data); 

這將構建喜歡你的實例中的數據步驟採取的類型的列表。